mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-11 01:34:55 -06:00
Fixed trimming comments on the remaining range (#45807)
* Fixed trimming comments on the remaining range * Added test
This commit is contained in:
parent
3c27c3a5ca
commit
8346143450
@ -432,13 +432,12 @@ namespace ts.formatting {
|
||||
if (leadingTrivia) {
|
||||
indentTriviaItems(leadingTrivia, indentation, /*indentNextTokenOrTrivia*/ false,
|
||||
item => processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!));
|
||||
if (options.trimTrailingWhitespace !== false) {
|
||||
trimTrailingWhitespacesForRemainingRange(leadingTrivia);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options.trimTrailingWhitespace !== false) {
|
||||
trimTrailingWhitespacesForRemainingRange();
|
||||
}
|
||||
|
||||
return edits;
|
||||
|
||||
// local functions
|
||||
@ -1142,13 +1141,29 @@ namespace ts.formatting {
|
||||
}
|
||||
|
||||
/**
|
||||
* Trimming will be done for lines after the previous range
|
||||
* Trimming will be done for lines after the previous range.
|
||||
* Exclude comments as they had been previously processed.
|
||||
*/
|
||||
function trimTrailingWhitespacesForRemainingRange() {
|
||||
const startPosition = previousRange ? previousRange.end : originalRange.pos;
|
||||
function trimTrailingWhitespacesForRemainingRange(trivias: TextRangeWithKind<SyntaxKind>[]) {
|
||||
let startPos = previousRange ? previousRange.end : originalRange.pos;
|
||||
for (const trivia of trivias) {
|
||||
if (isComment(trivia.kind)) {
|
||||
if (startPos < trivia.pos) {
|
||||
trimTrailingWitespacesForPositions(startPos, trivia.pos - 1, previousRange);
|
||||
}
|
||||
|
||||
const startLine = sourceFile.getLineAndCharacterOfPosition(startPosition).line;
|
||||
const endLine = sourceFile.getLineAndCharacterOfPosition(originalRange.end).line;
|
||||
startPos = trivia.end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (startPos < originalRange.end) {
|
||||
trimTrailingWitespacesForPositions(startPos, originalRange.end, previousRange);
|
||||
}
|
||||
}
|
||||
|
||||
function trimTrailingWitespacesForPositions(startPos: number, endPos: number, previousRange: TextRangeWithKind) {
|
||||
const startLine = sourceFile.getLineAndCharacterOfPosition(startPos).line;
|
||||
const endLine = sourceFile.getLineAndCharacterOfPosition(endPos).line;
|
||||
|
||||
trimTrailingWhitespacesForLines(startLine, endLine + 1, previousRange);
|
||||
}
|
||||
|
||||
13
tests/cases/fourslash/server/formatTrimRemainingRangets
Normal file
13
tests/cases/fourslash/server/formatTrimRemainingRangets
Normal file
@ -0,0 +1,13 @@
|
||||
/// <reference path="../fourslash.ts"/>
|
||||
|
||||
//// ;
|
||||
//// /*
|
||||
////
|
||||
//// */
|
||||
|
||||
format.document();
|
||||
verify.currentFileContentIs(
|
||||
`;
|
||||
/*
|
||||
|
||||
*/`);
|
||||
Loading…
x
Reference in New Issue
Block a user