Skip to content

Commit fe91f31

Browse files
author
Armando Aguirre
committed
Fixed uncomment bug
1 parent 381dd84 commit fe91f31

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/services/services.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2081,9 +2081,9 @@ namespace ts {
20812081

20822082
pos = commentRange.end + 1;
20832083
} else { // If it's not in a comment range, then we need to comment the uncommented portions.
2084-
isCommenting = true;
2084+
let newPos = text.substring(pos, textRange.end).search(`(${openMultilineRegex})|(${closeMultilineRegex})`);
20852085

2086-
const newPos = text.substring(pos, textRange.end).search(`(${openMultilineRegex})|(${closeMultilineRegex})`);
2086+
isCommenting = isCommenting || !isTextWhiteSpaceLike(text, pos, newPos === -1 ? textRange.end : pos + newPos);
20872087
pos = newPos === -1 ? textRange.end + 1 : pos + newPos + closeMultiline.length;
20882088
}
20892089
}
@@ -2130,20 +2130,20 @@ namespace ts {
21302130
}
21312131

21322132
// Insert open comment if the last position is not a comment already.
2133-
const lastPos = positions[positions.length - 1];
2134-
if (text.substr(lastPos - closeMultiline.length, closeMultiline.length) !== closeMultiline) {
2133+
if (textChanges.length % 2 !== 0) {
21352134
textChanges.push({
21362135
newText: closeMultiline,
21372136
span: {
21382137
length: 0,
2139-
start: lastPos
2138+
start: positions[positions.length - 1]
21402139
}
21412140
});
21422141
}
21432142
} else {
21442143
// If is not commenting then remove all comments found.
21452144
for (let i = 0; i < positions.length; i++) {
2146-
const offset = text.substr(positions[i] - closeMultiline.length, closeMultiline.length) === closeMultiline ? closeMultiline.length : 0;
2145+
const from = positions[i] - closeMultiline.length > 0 ? positions[i] - closeMultiline.length : 0;
2146+
const offset = text.substr(from, closeMultiline.length) === closeMultiline ? closeMultiline.length : 0;
21472147
textChanges.push({
21482148
newText: "",
21492149
span: {

src/services/utilities.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -877,14 +877,14 @@ namespace ts {
877877
// specially by `getSymbolAtLocation`.
878878
if (isModifier(node) && (forRename || node.kind !== SyntaxKind.DefaultKeyword) ? contains(parent.modifiers, node) :
879879
node.kind === SyntaxKind.ClassKeyword ? isClassDeclaration(parent) || isClassExpression(node) :
880-
node.kind === SyntaxKind.FunctionKeyword ? isFunctionDeclaration(parent) || isFunctionExpression(node) :
881-
node.kind === SyntaxKind.InterfaceKeyword ? isInterfaceDeclaration(parent) :
882-
node.kind === SyntaxKind.EnumKeyword ? isEnumDeclaration(parent) :
883-
node.kind === SyntaxKind.TypeKeyword ? isTypeAliasDeclaration(parent) :
884-
node.kind === SyntaxKind.NamespaceKeyword || node.kind === SyntaxKind.ModuleKeyword ? isModuleDeclaration(parent) :
885-
node.kind === SyntaxKind.ImportKeyword ? isImportEqualsDeclaration(parent) :
886-
node.kind === SyntaxKind.GetKeyword ? isGetAccessorDeclaration(parent) :
887-
node.kind === SyntaxKind.SetKeyword && isSetAccessorDeclaration(parent)) {
880+
node.kind === SyntaxKind.FunctionKeyword ? isFunctionDeclaration(parent) || isFunctionExpression(node) :
881+
node.kind === SyntaxKind.InterfaceKeyword ? isInterfaceDeclaration(parent) :
882+
node.kind === SyntaxKind.EnumKeyword ? isEnumDeclaration(parent) :
883+
node.kind === SyntaxKind.TypeKeyword ? isTypeAliasDeclaration(parent) :
884+
node.kind === SyntaxKind.NamespaceKeyword || node.kind === SyntaxKind.ModuleKeyword ? isModuleDeclaration(parent) :
885+
node.kind === SyntaxKind.ImportKeyword ? isImportEqualsDeclaration(parent) :
886+
node.kind === SyntaxKind.GetKeyword ? isGetAccessorDeclaration(parent) :
887+
node.kind === SyntaxKind.SetKeyword && isSetAccessorDeclaration(parent)) {
888888
const location = getAdjustedLocationForDeclaration(parent, forRename);
889889
if (location) {
890890
return location;
@@ -1947,6 +1947,16 @@ namespace ts {
19471947
return undefined;
19481948
}
19491949

1950+
export function isTextWhiteSpaceLike(text: string, startPos: number, endPos: number): boolean {
1951+
for (let i = startPos; i < endPos; i++) {
1952+
if (!isWhiteSpaceLike(text.charCodeAt(i))) {
1953+
return false;
1954+
}
1955+
}
1956+
1957+
return true;
1958+
}
1959+
19501960
// #endregion
19511961

19521962
// Display-part writer helpers
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// If the range only contains comments, uncomment all.
2+
3+
//// /*let var[|1 = 1;*/
4+
//// /*let var2 = 2;*/
5+
////
6+
//// /*let var3 |]= 3;*/
7+
8+
verify.toggleMultilineComment(
9+
`let var1 = 1;
10+
let var2 = 2;
11+
12+
let var3 = 3;`);

0 commit comments

Comments
 (0)