diff --git a/src/services/services.ts b/src/services/services.ts index b6003b4826..cf764be8fb 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2054,7 +2054,7 @@ namespace ts { let isCommenting = insertComment || false; const positions = [] as number[] as SortedArray; - let pos = textRange.pos; + let { pos } = textRange; const isJsx = isInsideJsx !== undefined ? isInsideJsx : isInsideJsxElement(sourceFile, pos); const openMultiline = isJsx ? "{/*" : "/*"; @@ -2170,8 +2170,16 @@ namespace ts { function uncommentSelection(fileName: string, textRange: TextRange): TextChange[] { const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); const textChanges: TextChange[] = []; + const { pos } = textRange; + let { end } = textRange; - for (let i = textRange.pos; i <= textRange.end; i++) { + // If cursor is not a selection we need to increase the end position + // to include the start of the comment. + if (pos === end) { + end += isInsideJsxElement(sourceFile, pos) ? 2 : 1; + } + + for (let i = pos; i <= end; i++) { const commentRange = isInComment(sourceFile, i); if (commentRange) { switch (commentRange.kind) { diff --git a/tests/cases/fourslash/toggleLineComment10.ts b/tests/cases/fourslash/toggleLineComment10.ts index 5acf9c4a27..0f6c3d2719 100644 --- a/tests/cases/fourslash/toggleLineComment10.ts +++ b/tests/cases/fourslash/toggleLineComment10.ts @@ -1,4 +1,4 @@ -// Close and open multiline comments if the line already contains more. +// Close and open multiline comments if the line already contains more comments. //@Filename: file.tsx //// const a =
diff --git a/tests/cases/fourslash/toggleLineComment4.ts b/tests/cases/fourslash/toggleLineComment4.ts index 1d162ca7be..5e4beb6070 100644 --- a/tests/cases/fourslash/toggleLineComment4.ts +++ b/tests/cases/fourslash/toggleLineComment4.ts @@ -1,4 +1,4 @@ -// If at least one line is uncomment then comment all lines again. +// If at least one line is not commented then comment all lines again. //// //const a[| = 1; //// const b = 2 diff --git a/tests/cases/fourslash/toggleLineComment9.ts b/tests/cases/fourslash/toggleLineComment9.ts index 562e77bf4d..1fbaea7ea3 100644 --- a/tests/cases/fourslash/toggleLineComment9.ts +++ b/tests/cases/fourslash/toggleLineComment9.ts @@ -1,4 +1,4 @@ -// If at least one line is uncomment then comment all lines again. +// If at least one line is not commented then comment all lines again. // TODO: Not sure about this one. The default behavior for line comment is to add en extra // layer of comments (see toggleLineComment4 test). For jsx this doesn't work right as it's actually // multiline comment. Figure out what to do. diff --git a/tests/cases/fourslash/uncommentSelection1.ts b/tests/cases/fourslash/uncommentSelection1.ts index 42c567d3ca..77066e4915 100644 --- a/tests/cases/fourslash/uncommentSelection1.ts +++ b/tests/cases/fourslash/uncommentSelection1.ts @@ -13,6 +13,10 @@ //// let var8/* = 1; //// let var9 [||]= 2; //// let var10 */= 3; +//// +//// let var11[||]/* = 1; +//// let var12 = 2; +//// let var13 */= 3; verify.uncommentSelection( `let var1 = 1; @@ -27,4 +31,8 @@ let var7 = 7; let var8 = 1; let var9 = 2; -let var10 = 3;`); \ No newline at end of file +let var10 = 3; + +let var11 = 1; +let var12 = 2; +let var13 = 3;`); \ No newline at end of file diff --git a/tests/cases/fourslash/uncommentSelection2.ts b/tests/cases/fourslash/uncommentSelection2.ts index 55a84555ca..745000d9c4 100644 --- a/tests/cases/fourslash/uncommentSelection2.ts +++ b/tests/cases/fourslash/uncommentSelection2.ts @@ -11,6 +11,10 @@ //// SomeText //// {/*
|]*/} //// ; +//// +//// const c = +//// [||]{/**/} +//// ; verify.uncommentSelection( @@ -23,4 +27,8 @@ const b =
SomeText
-
;`); \ No newline at end of file +; + +const c = + +;`); \ No newline at end of file