From eba538ed500342338135bc843a71b606355b6134 Mon Sep 17 00:00:00 2001 From: Heldenkrieger01 <6423282+Heldenkrieger01@users.noreply.github.com> Date: Wed, 16 May 2018 22:38:26 +0200 Subject: [PATCH 1/2] Update Emmet - balance.ts --- extensions/emmet/src/balance.ts | 62 ++++++++++++++------------------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/extensions/emmet/src/balance.ts b/extensions/emmet/src/balance.ts index 2d4917994aeb3..d962f784ffa51 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 balanceStack: Array = []; +let beforeEmmetSelection: vscode.Selection[]; +let lastOut = false; + export function balanceOut() { balance(true); } @@ -25,15 +29,28 @@ function balance(out: boolean) { return; } - let getRangeFunction = out ? getRangeToBalanceOut : getRangeToBalanceIn; - let newSelections: vscode.Selection[] = []; - editor.selections.forEach(selection => { - let range = getRangeFunction(editor.document, selection, rootNode); - newSelections.push(range); - }); - - editor.selection = newSelections[0]; - editor.selections = newSelections; + if(out) { + if(balanceStack.length === 0) { + beforeEmmetSelection = editor.selections; + } + + let newSelections: vscode.Selection[] = []; + editor.selections.forEach(selection => { + let range = getRangeToBalanceOut(editor.document, selection, rootNode); + newSelections.push(range); + }); + + editor.selections = newSelections; + balanceStack.push(newSelections); + lastOut = true; + } else { + if(lastOut) { + balanceStack.pop(); + lastOut = false; + } + let oldSelections = balanceStack.pop() || beforeEmmetSelection; + editor.selections = oldSelections; + } } function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection { @@ -56,30 +73,3 @@ function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.S } return selection; } - -function getRangeToBalanceIn(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection { - let nodeToBalance = getNode(rootNode, selection.start, true); - if (!nodeToBalance) { - return selection; - } - - if (selection.start.isEqual(nodeToBalance.start) - && selection.end.isEqual(nodeToBalance.end) - && nodeToBalance.close) { - return new vscode.Selection(nodeToBalance.open.end, nodeToBalance.close.start); - } - - if (!nodeToBalance.firstChild) { - return selection; - } - - if (selection.start.isEqual(nodeToBalance.firstChild.start) - && selection.end.isEqual(nodeToBalance.firstChild.end) - && nodeToBalance.firstChild.close) { - return new vscode.Selection(nodeToBalance.firstChild.open.end, nodeToBalance.firstChild.close.start); - } - - return new vscode.Selection(nodeToBalance.firstChild.start, nodeToBalance.firstChild.end); - -} - From 299b2ca1d56374309ebf9ebf1802f0ef58c31bcd Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Tue, 29 May 2018 17:08:44 -0700 Subject: [PATCH 2/2] Cover all cases --- extensions/emmet/src/balance.ts | 85 ++++++++++++++----- .../test/editPointSelectItemBalance.test.ts | 29 +++++++ 2 files changed, 94 insertions(+), 20 deletions(-) diff --git a/extensions/emmet/src/balance.ts b/extensions/emmet/src/balance.ts index d962f784ffa51..109a82b6150a3 100644 --- a/extensions/emmet/src/balance.ts +++ b/extensions/emmet/src/balance.ts @@ -7,9 +7,9 @@ import * as vscode from 'vscode'; import { HtmlNode } from 'EmmetNode'; import { getNode, parseDocument, validate } from './util'; -let balanceStack: Array = []; -let beforeEmmetSelection: vscode.Selection[]; +let balanceOutStack: Array = []; let lastOut = false; +let lastBalancedSelections: vscode.Selection[] = []; export function balanceOut() { balance(true); @@ -29,28 +29,35 @@ function balance(out: boolean) { return; } - if(out) { - if(balanceStack.length === 0) { - beforeEmmetSelection = editor.selections; - } + let getRangeFunction = out ? getRangeToBalanceOut : getRangeToBalanceIn; + let newSelections: vscode.Selection[] = []; + editor.selections.forEach(selection => { + let range = getRangeFunction(editor.document, selection, rootNode); + newSelections.push(range); + }); - let newSelections: vscode.Selection[] = []; - editor.selections.forEach(selection => { - let range = getRangeToBalanceOut(editor.document, selection, rootNode); - newSelections.push(range); - }); + if (areSameSelections(newSelections, editor.selections)) { + return; + } - editor.selections = newSelections; - balanceStack.push(newSelections); - lastOut = true; - } else { - if(lastOut) { - balanceStack.pop(); - lastOut = false; + 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; } - let oldSelections = balanceStack.pop() || beforeEmmetSelection; - editor.selections = oldSelections; + } else { + balanceOutStack = out ? [editor.selections, newSelections] : []; } + + lastOut = out; + lastBalancedSelections = editor.selections = newSelections; } function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection { @@ -73,3 +80,41 @@ function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.S } return selection; } + +function getRangeToBalanceIn(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection { + let nodeToBalance = getNode(rootNode, selection.start, true); + if (!nodeToBalance) { + return selection; + } + + if (selection.start.isEqual(nodeToBalance.start) + && selection.end.isEqual(nodeToBalance.end) + && nodeToBalance.close) { + return new vscode.Selection(nodeToBalance.open.end, nodeToBalance.close.start); + } + + if (!nodeToBalance.firstChild) { + return selection; + } + + if (selection.start.isEqual(nodeToBalance.firstChild.start) + && selection.end.isEqual(nodeToBalance.firstChild.end) + && nodeToBalance.firstChild.close) { + return new vscode.Selection(nodeToBalance.firstChild.open.end, nodeToBalance.firstChild.close.start); + } + + return new vscode.Selection(nodeToBalance.firstChild.start, nodeToBalance.firstChild.end); + +} + +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