From 7f274127ffd5291c4d9b5e0cda6d2799403f8c43 Mon Sep 17 00:00:00 2001 From: Jean Pierre Date: Sun, 13 May 2018 22:31:50 -0500 Subject: [PATCH] Fix for #49138 (#49501) * Fixes #49138 * Updated comment * Renaming variables for readability --- extensions/emmet/src/abbreviationActions.ts | 45 +++++++++++---------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/extensions/emmet/src/abbreviationActions.ts b/extensions/emmet/src/abbreviationActions.ts index 0a29823304f..e75860293ad 100644 --- a/extensions/emmet/src/abbreviationActions.ts +++ b/extensions/emmet/src/abbreviationActions.ts @@ -61,24 +61,12 @@ function doWrapping(individualLines: boolean, args: any) { return; } - const selections = editor.selections.sort((a: vscode.Selection, b: vscode.Selection) => { return a.start.compareTo(b.start); }); - let disableLivePreview = false; - for (let i = 1; i < selections.length; i++) { - if (selections[i].start.line === selections[i - 1].start.line - || selections[i].end.line === selections[i - 1].start.line - || selections[i].start.line === selections[i - 1].end.line - || selections[i].end.line === selections[i - 1].end.line) { - disableLivePreview = true; // Disable Live Preview due to https://github.com/Microsoft/vscode/issues/49138 - break; - } - } - let inPreview = false; let currentValue = ''; const helper = getEmmetHelper(); // Fetch general information for the succesive expansions. i.e. the ranges to replace and its contents - let rangesToReplace: PreviewRangesWithContent[] = selections.map(selection => { + let rangesToReplace: PreviewRangesWithContent[] = editor.selections.sort((a: vscode.Selection, b: vscode.Selection) => { return a.start.compareTo(b.start); }).map(selection => { let rangeToReplace: vscode.Range = selection.isReversed ? new vscode.Range(selection.active, selection.anchor) : selection; if (!rangeToReplace.isSingleLine && rangeToReplace.end.character === 0) { const previousLine = rangeToReplace.end.line - 1; @@ -128,6 +116,8 @@ function doWrapping(individualLines: boolean, args: any) { } function applyPreview(expandAbbrList: ExpandAbbreviationInput[]): Thenable { + let lastOldPreviewRange = new vscode.Range(0, 0, 0, 0); + let lastNewPreviewRange = new vscode.Range(0, 0, 0, 0); let totalLinesInserted = 0; return editor.edit(builder => { @@ -153,13 +143,29 @@ function doWrapping(individualLines: boolean, args: any) { const oldPreviewLines = oldPreviewRange.end.line - oldPreviewRange.start.line + 1; const newLinesInserted = expandedTextLines.length - oldPreviewLines; - let lastLineEnd = expandedTextLines[expandedTextLines.length - 1].length; - if (expandedTextLines.length === 1) { - // If the expandedText is single line, add the length of preceeding whitespace as it will not be included in line length. - lastLineEnd += oldPreviewRange.start.character; + let newPreviewLineStart = oldPreviewRange.start.line + totalLinesInserted; + let newPreviewStart = oldPreviewRange.start.character; + const newPreviewLineEnd = oldPreviewRange.end.line + totalLinesInserted + newLinesInserted; + let newPreviewEnd = expandedTextLines[expandedTextLines.length - 1].length; + if (i > 0 && newPreviewLineEnd === lastNewPreviewRange.end.line) { + // If newPreviewLineEnd is equal to the previous expandedText lineEnd, + // set newPreviewStart to the length of the previous expandedText in that line + // plus the number of characters between both selections. + newPreviewStart = lastNewPreviewRange.end.character + (oldPreviewRange.start.character - lastOldPreviewRange.end.character); + newPreviewEnd += newPreviewStart; + } + else if (i > 0 && newPreviewLineStart === lastNewPreviewRange.end.line) { + // Same as above but expandedTextLines.length > 1 so newPreviewEnd keeps its value. + newPreviewStart = lastNewPreviewRange.end.character + (oldPreviewRange.start.character - lastOldPreviewRange.end.character); + } + else if (expandedTextLines.length === 1) { + // If the expandedText is single line, add the length of preceeding text as it will not be included in line length. + newPreviewEnd += oldPreviewRange.start.character; } - rangesToReplace[i].previewRange = new vscode.Range(oldPreviewRange.start.line + totalLinesInserted, oldPreviewRange.start.character, oldPreviewRange.end.line + totalLinesInserted + newLinesInserted, lastLineEnd); + lastOldPreviewRange = rangesToReplace[i].previewRange; + rangesToReplace[i].previewRange = lastNewPreviewRange = new vscode.Range(newPreviewLineStart, newPreviewStart, newPreviewLineEnd, newPreviewEnd); + totalLinesInserted += newLinesInserted; } }, { undoStopBefore: false, undoStopAfter: false }); @@ -203,9 +209,6 @@ function doWrapping(individualLines: boolean, args: any) { } function inputChanged(value: string): string { - if (disableLivePreview) { - return ''; - } if (value !== currentValue) { currentValue = value; makeChanges(value, false).then((out) => { -- GitLab