Skip to content

Commit 30bcdff

Browse files
Heldenkrieger01ramya-rao-a
authored andcommitted
Fix microsoft#49777 - Emmet balance In after balance out should go back to initial selection and not first child (microsoft#49996)
* Update Emmet - balance.ts * Cover all cases
1 parent 0c64d0d commit 30bcdff

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

extensions/emmet/src/balance.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import * as vscode from 'vscode';
77
import { HtmlNode } from 'EmmetNode';
88
import { getNode, parseDocument, validate } from './util';
99

10+
let balanceOutStack: Array<vscode.Selection[]> = [];
11+
let lastOut = false;
12+
let lastBalancedSelections: vscode.Selection[] = [];
13+
1014
export function balanceOut() {
1115
balance(true);
1216
}
@@ -32,8 +36,28 @@ function balance(out: boolean) {
3236
newSelections.push(range);
3337
});
3438

35-
editor.selection = newSelections[0];
36-
editor.selections = newSelections;
39+
if (areSameSelections(newSelections, editor.selections)) {
40+
return;
41+
}
42+
43+
if (areSameSelections(lastBalancedSelections, editor.selections)) {
44+
if (out) {
45+
if (!balanceOutStack.length) {
46+
balanceOutStack.push(editor.selections);
47+
}
48+
balanceOutStack.push(newSelections);
49+
} else {
50+
if (lastOut) {
51+
balanceOutStack.pop();
52+
}
53+
newSelections = balanceOutStack.pop() || newSelections;
54+
}
55+
} else {
56+
balanceOutStack = out ? [editor.selections, newSelections] : [];
57+
}
58+
59+
lastOut = out;
60+
lastBalancedSelections = editor.selections = newSelections;
3761
}
3862

3963
function getRangeToBalanceOut(document: vscode.TextDocument, selection: vscode.Selection, rootNode: HtmlNode): vscode.Selection {
@@ -83,3 +107,14 @@ function getRangeToBalanceIn(document: vscode.TextDocument, selection: vscode.Se
83107

84108
}
85109

110+
function areSameSelections(a: vscode.Selection[], b: vscode.Selection[]): boolean {
111+
if (a.length !== b.length) {
112+
return false;
113+
}
114+
for (let i = 0; i < a.length; i++) {
115+
if (!a[i].isEqual(b[i])) {
116+
return false;
117+
}
118+
}
119+
return true;
120+
}

extensions/emmet/src/test/editPointSelectItemBalance.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,35 @@ suite('Tests for Next/Previous Select/Edit point and Balance actions', () => {
211211
});
212212
});
213213

214+
test('Emmet Balance In using the same stack as Balance out in html file', function (): any {
215+
return withRandomFileEditor(htmlContents, 'html', (editor, doc) => {
216+
217+
editor.selections = [new Selection(15, 6, 15, 10)];
218+
let expectedBalanceOutRanges: [number, number, number, number][] = [
219+
[15, 3, 15, 32], // <li class="item1">Item 2</li>
220+
[13, 23, 16, 2], // inner contents of <ul class="nav main">
221+
[13, 2, 16, 7], // outer contents of <ul class="nav main">
222+
[12, 21, 17, 1], // inner contents of <div class="header">
223+
[12, 1, 17, 7], // outer contents of <div class="header">
224+
[8, 6, 18, 0], // inner contents of <body>
225+
[8, 0, 18, 7], // outer contents of <body>
226+
[2, 16, 19, 0], // inner contents of <html>
227+
[2, 0, 19, 7], // outer contents of <html>
228+
];
229+
expectedBalanceOutRanges.forEach(([linestart, colstart, lineend, colend]) => {
230+
balanceOut();
231+
testSelection(editor.selection, colstart, linestart, colend, lineend);
232+
});
233+
234+
expectedBalanceOutRanges.reverse().forEach(([linestart, colstart, lineend, colend]) => {
235+
testSelection(editor.selection, colstart, linestart, colend, lineend);
236+
balanceIn();
237+
});
238+
239+
return Promise.resolve();
240+
});
241+
});
242+
214243
});
215244

216245
function testSelection(selection: Selection, startChar: number, startline: number, endChar?: number, endLine?: number) {

0 commit comments

Comments
 (0)