mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
Formatter: fix indentation of comments at end of range (#54250)
This commit is contained in:
parent
bd9992a33d
commit
da686774dc
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
tests/cases/fourslash/formatSelectionWithTrivia3.ts
Normal file
11
tests/cases/fourslash/formatSelectionWithTrivia3.ts
Normal 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}");
|
||||
12
tests/cases/fourslash/formatSelectionWithTrivia4.ts
Normal file
12
tests/cases/fourslash/formatSelectionWithTrivia4.ts
Normal 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}");
|
||||
12
tests/cases/fourslash/formatSelectionWithTrivia5.ts
Normal file
12
tests/cases/fourslash/formatSelectionWithTrivia5.ts
Normal 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}");
|
||||
10
tests/cases/fourslash/formatSelectionWithTrivia6.ts
Normal file
10
tests/cases/fourslash/formatSelectionWithTrivia6.ts
Normal 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");
|
||||
11
tests/cases/fourslash/formatSelectionWithTrivia7.ts
Normal file
11
tests/cases/fourslash/formatSelectionWithTrivia7.ts
Normal 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}");
|
||||
11
tests/cases/fourslash/formatSelectionWithTrivia8.ts
Normal file
11
tests/cases/fourslash/formatSelectionWithTrivia8.ts
Normal 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();");
|
||||
Loading…
x
Reference in New Issue
Block a user