diff --git a/src/services/formatting/indentationTrackingWalker.ts b/src/services/formatting/indentationTrackingWalker.ts index 3295bdaff5f..8e86c67f40f 100644 --- a/src/services/formatting/indentationTrackingWalker.ts +++ b/src/services/formatting/indentationTrackingWalker.ts @@ -95,6 +95,9 @@ module TypeScript.Services.Formatting { var trivia = _nextToken.leadingTrivia(); this._lastTriviaWasNewLine = trivia.hasNewLine(); } + else { + this._lastTriviaWasNewLine = false; + } } // Update the position @@ -356,7 +359,7 @@ module TypeScript.Services.Formatting { private forceRecomputeIndentationOfParent(tokenStart: number, newLineAdded: boolean /*as opposed to removed*/): void { var parent = this._parent; - if (parent.fullStart() === tokenStart) { + if (start(parent.node()) === tokenStart) { // Temporarily pop the parent before recomputing this._parent = parent.parent(); var indentation = this.getNodeIndentation(parent.node(), /* newLineInsertedByFormatting */ newLineAdded); diff --git a/src/services/formatting/multipleTokenIndenter.ts b/src/services/formatting/multipleTokenIndenter.ts index f4dda09f85b..ad74952ad3d 100644 --- a/src/services/formatting/multipleTokenIndenter.ts +++ b/src/services/formatting/multipleTokenIndenter.ts @@ -66,8 +66,23 @@ module TypeScript.Services.Formatting { // Process any leading trivia if any var triviaList = token.leadingTrivia(); if (triviaList) { + var seenNewLine = position === 0; + for (var i = 0, length = triviaList.count(); i < length; i++, position += trivia.fullWidth()) { var trivia = triviaList.syntaxTriviaAt(i); + + // Skip all trivia up to the first newline we see. We consider this trivia to + // 'belong' to the previous token. + if (!seenNewLine) { + if (trivia.kind !== SyntaxKind.NewLineTrivia) { + continue; + } + else { + seenNewLine = true; + continue; + } + } + // Skip this trivia if it is not in the span if (!this.textSpan().containsTextSpan(new TextSpan(position, trivia.fullWidth()))) { continue; diff --git a/tests/cases/fourslash/autoFormattingOnPasting.ts b/tests/cases/fourslash/autoFormattingOnPasting.ts index 03f70ac58c1..98efcc02ab3 100644 --- a/tests/cases/fourslash/autoFormattingOnPasting.ts +++ b/tests/cases/fourslash/autoFormattingOnPasting.ts @@ -3,7 +3,7 @@ ////module TestModule { /////**/ ////} - +debugger; goTo.marker(""); edit.paste(" class TestClass{\r\n\ private foo;\r\n\ diff --git a/tests/cases/fourslash/forceIndentAfterNewLineInsert.ts b/tests/cases/fourslash/forceIndentAfterNewLineInsert.ts index bece44bc24d..af6a21e97ea 100644 --- a/tests/cases/fourslash/forceIndentAfterNewLineInsert.ts +++ b/tests/cases/fourslash/forceIndentAfterNewLineInsert.ts @@ -6,7 +6,7 @@ ////{ function h() { ////return 0; ////}} - +debugger; format.document(); verify.currentFileContentIs( "function f()\n" + diff --git a/tests/cases/fourslash/formattingAfterMultiLineIfCondition.ts b/tests/cases/fourslash/formattingAfterMultiLineIfCondition.ts index 21812b0b2b3..843eea5155b 100644 --- a/tests/cases/fourslash/formattingAfterMultiLineIfCondition.ts +++ b/tests/cases/fourslash/formattingAfterMultiLineIfCondition.ts @@ -11,4 +11,4 @@ goTo.marker(); edit.insert('}'); goTo.marker('comment'); // Comment below multi-line 'if' condition formatting -verify.currentLineContentIs(' // This is a comment'); \ No newline at end of file +verify.currentLineContentIs(' // This is a comment'); \ No newline at end of file diff --git a/tests/cases/fourslash/multilineCommentBeforeOpenBrace.ts b/tests/cases/fourslash/multilineCommentBeforeOpenBrace.ts index 7b784445486..792691d9418 100644 --- a/tests/cases/fourslash/multilineCommentBeforeOpenBrace.ts +++ b/tests/cases/fourslash/multilineCommentBeforeOpenBrace.ts @@ -8,11 +8,11 @@ ////} ////function a() { //// /* %^ */ }/*3*/ - +debugger; format.document(); goTo.marker('1'); -verify.currentLineContentIs('function test() /* %^ */ {'); +verify.currentLineContentIs('function test() /* %^ */'); goTo.marker('2'); -verify.currentLineContentIs(' if (true) /* %^ */ {'); +verify.currentLineContentIs(' if (true) /* %^ */'); goTo.marker('3'); verify.currentLineContentIs('}'); \ No newline at end of file diff --git a/tests/cases/fourslash/semicolonFormattingNestedStatements.ts b/tests/cases/fourslash/semicolonFormattingNestedStatements.ts index 20d49a35f47..87cdebb3c9b 100644 --- a/tests/cases/fourslash/semicolonFormattingNestedStatements.ts +++ b/tests/cases/fourslash/semicolonFormattingNestedStatements.ts @@ -12,11 +12,11 @@ goTo.marker("innermost"); edit.insert(";"); // Adding smicolon should format the innermost statement -verify.currentLineContentIs(' var x = 0;'); +verify.currentLineContentIs(' var x = 0;'); // Also should format any parent statement that is terminated by the semicolon goTo.marker("directParent"); -verify.currentLineContentIs(' if (true)'); +verify.currentLineContentIs(' if (true)'); // But not parents that are not terminated by it goTo.marker("parentOutsideBlock"); diff --git a/tests/cases/fourslash/spaceAfterReturn.ts b/tests/cases/fourslash/spaceAfterReturn.ts index 16b44d81024..e59b604f976 100644 --- a/tests/cases/fourslash/spaceAfterReturn.ts +++ b/tests/cases/fourslash/spaceAfterReturn.ts @@ -5,7 +5,7 @@ ////return[1];/*2*/ ////return ;/*3*/ ////} - +debugger; format.document(); goTo.marker("1"); verify.currentLineContentIs(" return 1;");