Skip to content

Commit 79ed041

Browse files
authored
Fix Format Selection on JSDoc comments (#42411)
1 parent f1583f0 commit 79ed041

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/services/formatting/formatting.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ namespace ts.formatting {
390390
sourceFile));
391391
}
392392

393-
function formatSpanWorker(originalRange: TextRange,
393+
function formatSpanWorker(
394+
originalRange: TextRange,
394395
enclosingNode: Node,
395396
initialIndentation: number,
396397
delta: number,
@@ -424,16 +425,20 @@ namespace ts.formatting {
424425
}
425426

426427
if (!formattingScanner.isOnToken()) {
428+
const indentation = SmartIndenter.nodeWillIndentChild(options, enclosingNode, /*child*/ undefined, sourceFile, /*indentByDefault*/ false)
429+
? initialIndentation + options.indentSize!
430+
: initialIndentation;
427431
const leadingTrivia = formattingScanner.getCurrentLeadingTrivia();
428432
if (leadingTrivia) {
429-
indentTriviaItems(leadingTrivia, initialIndentation, /*indentNextTokenOrTrivia*/ false,
433+
indentTriviaItems(leadingTrivia, indentation, /*indentNextTokenOrTrivia*/ false,
430434
item => processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!));
431-
if (options.trimTrailingWhitespace !== false) {
432-
trimTrailingWhitespacesForRemainingRange();
433-
}
434435
}
435436
}
436437

438+
if (options.trimTrailingWhitespace !== false) {
439+
trimTrailingWhitespacesForRemainingRange();
440+
}
441+
437442
return edits;
438443

439444
// local functions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// {
4+
//// /*1*//**
5+
//// * Some doc comment
6+
//// *//*2*/
7+
//// const a = 1;
8+
//// }
9+
////
10+
//// while (true) {
11+
//// /*3*//**
12+
//// * Some doc comment
13+
//// *//*4*/
14+
//// }
15+
16+
format.selection("1", "2");
17+
verify.currentFileContentIs(
18+
`{
19+
/**
20+
* Some doc comment
21+
*/
22+
const a = 1;
23+
}
24+
25+
while (true) {
26+
/**
27+
* Some doc comment
28+
*/
29+
}`);
30+
31+
format.selection("3", "4");
32+
verify.currentFileContentIs(
33+
`{
34+
/**
35+
* Some doc comment
36+
*/
37+
const a = 1;
38+
}
39+
40+
while (true) {
41+
/**
42+
* Some doc comment
43+
*/
44+
}`);

0 commit comments

Comments
 (0)