Skip to content

Commit 1135e8e

Browse files
authored
Use model EOL for newline handling in insertion view (microsoft#239312)
use model EOL to handle new line shifting and trimming in insertion view
1 parent e7e862e commit 1135e8e

File tree

1 file changed

+10
-8
lines changed
  • src/vs/editor/contrib/inlineCompletions/browser/view/inlineEdits

1 file changed

+10
-8
lines changed

src/vs/editor/contrib/inlineCompletions/browser/view/inlineEdits/insertionView.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ export class InlineEditsInsertionView extends Disposable implements IInlineEdits
3030
if (!state) { return undefined; }
3131

3232
const textModel = this._editor.getModel()!;
33+
const eol = textModel.getEOL();
3334

34-
if (state.startColumn === 1 && state.lineNumber > 1 && textModel.getLineLength(state.lineNumber) !== 0 && state.text.endsWith('\n') && !state.text.startsWith('\n')) {
35+
if (state.startColumn === 1 && state.lineNumber > 1 && textModel.getLineLength(state.lineNumber) !== 0 && state.text.endsWith(eol) && !state.text.startsWith(eol)) {
3536
const endOfLineColumn = textModel.getLineLength(state.lineNumber - 1) + 1;
36-
return { lineNumber: state.lineNumber - 1, column: endOfLineColumn, text: '\n' + state.text.slice(0, -1) };
37+
return { lineNumber: state.lineNumber - 1, column: endOfLineColumn, text: eol + state.text.slice(0, -eol.length) };
3738
}
3839

3940
return { lineNumber: state.lineNumber, column: state.startColumn, text: state.text };
@@ -88,12 +89,12 @@ export class InlineEditsInsertionView extends Disposable implements IInlineEdits
8889
}
8990
this._editorObs.versionId.read(reader);
9091
const textModel = this._editor.getModel()!;
92+
const eol = textModel.getEOL();
9193

92-
const cleanText = state.text.replace('\r\n', '\n');
93-
const textBeforeInsertion = cleanText.startsWith('\n') ? '' : textModel.getValueInRange(new Range(state.lineNumber, 1, state.lineNumber, state.column));
94+
const textBeforeInsertion = state.text.startsWith(eol) ? '' : textModel.getValueInRange(new Range(state.lineNumber, 1, state.lineNumber, state.column));
9495
const textAfterInsertion = textModel.getValueInRange(new Range(state.lineNumber, state.column, state.lineNumber, textModel.getLineLength(state.lineNumber) + 1));
95-
const text = textBeforeInsertion + cleanText + textAfterInsertion;
96-
const lines = text.split('\n');
96+
const text = textBeforeInsertion + state.text + textAfterInsertion;
97+
const lines = text.split(eol);
9798

9899
const renderOptions = RenderOptions.fromEditor(this._editor).withSetWidth(false);
99100
const lineWidths = lines.map(line => {
@@ -121,15 +122,16 @@ export class InlineEditsInsertionView extends Disposable implements IInlineEdits
121122

122123
// Adjust for leading/trailing newlines
123124
const lineHeight = this._editor.getOption(EditorOption.lineHeight);
125+
const eol = this._editor.getModel()!.getEOL();
124126
let topTrim = 0;
125127
let bottomTrim = 0;
126128

127129
let i = 0;
128-
for (; i < text.length && text[i] === '\n'; i++) {
130+
for (; i < text.length && text.startsWith(eol, i); i += eol.length) {
129131
topTrim += lineHeight;
130132
}
131133

132-
for (let j = text.length - 1; j > i && text[j] === '\n'; j--) {
134+
for (let j = text.length; j > i && text.endsWith(eol, j); j -= eol.length) {
133135
bottomTrim += lineHeight;
134136
}
135137

0 commit comments

Comments
 (0)