Merge pull request #4638 from SaschaNaz/fixCommentIndentation
Fix comment indentation
This commit is contained in:
commit
6c23a6b534
|
@ -32,7 +32,7 @@ namespace ts.formatting {
|
||||||
*/
|
*/
|
||||||
interface DynamicIndentation {
|
interface DynamicIndentation {
|
||||||
getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind): number;
|
getIndentationForToken(tokenLine: number, tokenKind: SyntaxKind): number;
|
||||||
getIndentationForComment(owningToken: SyntaxKind): number;
|
getIndentationForComment(owningToken: SyntaxKind, tokenIndentation: number): number;
|
||||||
/**
|
/**
|
||||||
* Indentation for open and close tokens of the node if it is block or another node that needs special indentation
|
* Indentation for open and close tokens of the node if it is block or another node that needs special indentation
|
||||||
* ... {
|
* ... {
|
||||||
|
@ -455,7 +455,7 @@ namespace ts.formatting {
|
||||||
|
|
||||||
function getDynamicIndentation(node: Node, nodeStartLine: number, indentation: number, delta: number): DynamicIndentation {
|
function getDynamicIndentation(node: Node, nodeStartLine: number, indentation: number, delta: number): DynamicIndentation {
|
||||||
return {
|
return {
|
||||||
getIndentationForComment: kind => {
|
getIndentationForComment: (kind, tokenIndentation) => {
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
// preceding comment to the token that closes the indentation scope inherits the indentation from the scope
|
// preceding comment to the token that closes the indentation scope inherits the indentation from the scope
|
||||||
// .. {
|
// .. {
|
||||||
|
@ -463,9 +463,10 @@ namespace ts.formatting {
|
||||||
// }
|
// }
|
||||||
case SyntaxKind.CloseBraceToken:
|
case SyntaxKind.CloseBraceToken:
|
||||||
case SyntaxKind.CloseBracketToken:
|
case SyntaxKind.CloseBracketToken:
|
||||||
|
case SyntaxKind.CloseParenToken:
|
||||||
return indentation + delta;
|
return indentation + delta;
|
||||||
}
|
}
|
||||||
return indentation;
|
return tokenIndentation !== Constants.Unknown ? tokenIndentation : indentation;
|
||||||
},
|
},
|
||||||
getIndentationForToken: (line, kind) => {
|
getIndentationForToken: (line, kind) => {
|
||||||
if (nodeStartLine !== line && node.decorators) {
|
if (nodeStartLine !== line && node.decorators) {
|
||||||
|
@ -716,8 +717,14 @@ namespace ts.formatting {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (indentToken) {
|
if (indentToken) {
|
||||||
let indentNextTokenOrTrivia = true;
|
let tokenIndentation = (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) ?
|
||||||
|
dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind) :
|
||||||
|
Constants.Unknown;
|
||||||
|
|
||||||
if (currentTokenInfo.leadingTrivia) {
|
if (currentTokenInfo.leadingTrivia) {
|
||||||
|
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind, tokenIndentation);
|
||||||
|
let indentNextTokenOrTrivia = true;
|
||||||
|
|
||||||
for (let triviaItem of currentTokenInfo.leadingTrivia) {
|
for (let triviaItem of currentTokenInfo.leadingTrivia) {
|
||||||
if (!rangeContainsRange(originalRange, triviaItem)) {
|
if (!rangeContainsRange(originalRange, triviaItem)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -725,13 +732,11 @@ namespace ts.formatting {
|
||||||
|
|
||||||
switch (triviaItem.kind) {
|
switch (triviaItem.kind) {
|
||||||
case SyntaxKind.MultiLineCommentTrivia:
|
case SyntaxKind.MultiLineCommentTrivia:
|
||||||
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
|
|
||||||
indentMultilineComment(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
|
indentMultilineComment(triviaItem, commentIndentation, /*firstLineIsIndented*/ !indentNextTokenOrTrivia);
|
||||||
indentNextTokenOrTrivia = false;
|
indentNextTokenOrTrivia = false;
|
||||||
break;
|
break;
|
||||||
case SyntaxKind.SingleLineCommentTrivia:
|
case SyntaxKind.SingleLineCommentTrivia:
|
||||||
if (indentNextTokenOrTrivia) {
|
if (indentNextTokenOrTrivia) {
|
||||||
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
|
|
||||||
insertIndentation(triviaItem.pos, commentIndentation, /*lineAdded*/ false);
|
insertIndentation(triviaItem.pos, commentIndentation, /*lineAdded*/ false);
|
||||||
indentNextTokenOrTrivia = false;
|
indentNextTokenOrTrivia = false;
|
||||||
}
|
}
|
||||||
|
@ -744,8 +749,7 @@ namespace ts.formatting {
|
||||||
}
|
}
|
||||||
|
|
||||||
// indent token only if is it is in target range and does not overlap with any error ranges
|
// indent token only if is it is in target range and does not overlap with any error ranges
|
||||||
if (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) {
|
if (tokenIndentation !== Constants.Unknown) {
|
||||||
let tokenIndentation = dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind);
|
|
||||||
insertIndentation(currentTokenInfo.token.pos, tokenIndentation, lineAdded);
|
insertIndentation(currentTokenInfo.token.pos, tokenIndentation, lineAdded);
|
||||||
|
|
||||||
lastIndentedLine = tokenStart.line;
|
lastIndentedLine = tokenStart.line;
|
||||||
|
|
24
tests/cases/fourslash/formatComments.ts
Normal file
24
tests/cases/fourslash/formatComments.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
/// <reference path="fourslash.ts"/>
|
||||||
|
|
||||||
|
////_.chain()
|
||||||
|
////// wow/*callChain1*/
|
||||||
|
//// .then()
|
||||||
|
////// waa/*callChain2*/
|
||||||
|
//// .then();
|
||||||
|
////wow(
|
||||||
|
//// 3,
|
||||||
|
////// uaa/*argument1*/
|
||||||
|
//// 4
|
||||||
|
////// wua/*argument2*/
|
||||||
|
////);
|
||||||
|
|
||||||
|
format.document();
|
||||||
|
|
||||||
|
goTo.marker("callChain1");
|
||||||
|
verify.currentLineContentIs(" // wow");
|
||||||
|
goTo.marker("callChain2");
|
||||||
|
verify.currentLineContentIs(" // waa");
|
||||||
|
goTo.marker("argument1");
|
||||||
|
verify.currentLineContentIs(" // uaa");
|
||||||
|
goTo.marker("argument2");
|
||||||
|
verify.currentLineContentIs(" // wua");
|
Loading…
Reference in a new issue