Skip to content

Commit cfe354d

Browse files
committed
Toggle comment shld ignore whitespace when fetching nodes
1 parent d832738 commit cfe354d

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

extensions/emmet/src/test/toggleComment.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ suite('Tests for Toggle Comment action from Emmet (CSS)', () => {
187187
const expectedContents = `
188188
.one {
189189
/*margin: 10px;*/
190-
padding: 10px;
190+
/*padding: 10px;*/
191191
}
192192
/*.two {
193193
height: 42px;
@@ -199,6 +199,7 @@ suite('Tests for Toggle Comment action from Emmet (CSS)', () => {
199199
return withRandomFileEditor(contents, 'css', (editor, doc) => {
200200
editor.selections = [
201201
new Selection(2, 2, 2, 15), // A property completely selected
202+
new Selection(3, 0, 3, 16), // A property completely selected along with whitespace
202203
new Selection(5, 1, 8, 1), // A rule completely selected
203204
];
204205

@@ -226,7 +227,7 @@ suite('Tests for Toggle Comment action from Emmet (CSS)', () => {
226227
}*/`;
227228
return withRandomFileEditor(contents, 'css', (editor, doc) => {
228229
editor.selections = [
229-
new Selection(2, 2, 3, 16), // 2 properties completely under a single selection
230+
new Selection(2, 0, 3, 16), // 2 properties completely under a single selection along with whitespace
230231
new Selection(5, 1, 11, 2), // 2 rules completely under a single selection
231232
];
232233

extensions/emmet/src/toggleComment.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,30 @@ function toggleCommentStylesheet(document: vscode.TextDocument, selection: vscod
106106

107107
let startNode = getNode(rootNode, selectionStart, true);
108108
let endNode = getNode(rootNode, selectionEnd, true);
109-
if (sameNodes(startNode, endNode)) {
110-
selection = new vscode.Selection(startNode.start, endNode.end);
109+
if (startNode && endNode) {
110+
if (sameNodes(startNode, endNode) || sameNodes(startNode.parent, endNode.parent)) {
111+
selection = new vscode.Selection(startNode.start, endNode.end);
112+
} else {
113+
if (sameNodes(startNode, endNode.parent)) {
114+
let newStartNode = startNode.firstChild;
115+
while (newStartNode.end.isBefore(selectionStart) && newStartNode.nextSibling) {
116+
newStartNode = newStartNode.nextSibling;
117+
}
118+
startNode = newStartNode;
119+
} else if (sameNodes(startNode.parent, endNode)) {
120+
let newEndNode = endNode.children[endNode.children.length - 1];
121+
while (newEndNode.end.isAfter(selectionEnd) && newEndNode.previousSibling) {
122+
newEndNode = newEndNode.previousSibling;
123+
}
124+
endNode = newEndNode;
125+
}
126+
127+
// TODO: both are properties of different rule : have 2 comments for the 2 rules
128+
}
111129
}
112130

131+
132+
113133
let rangesToUnComment: vscode.Range[] = [];
114134

115135
let isFirstNodeCommented = false;

0 commit comments

Comments
 (0)