Skip to content

Commit 59f5ce4

Browse files
committed
fix comment indentation
1 parent dc86736 commit 59f5ce4

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

src/services/formatting/formatting.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace ts.formatting {
3232
*/
3333
interface DynamicIndentation {
3434
getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind): number;
35-
getIndentationForComment(owningToken: SyntaxKind): number;
35+
getIndentationForComment(owningToken: SyntaxKind, tokenIndentation: number): number;
3636
/**
3737
* Indentation for open and close tokens of the node if it is block or another node that needs special indentation
3838
* ... {
@@ -455,17 +455,18 @@ namespace ts.formatting {
455455

456456
function getDynamicIndentation(node: Node, nodeStartLine: number, indentation: number, delta: number): DynamicIndentation {
457457
return {
458-
getIndentationForComment: kind => {
458+
getIndentationForComment: (kind, tokenIndentation) => {
459459
switch (kind) {
460460
// preceding comment to the token that closes the indentation scope inherits the indentation from the scope
461461
// .. {
462462
// // comment
463463
// }
464464
case SyntaxKind.CloseBraceToken:
465465
case SyntaxKind.CloseBracketToken:
466+
case SyntaxKind.CloseParenToken:
466467
return indentation + delta;
467468
}
468-
return indentation;
469+
return tokenIndentation !== Constants.Unknown ? tokenIndentation : indentation;
469470
},
470471
getIndentationForToken: (line, kind) => {
471472
if (nodeStartLine !== line && node.decorators) {
@@ -716,22 +717,25 @@ namespace ts.formatting {
716717
}
717718

718719
if (indentToken) {
719-
let indentNextTokenOrTrivia = true;
720+
let tokenIndentation = (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) ?
721+
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind) :
722+
Constants.Unknown;
723+
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind, tokenIndentation);
724+
720725
if (currentTokenInfo.leadingTrivia) {
726+
let indentNextTokenOrTrivia = true;
721727
for (let triviaItem of currentTokenInfo.leadingTrivia) {
722728
if (!rangeContainsRange(originalRange, triviaItem)) {
723729
continue;
724730
}
725731

726732
switch (triviaItem.kind) {
727733
case SyntaxKind.MultiLineCommentTrivia:
728-
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
729734
indentMultilineComment(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
730735
indentNextTokenOrTrivia = false;
731736
break;
732737
case SyntaxKind.SingleLineCommentTrivia:
733738
if (indentNextTokenOrTrivia) {
734-
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
735739
insertIndentation(triviaItem.pos, commentIndentation, /*lineAdded*/ false);
736740
indentNextTokenOrTrivia = false;
737741
}
@@ -744,8 +748,7 @@ namespace ts.formatting {
744748
}
745749

746750
// indent token only if is it is in target range and does not overlap with any error ranges
747-
if (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) {
748-
let tokenIndentation = dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind);
751+
if (tokenIndentation !== Constants.Unknown) {
749752
insertIndentation(currentTokenInfo.token.pos, tokenIndentation, lineAdded);
750753

751754
lastIndentedLine = tokenStart.line;

0 commit comments

Comments
 (0)