diff --git a/extensions/emmet/src/balance.ts b/extensions/emmet/src/balance.ts index 2d4917994aeb3..109a82b6150a3 100644 --- a/extensions/emmet/src/balance.ts +++ b/extensions/emmet/src/balance.ts @@ -7,6 +7,10 @@ import * as vscode from 'vscode'; import { HtmlNode } from 'EmmetNode'; import { getNode, parseDocument, validate } from './util'; +let balanceOutStack: Array = []; +let lastOut = false; +let lastBalancedSelections: vscode.Selection[] = []; + export function balanceOut() { balance(true); } @@ -32,8 +36,28 @@ function balance(out: boolean) { newSelections.push(range); }); - editor.selection = newSelections[0]; - editor.selections = newSelections; + if (areSameSelections(newSelections, editor.selections)) { + return; + } + + if (areSameSelections(lastBalancedSelections, editor.selections)) { + if (out) { + if (!balanceOutStack.length) { + balanceOutStack.push(editor.selections); + } + balanceOutStack.push(newSelections); + } else { + if (lastOut) { + balanceOutStack.pop(); + } + newSelections = balanceOutStack.pop() || newSelections; + } + } else { + balanceOutStack = out ? [editor.selections, newSelections] : []; + } + + lastOut = out; + lastBalancedSelections = editor.selections = newSelections; } function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection { @@ -83,3 +107,14 @@ function getRangeToBalanceIn(document: vscode.TextDocument, selection: vscode.Se } +function areSameSelections(a: vscode.Selection[], b: vscode.Selection[]): boolean { + if (a.length !== b.length) { + return false; + } + for (let i = 0; i < a.length; i++) { + if (!a[i].isEqual(b[i])) { + return false; + } + } + return true; +} \ No newline at end of file diff --git a/extensions/emmet/src/test/editPointSelectItemBalance.test.ts b/extensions/emmet/src/test/editPointSelectItemBalance.test.ts index 3393fdddc24f5..61fe36ea4eecb 100644 --- a/extensions/emmet/src/test/editPointSelectItemBalance.test.ts +++ b/extensions/emmet/src/test/editPointSelectItemBalance.test.ts @@ -211,6 +211,35 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => { }); }); + test('Emmet Balance In using the same stack as Balance out in html file', function (): any { + return withRandomFileEditor(htmlContents, 'html', (editor, doc) => { + + editor.selections = [new Selection(15, 6, 15, 10)]; + let expectedBalanceOutRanges: [number, number, number, number][] = [ + [15, 3, 15, 32], //
  • Item 2
  • + [13, 23, 16, 2], // inner contents of