Formatter: fix indentation of comments at end of range (#54250)

This commit is contained in:
Russell Davis 2023-05-25 13:13:53 -07:00 committed by GitHub
parent bd9992a33d
commit da686774dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 7 deletions

View File

@ -515,17 +515,22 @@ function formatSpanWorker(
processNode(enclosingNode, enclosingNode, startLine, undecoratedStartLine, initialIndentation, delta);
}
if (!formattingScanner.isOnToken()) {
// Leading trivia items get attached to and processed with the token that proceeds them. If the
// range ends in the middle of some leading trivia, the token that proceeds them won't be in the
// range and thus won't get processed. So we process those remaining trivia items here.
const remainingTrivia = formattingScanner.getCurrentLeadingTrivia();
if (remainingTrivia) {
const indentation = SmartIndenter.nodeWillIndentChild(options, enclosingNode, /*child*/ undefined, sourceFile, /*indentByDefault*/ false)
? initialIndentation + options.indentSize!
: initialIndentation;
const leadingTrivia = formattingScanner.getCurrentLeadingTrivia();
if (leadingTrivia) {
indentTriviaItems(leadingTrivia, indentation, /*indentNextTokenOrTrivia*/ false,
item => processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!));
if (options.trimTrailingWhitespace !== false) {
trimTrailingWhitespacesForRemainingRange(leadingTrivia);
indentTriviaItems(remainingTrivia, indentation, /*indentNextTokenOrTrivia*/ true,
item => {
processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!);
insertIndentation(item.pos, indentation, /*lineAdded*/ false);
}
);
if (options.trimTrailingWhitespace !== false) {
trimTrailingWhitespacesForRemainingRange(remainingTrivia);
}
}

View File

@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />
// Tests comment indentation with range ending before next token (end block)
////if (true) {
/////*begin*/// test comment
/////*end*/}
format.selection('begin', 'end');
verify.currentFileContentIs("if (true) {\n // test comment\n}");

View File

@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />
// Tests comment indentation with range ending before next token (statement)
////if (true) {
/////*begin*/// test comment
/////*end*/console.log();
////}
format.selection('begin', 'end');
verify.currentFileContentIs("if (true) {\n // test comment\nconsole.log();\n}");

View File

@ -0,0 +1,12 @@
/// <reference path="fourslash.ts" />
// Tests comment indentation with range ending before next token (statement w/ preceding whitespace)
////if (true) {
/////*begin*/// test comment
/////*end*/ console.log();
////}
format.selection('begin', 'end');
verify.currentFileContentIs("if (true) {\n // test comment\n console.log();\n}");

View File

@ -0,0 +1,10 @@
/// <reference path="fourslash.ts" />
// Tests comment indentation with range ending before next token (end of file)
/////*begin*/ // test comment
/////*end*/
format.selection('begin', 'end');
verify.currentFileContentIs("// test comment\n");

View File

@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />
// Tests comment indentation with range ending before next token (same line)
////if (true) {
/////*begin*/// test comment/*end*/
////}
format.selection('begin', 'end');
verify.currentFileContentIs("if (true) {\n // test comment\n}");

View File

@ -0,0 +1,11 @@
/// <reference path="fourslash.ts" />
// Same as formatSelectionWithTrivia2, but range is immediately proceeded by a token
/////*begin*/;
////
/////*end*/console.log();
format.selection('begin', 'end');
verify.currentFileContentIs(";\n\nconsole.log();");