Skip to content

Commit 5566a2d

Browse files
committed
use monaco's eol detection
1 parent db3d572 commit 5566a2d

File tree

3 files changed

+7
-24
lines changed

3 files changed

+7
-24
lines changed

web_src/js/features/codeeditor.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tinycolor from 'tinycolor2';
2-
import {basename, extname, isObject, isDarkTheme, detectEol} from '../utils.js';
2+
import {basename, extname, isObject, isDarkTheme} from '../utils.js';
33
import {onInputDebounce} from '../utils/dom.js';
44

55
const languagesByFilename = {};
@@ -119,8 +119,11 @@ export async function createMonaco(textarea, filename, editorOpts) {
119119

120120
const model = editor.getModel();
121121

122-
// set eol mode to value from editorconfig or content-detected value
123-
model.setEOL(monaco.editor.EndOfLineSequence[eol || detectEol(value) || 'LF']);
122+
// monaco performs auto-detection of dominant EOL in the file, biased towards LF for
123+
// empty files, if there is an editorconfig value, override this detected value
124+
if (eol) {
125+
model.setEOL(monaco.editor.EndOfLineSequence[eol]);
126+
}
124127

125128
model.onDidChangeContent(() => {
126129
textarea.value = editor.getValue();
@@ -197,6 +200,6 @@ function getEditorConfigOptions(ec) {
197200
opts.insertSpaces = ec.indent_style === 'space';
198201
opts.useTabStops = ec.indent_style === 'tab';
199202
const eol = ec.end_of_line?.toUpperCase();
200-
if (['LF', 'CRLF'].includes(eol)) opts.eol = eol;
203+
opts.eol = ['LF', 'CRLF'].includes(eol) ? eol : undefined;
201204
return opts;
202205
}

web_src/js/utils.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,3 @@ export function decodeURLEncodedBase64(base64url) {
128128
.replace(/_/g, '/')
129129
.replace(/-/g, '+'));
130130
}
131-
132-
// Detect dominant line ending in a string
133-
// based on https://github.com/sindresorhus/detect-newline
134-
export function detectEol(str) {
135-
const newlines = (str || '').match(/\r?\n/g) || [];
136-
if (newlines.length === 0) return undefined;
137-
const crlf = newlines.filter((newline) => newline === '\r\n').length;
138-
const lf = newlines.length - crlf;
139-
if (crlf === lf) return undefined;
140-
return crlf > lf ? 'CRLF' : 'LF';
141-
}

web_src/js/utils.test.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
basename, extname, isObject, stripTags, parseIssueHref,
44
parseUrl, translateMonth, translateDay, blobToDataURI,
55
toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64,
6-
detectEol,
76
} from './utils.js';
87

98
test('basename', () => {
@@ -114,11 +113,3 @@ test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => {
114113
expect(Array.from(decodeURLEncodedBase64('YQ'))).toEqual(Array.from(uint8array('a')));
115114
expect(Array.from(decodeURLEncodedBase64('YQ=='))).toEqual(Array.from(uint8array('a')));
116115
});
117-
118-
test('detectEol', () => {
119-
expect(detectEol(undefined)).toEqual(undefined);
120-
expect(detectEol('')).toEqual(undefined);
121-
expect(detectEol('a\nb')).toEqual('LF');
122-
expect(detectEol('a\nb\r\n')).toEqual(undefined);
123-
expect(detectEol('a\nb\r\nc\r\n')).toEqual('CRLF');
124-
});

0 commit comments

Comments
 (0)