Skip to content

Commit da68677

Browse files
authored
Formatter: fix indentation of comments at end of range (#54250)
1 parent bd9992a commit da68677

File tree

7 files changed

+79
-7
lines changed

7 files changed

+79
-7
lines changed

src/services/formatting/formatting.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,17 +515,22 @@ function formatSpanWorker(
515515
processNode(enclosingNode, enclosingNode, startLine, undecoratedStartLine, initialIndentation, delta);
516516
}
517517

518-
if (!formattingScanner.isOnToken()) {
518+
// Leading trivia items get attached to and processed with the token that proceeds them. If the
519+
// range ends in the middle of some leading trivia, the token that proceeds them won't be in the
520+
// range and thus won't get processed. So we process those remaining trivia items here.
521+
const remainingTrivia = formattingScanner.getCurrentLeadingTrivia();
522+
if (remainingTrivia) {
519523
const indentation = SmartIndenter.nodeWillIndentChild(options, enclosingNode, /*child*/ undefined, sourceFile, /*indentByDefault*/ false)
520524
? initialIndentation + options.indentSize!
521525
: initialIndentation;
522-
const leadingTrivia = formattingScanner.getCurrentLeadingTrivia();
523-
if (leadingTrivia) {
524-
indentTriviaItems(leadingTrivia, indentation, /*indentNextTokenOrTrivia*/ false,
525-
item => processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!));
526-
if (options.trimTrailingWhitespace !== false) {
527-
trimTrailingWhitespacesForRemainingRange(leadingTrivia);
526+
indentTriviaItems(remainingTrivia, indentation, /*indentNextTokenOrTrivia*/ true,
527+
item => {
528+
processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!);
529+
insertIndentation(item.pos, indentation, /*lineAdded*/ false);
528530
}
531+
);
532+
if (options.trimTrailingWhitespace !== false) {
533+
trimTrailingWhitespacesForRemainingRange(remainingTrivia);
529534
}
530535
}
531536

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Tests comment indentation with range ending before next token (end block)
4+
5+
////if (true) {
6+
/////*begin*/// test comment
7+
/////*end*/}
8+
9+
format.selection('begin', 'end');
10+
11+
verify.currentFileContentIs("if (true) {\n // test comment\n}");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Tests comment indentation with range ending before next token (statement)
4+
5+
////if (true) {
6+
/////*begin*/// test comment
7+
/////*end*/console.log();
8+
////}
9+
10+
format.selection('begin', 'end');
11+
12+
verify.currentFileContentIs("if (true) {\n // test comment\nconsole.log();\n}");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Tests comment indentation with range ending before next token (statement w/ preceding whitespace)
4+
5+
////if (true) {
6+
/////*begin*/// test comment
7+
/////*end*/ console.log();
8+
////}
9+
10+
format.selection('begin', 'end');
11+
12+
verify.currentFileContentIs("if (true) {\n // test comment\n console.log();\n}");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Tests comment indentation with range ending before next token (end of file)
4+
5+
/////*begin*/ // test comment
6+
/////*end*/
7+
8+
format.selection('begin', 'end');
9+
10+
verify.currentFileContentIs("// test comment\n");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Tests comment indentation with range ending before next token (same line)
4+
5+
////if (true) {
6+
/////*begin*/// test comment/*end*/
7+
////}
8+
9+
format.selection('begin', 'end');
10+
11+
verify.currentFileContentIs("if (true) {\n // test comment\n}");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Same as formatSelectionWithTrivia2, but range is immediately proceeded by a token
4+
5+
/////*begin*/;
6+
////
7+
/////*end*/console.log();
8+
9+
format.selection('begin', 'end');
10+
11+
verify.currentFileContentIs(";\n\nconsole.log();");

0 commit comments

Comments
 (0)