Skip to content

Commit ff5ae35

Browse files
authored
feat(42735): add single line comments to selection ranges (microsoft#42763)
1 parent 4750369 commit ff5ae35

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

src/services/smartSelection.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ namespace ts.SmartSelectionRange {
1313
const prevNode: Node | undefined = children[i - 1];
1414
const node: Node = children[i];
1515
const nextNode: Node | undefined = children[i + 1];
16+
1617
if (getTokenPosOfNode(node, sourceFile, /*includeJsDoc*/ true) > pos) {
1718
break outer;
1819
}
1920

21+
const comment = singleOrUndefined(getTrailingCommentRanges(sourceFile.text, node.end));
22+
if (comment && comment.kind === SyntaxKind.SingleLineCommentTrivia) {
23+
pushSelectionCommentRange(comment.pos, comment.end);
24+
}
25+
2026
if (positionShouldSnapToNode(sourceFile, pos, node)) {
2127
// 1. Blocks are effectively redundant with SyntaxLists.
2228
// 2. TemplateSpans, along with the SyntaxLists containing them, are a somewhat unintuitive grouping
@@ -89,6 +95,16 @@ namespace ts.SmartSelectionRange {
8995
}
9096
}
9197
}
98+
99+
function pushSelectionCommentRange(start: number, end: number): void {
100+
pushSelectionRange(start, end);
101+
102+
let pos = start;
103+
while (sourceFile.text.charCodeAt(pos) === CharacterCodes.slash) {
104+
pos++;
105+
}
106+
pushSelectionRange(pos, end);
107+
}
92108
}
93109

94110
/**
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const a = 1; ///**/comment content
2+
3+
comment content
4+
//comment content
5+
const a = 1; //comment content
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const a = 1; //a b/**/c d
2+
3+
a bc d
4+
//a bc d
5+
const a = 1; //a bc d
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const a = 1; ///**/comment content
4+
5+
verify.baselineSmartSelection();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const a = 1; //a b/**/c d
4+
5+
verify.baselineSmartSelection();

0 commit comments

Comments
 (0)