diff --git a/src/services/smartSelection.ts b/src/services/smartSelection.ts index 3fcab16695979..6cd5c8f94d854 100644 --- a/src/services/smartSelection.ts +++ b/src/services/smartSelection.ts @@ -13,10 +13,16 @@ namespace ts.SmartSelectionRange { const prevNode: Node | undefined = children[i - 1]; const node: Node = children[i]; const nextNode: Node | undefined = children[i + 1]; + if (getTokenPosOfNode(node, sourceFile, /*includeJsDoc*/ true) > pos) { break outer; } + const comment = singleOrUndefined(getTrailingCommentRanges(sourceFile.text, node.end)); + if (comment && comment.kind === SyntaxKind.SingleLineCommentTrivia) { + pushSelectionCommentRange(comment.pos, comment.end); + } + if (positionShouldSnapToNode(sourceFile, pos, node)) { // 1. Blocks are effectively redundant with SyntaxLists. // 2. TemplateSpans, along with the SyntaxLists containing them, are a somewhat unintuitive grouping @@ -89,6 +95,16 @@ namespace ts.SmartSelectionRange { } } } + + function pushSelectionCommentRange(start: number, end: number): void { + pushSelectionRange(start, end); + + let pos = start; + while (sourceFile.text.charCodeAt(pos) === CharacterCodes.slash) { + pos++; + } + pushSelectionRange(pos, end); + } } /** diff --git a/tests/baselines/reference/smartSelection_comment1.baseline b/tests/baselines/reference/smartSelection_comment1.baseline new file mode 100644 index 0000000000000..2e541c0b2301a --- /dev/null +++ b/tests/baselines/reference/smartSelection_comment1.baseline @@ -0,0 +1,5 @@ +const a = 1; ///**/comment content + + comment content + //comment content +const a = 1; //comment content \ No newline at end of file diff --git a/tests/baselines/reference/smartSelection_comment2.baseline b/tests/baselines/reference/smartSelection_comment2.baseline new file mode 100644 index 0000000000000..ccb5286f0b564 --- /dev/null +++ b/tests/baselines/reference/smartSelection_comment2.baseline @@ -0,0 +1,5 @@ +const a = 1; //a b/**/c d + + a bc d + //a bc d +const a = 1; //a bc d \ No newline at end of file diff --git a/tests/cases/fourslash/smartSelection_comment1.ts b/tests/cases/fourslash/smartSelection_comment1.ts new file mode 100644 index 0000000000000..dbd2aa6a16379 --- /dev/null +++ b/tests/cases/fourslash/smartSelection_comment1.ts @@ -0,0 +1,5 @@ +/// + +////const a = 1; ///**/comment content + +verify.baselineSmartSelection(); diff --git a/tests/cases/fourslash/smartSelection_comment2.ts b/tests/cases/fourslash/smartSelection_comment2.ts new file mode 100644 index 0000000000000..1da1b56cf0f80 --- /dev/null +++ b/tests/cases/fourslash/smartSelection_comment2.ts @@ -0,0 +1,5 @@ +/// + +////const a = 1; //a b/**/c d + +verify.baselineSmartSelection();