diff --git a/src/services/services.ts b/src/services/services.ts index 36638f78671..983439f27c7 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2068,6 +2068,7 @@ namespace ts { const textChanges: TextChange[] = []; const { text } = sourceFile; + let hasComment = false; let isCommenting = insertComment || false; const positions = [] as number[] as SortedArray; @@ -2098,6 +2099,7 @@ namespace ts { positions.push(commentRange.end); } + hasComment = true; pos = commentRange.end + 1; } else { // If it's not in a comment range, then we need to comment the uncommented portions. @@ -2110,7 +2112,9 @@ namespace ts { } } - if (isCommenting) { + // If it didn't found a comment and isCommenting is false means is only empty space. + // We want to insert comment in this scenario. + if (isCommenting || !hasComment) { if (isInComment(sourceFile, textRange.pos)?.kind !== SyntaxKind.SingleLineCommentTrivia) { insertSorted(positions, textRange.pos, compareValues); } diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 18bdf9557fd..29c09102424 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1321,7 +1321,7 @@ namespace ts { } export function isInsideJsxElement(sourceFile: SourceFile, position: number): boolean { - function isInsideJsxElementRecursion(node: Node): boolean { + function isInsideJsxElementTraversal(node: Node): boolean { while (node) { if (node.kind >= SyntaxKind.JsxSelfClosingElement && node.kind <= SyntaxKind.JsxExpression || node.kind === SyntaxKind.JsxText @@ -1334,7 +1334,9 @@ namespace ts { node = node.parent; } else if (node.kind === SyntaxKind.JsxElement) { - return position > node.getStart(sourceFile) || isInsideJsxElementRecursion(node.parent); + if (position > node.getStart(sourceFile)) return true; + + node = node.parent; } else { return false; @@ -1344,7 +1346,7 @@ namespace ts { return false; } - return isInsideJsxElementRecursion(getTokenAtPosition(sourceFile, position)); + return isInsideJsxElementTraversal(getTokenAtPosition(sourceFile, position)); } export function findPrecedingMatchingToken(token: Node, matchingTokenKind: SyntaxKind, sourceFile: SourceFile) { @@ -2279,8 +2281,8 @@ namespace ts { // This only happens for leaf nodes - internal nodes always see their children change. const clone = isStringLiteral(node) ? setOriginalNode(factory.createStringLiteralFromNode(node), node) as Node as T : - isNumericLiteral(node) ? setOriginalNode(factory.createNumericLiteral(node.text, node.numericLiteralFlags), node) as Node as T : - factory.cloneNode(node); + isNumericLiteral(node) ? setOriginalNode(factory.createNumericLiteral(node.text, node.numericLiteralFlags), node) as Node as T : + factory.cloneNode(node); return setTextRange(clone, node); } diff --git a/tests/cases/fourslash/toggleMultilineComment1.ts b/tests/cases/fourslash/toggleMultilineComment1.ts index d32e7b28ecf..f76e9d56473 100644 --- a/tests/cases/fourslash/toggleMultilineComment1.ts +++ b/tests/cases/fourslash/toggleMultilineComment1.ts @@ -11,6 +11,8 @@ //// [|/*let var7 = 1; //// let var8 = 2; //// let var9 = 3;*/|] +//// +//// let var10[||] = 10; verify.toggleMultilineComment( `let var1/* = 1; @@ -23,4 +25,6 @@ let var6 = 3; let var7 = 1; let var8 = 2; -let var9 = 3;`); \ No newline at end of file +let var9 = 3; + +let var10/**/ = 10;`); \ No newline at end of file diff --git a/tests/cases/fourslash/toggleMultilineComment9.ts b/tests/cases/fourslash/toggleMultilineComment9.ts new file mode 100644 index 00000000000..f9761a98773 --- /dev/null +++ b/tests/cases/fourslash/toggleMultilineComment9.ts @@ -0,0 +1,30 @@ +// When there's is only whitespace, insert comment. If there is whitespace but theres a comment in bewteen, then uncomment. + +//// /*let var1[| = 1;*/ +//// |] +//// +//// [| +//// /*let var2 = 2;*/|] +//// +//// [| +//// +//// |] +//// +//// [||] +//// +//// let var3[||] = 3; + +verify.toggleMultilineComment( + `let var1 = 1; + + + +let var2 = 2; + +/* + +*/ + + /**/ + +let var3/**/ = 3;`); \ No newline at end of file diff --git a/tests/cases/fourslash/uncommentSelection1.ts b/tests/cases/fourslash/uncommentSelection1.ts index 77066e49152..2245cbc2d00 100644 --- a/tests/cases/fourslash/uncommentSelection1.ts +++ b/tests/cases/fourslash/uncommentSelection1.ts @@ -17,6 +17,8 @@ //// let var11[||]/* = 1; //// let var12 = 2; //// let var13 */= 3; +//// +//// ////let var14 [||]= 14; verify.uncommentSelection( `let var1 = 1; @@ -35,4 +37,6 @@ let var10 = 3; let var11 = 1; let var12 = 2; -let var13 = 3;`); \ No newline at end of file +let var13 = 3; + +//let var14 = 14;`); \ No newline at end of file