Skip to content

Commit 6c23a6b

Browse files
Merge pull request microsoft#4638 from SaschaNaz/fixCommentIndentation
Fix comment indentation
2 parents 46c0057 + 4aded27 commit 6c23a6b

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

src/services/formatting/formatting.ts

Lines changed: 12 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,26 @@ 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+
720724
if (currentTokenInfo.leadingTrivia) {
725+
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind, tokenIndentation);
726+
let indentNextTokenOrTrivia = true;
727+
721728
for (let triviaItem of currentTokenInfo.leadingTrivia) {
722729
if (!rangeContainsRange(originalRange, triviaItem)) {
723730
continue;
724731
}
725732

726733
switch (triviaItem.kind) {
727734
case SyntaxKind.MultiLineCommentTrivia:
728-
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
729735
indentMultilineComment(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
730736
indentNextTokenOrTrivia = false;
731737
break;
732738
case SyntaxKind.SingleLineCommentTrivia:
733739
if (indentNextTokenOrTrivia) {
734-
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
735740
insertIndentation(triviaItem.pos, commentIndentation, /*lineAdded*/ false);
736741
indentNextTokenOrTrivia = false;
737742
}
@@ -744,8 +749,7 @@ namespace ts.formatting {
744749
}
745750

746751
// 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);
752+
if (tokenIndentation !== Constants.Unknown) {
749753
insertIndentation(currentTokenInfo.token.pos, tokenIndentation, lineAdded);
750754

751755
lastIndentedLine = tokenStart.line;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
////_.chain()
4+
////// wow/*callChain1*/
5+
//// .then()
6+
////// waa/*callChain2*/
7+
//// .then();
8+
////wow(
9+
//// 3,
10+
////// uaa/*argument1*/
11+
//// 4
12+
////// wua/*argument2*/
13+
////);
14+
15+
format.document();
16+
17+
goTo.marker("callChain1");
18+
verify.currentLineContentIs(" // wow");
19+
goTo.marker("callChain2");
20+
verify.currentLineContentIs(" // waa");
21+
goTo.marker("argument1");
22+
verify.currentLineContentIs(" // uaa");
23+
goTo.marker("argument2");
24+
verify.currentLineContentIs(" // wua");

0 commit comments

Comments
 (0)