Skip to content

fix formatting for chained callbacks #3001

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ module ts.formatting {
let previousParent: Node;
let previousRangeStartLine: number;

let lastIndentedLine: number;
let indentationOnLastIndentedLine: number;

let edits: TextChange[] = [];

formattingScanner.advance();
Expand Down Expand Up @@ -416,7 +419,9 @@ module ts.formatting {
// if node is located on the same line with the parent
// - inherit indentation from the parent
// - push children if either parent of node itself has non-zero delta
indentation = parentDynamicIndentation.getIndentation();
indentation = startLine === lastIndentedLine
? indentationOnLastIndentedLine
: parentDynamicIndentation.getIndentation();
delta = Math.min(options.IndentSize, parentDynamicIndentation.getDelta() + delta);
}
return {
Expand Down Expand Up @@ -716,7 +721,6 @@ module ts.formatting {
continue;
}

let triviaStartLine = sourceFile.getLineAndCharacterOfPosition(triviaItem.pos).line;
switch (triviaItem.kind) {
case SyntaxKind.MultiLineCommentTrivia:
let commentIndentation = dynamicIndentation.getIndentationForComment(currentTokenInfo.token.kind);
Expand All @@ -741,6 +745,9 @@ module ts.formatting {
if (isTokenInRange && !rangeContainsError(currentTokenInfo.token)) {
let tokenIndentation = dynamicIndentation.getIndentationForToken(tokenStart.line, currentTokenInfo.token.kind);
insertIndentation(currentTokenInfo.token.pos, tokenIndentation, lineAdded);

lastIndentedLine = tokenStart.line;
indentationOnLastIndentedLine = tokenIndentation;
}
}

Expand Down
11 changes: 4 additions & 7 deletions tests/cases/fourslash/formattingOnChainedCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//// .resolve()
//// .then(() => {/*1*/""/*2*/
////}).then(() => {/*3*//*4*/
////})/*semi1*/ /*semi2*/
////})/*semi1*//*semi2*/

////function foo() {
//// return Promise.resolve()
Expand All @@ -16,17 +16,15 @@
goTo.marker('1');
edit.insertLine('');
goTo.marker('2');
// Expected, with bug 1888: verify.currentLineContentIs(' ""');
verify.currentLineContentIs(' ""');
verify.currentLineContentIs(' ""');
goTo.marker('4');
edit.insertLine('');
goTo.marker('3');
verify.currentLineContentIs(' }).then(() => {');

goTo.marker("semi1");
edit.insert(';');
// Expected, with bug 1888: verify.currentLineContentIs(' });');
verify.currentLineContentIs('}); ');
verify.currentLineContentIs(' });');
goTo.marker("semi2");
edit.insert(';');
verify.currentLineContentIs(' });;');
Expand All @@ -36,5 +34,4 @@ edit.insert(';');
verify.currentLineContentIs(' "";');
goTo.marker('b');
edit.insert(';');
// Expected, with bug 1888: verify.currentLineContentIs(' });');
verify.currentLineContentIs(' });');
verify.currentLineContentIs(' });');