From fe91f317de7adf6dd0af648505c5e06116e58214 Mon Sep 17 00:00:00 2001 From: Armando Aguirre Date: Thu, 27 Feb 2020 17:53:31 -0800 Subject: [PATCH] Fixed uncomment bug --- src/services/services.ts | 12 ++++----- src/services/utilities.ts | 26 +++++++++++++------ .../fourslash/toggleMultilineComment8.ts | 12 +++++++++ 3 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 tests/cases/fourslash/toggleMultilineComment8.ts diff --git a/src/services/services.ts b/src/services/services.ts index 1e5f8f5043..4e655b8775 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2081,9 +2081,9 @@ namespace ts { pos = commentRange.end + 1; } else { // If it's not in a comment range, then we need to comment the uncommented portions. - isCommenting = true; + let newPos = text.substring(pos, textRange.end).search(`(${openMultilineRegex})|(${closeMultilineRegex})`); - const newPos = text.substring(pos, textRange.end).search(`(${openMultilineRegex})|(${closeMultilineRegex})`); + isCommenting = isCommenting || !isTextWhiteSpaceLike(text, pos, newPos === -1 ? textRange.end : pos + newPos); pos = newPos === -1 ? textRange.end + 1 : pos + newPos + closeMultiline.length; } } @@ -2130,20 +2130,20 @@ namespace ts { } // Insert open comment if the last position is not a comment already. - const lastPos = positions[positions.length - 1]; - if (text.substr(lastPos - closeMultiline.length, closeMultiline.length) !== closeMultiline) { + if (textChanges.length % 2 !== 0) { textChanges.push({ newText: closeMultiline, span: { length: 0, - start: lastPos + start: positions[positions.length - 1] } }); } } else { // If is not commenting then remove all comments found. for (let i = 0; i < positions.length; i++) { - const offset = text.substr(positions[i] - closeMultiline.length, closeMultiline.length) === closeMultiline ? closeMultiline.length : 0; + const from = positions[i] - closeMultiline.length > 0 ? positions[i] - closeMultiline.length : 0; + const offset = text.substr(from, closeMultiline.length) === closeMultiline ? closeMultiline.length : 0; textChanges.push({ newText: "", span: { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index d692099186..e7642cb80f 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -877,14 +877,14 @@ namespace ts { // specially by `getSymbolAtLocation`. if (isModifier(node) && (forRename || node.kind !== SyntaxKind.DefaultKeyword) ? contains(parent.modifiers, node) : node.kind === SyntaxKind.ClassKeyword ? isClassDeclaration(parent) || isClassExpression(node) : - node.kind === SyntaxKind.FunctionKeyword ? isFunctionDeclaration(parent) || isFunctionExpression(node) : - node.kind === SyntaxKind.InterfaceKeyword ? isInterfaceDeclaration(parent) : - node.kind === SyntaxKind.EnumKeyword ? isEnumDeclaration(parent) : - node.kind === SyntaxKind.TypeKeyword ? isTypeAliasDeclaration(parent) : - node.kind === SyntaxKind.NamespaceKeyword || node.kind === SyntaxKind.ModuleKeyword ? isModuleDeclaration(parent) : - node.kind === SyntaxKind.ImportKeyword ? isImportEqualsDeclaration(parent) : - node.kind === SyntaxKind.GetKeyword ? isGetAccessorDeclaration(parent) : - node.kind === SyntaxKind.SetKeyword && isSetAccessorDeclaration(parent)) { + node.kind === SyntaxKind.FunctionKeyword ? isFunctionDeclaration(parent) || isFunctionExpression(node) : + node.kind === SyntaxKind.InterfaceKeyword ? isInterfaceDeclaration(parent) : + node.kind === SyntaxKind.EnumKeyword ? isEnumDeclaration(parent) : + node.kind === SyntaxKind.TypeKeyword ? isTypeAliasDeclaration(parent) : + node.kind === SyntaxKind.NamespaceKeyword || node.kind === SyntaxKind.ModuleKeyword ? isModuleDeclaration(parent) : + node.kind === SyntaxKind.ImportKeyword ? isImportEqualsDeclaration(parent) : + node.kind === SyntaxKind.GetKeyword ? isGetAccessorDeclaration(parent) : + node.kind === SyntaxKind.SetKeyword && isSetAccessorDeclaration(parent)) { const location = getAdjustedLocationForDeclaration(parent, forRename); if (location) { return location; @@ -1947,6 +1947,16 @@ namespace ts { return undefined; } + export function isTextWhiteSpaceLike(text: string, startPos: number, endPos: number): boolean { + for (let i = startPos; i < endPos; i++) { + if (!isWhiteSpaceLike(text.charCodeAt(i))) { + return false; + } + } + + return true; + } + // #endregion // Display-part writer helpers diff --git a/tests/cases/fourslash/toggleMultilineComment8.ts b/tests/cases/fourslash/toggleMultilineComment8.ts new file mode 100644 index 0000000000..724c597dd0 --- /dev/null +++ b/tests/cases/fourslash/toggleMultilineComment8.ts @@ -0,0 +1,12 @@ +// If the range only contains comments, uncomment all. + +//// /*let var[|1 = 1;*/ +//// /*let var2 = 2;*/ +//// +//// /*let var3 |]= 3;*/ + +verify.toggleMultilineComment( + `let var1 = 1; +let var2 = 2; + +let var3 = 3;`); \ No newline at end of file