Skip to content

Commit 66212c6

Browse files
committed
toggle comment across css nodes microsoft#27629
1 parent 0da90fc commit 66212c6

1 file changed

Lines changed: 18 additions & 32 deletions

File tree

extensions/emmet/src/toggleComment.ts

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -108,45 +108,31 @@ function toggleCommentStylesheet(document: vscode.TextDocument, selection: vscod
108108
const selectionStart = document.offsetAt(selection.isReversed ? selection.active : selection.anchor);
109109
const selectionEnd = document.offsetAt(selection.isReversed ? selection.anchor : selection.active);
110110

111-
// If current node is commented, then uncomment and return
112-
let rangesToUnComment = getRangesToUnCommentStylesheet(rootNode, selectionStart, selectionEnd, document, true);
113-
if (rangesToUnComment.length > 0) {
114-
return [rangesToUnComment, null, null];
115-
}
116-
117-
// Find the node that needs to be commented
118-
let nodeToComment = getNode(rootNode, selectionStart, true);
119-
if (!nodeToComment) {
120-
return [[], null, null];
121-
}
122-
123-
// Uncomment children of current node and then comment the node
124-
rangesToUnComment = getRangesToUnCommentStylesheet(rootNode, nodeToComment.start, nodeToComment.end, document, false);
125-
let positionForCommentStart = document.positionAt(nodeToComment.start);
126-
let positionForCommentEnd = document.positionAt(nodeToComment.end);
127-
128-
return [rangesToUnComment, positionForCommentStart, positionForCommentEnd];
129-
}
111+
let startNode = getNode(rootNode, selectionStart, true);
112+
let endNode = getNode(rootNode, selectionEnd, true);
113+
let rangesToUnComment: vscode.Range[] = [];
130114

131-
function getRangesToUnCommentStylesheet(rootNode: Node, selectionStart: number, selectionEnd: number, document: vscode.TextDocument, selectionInsideComment: boolean): vscode.Range[] {
132-
if (!rootNode.comments || rootNode.comments.length === 0) {
133-
return [];
134-
}
115+
let isFirstNodeCommented = false;
135116

136-
let rangesToUnComment = [];
117+
// Uncomment the comments that intersect with the selection.
137118
rootNode.comments.forEach(comment => {
138-
let foundComment = false;
139-
if (selectionInsideComment) {
140-
foundComment = comment.start <= selectionStart && comment.end >= selectionEnd;
141-
} else {
142-
foundComment = selectionStart <= comment.start && selectionEnd >= comment.end;
119+
let commentStart = document.positionAt(comment.start);
120+
let commentEnd = document.positionAt(comment.end);
121+
122+
if (!isFirstNodeCommented) {
123+
isFirstNodeCommented = (comment.start <= selectionStart && comment.end >= selectionEnd);
143124
}
144125

145-
if (foundComment) {
126+
if (selection.contains(commentStart) || selection.contains(commentEnd) || (comment.start <= selectionStart && comment.end >= selectionEnd)) {
146127
rangesToUnComment.push(new vscode.Range(document.positionAt(comment.start), document.positionAt(comment.start + startCommentStylesheet.length)));
147128
rangesToUnComment.push(new vscode.Range(document.positionAt(comment.end), document.positionAt(comment.end - endCommentStylesheet.length)));
148129
}
149130
});
150131

151-
return rangesToUnComment;
152-
}
132+
let positionForCommentStart = isFirstNodeCommented ? null : document.positionAt(startNode.start);
133+
let positionForCommentEnd = isFirstNodeCommented ? null : document.positionAt(endNode.end);
134+
135+
return [rangesToUnComment, positionForCommentStart, positionForCommentEnd];
136+
137+
138+
}

0 commit comments

Comments
 (0)