From 30ada4cffe77ac1f84fbd8ad9efd0fedb0c40774 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 10 Dec 2014 14:03:14 -0800 Subject: [PATCH 1/3] conditionals are now introduce indentation scope --- src/services/smartIndenter.ts | 1 + .../cases/fourslash/formattingConditionals.ts | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/cases/fourslash/formattingConditionals.ts diff --git a/src/services/smartIndenter.ts b/src/services/smartIndenter.ts index cafd92a7551..758c06226a4 100644 --- a/src/services/smartIndenter.ts +++ b/src/services/smartIndenter.ts @@ -342,6 +342,7 @@ module ts.formatting { case SyntaxKind.VariableDeclaration: case SyntaxKind.ExportAssignment: case SyntaxKind.ReturnStatement: + case SyntaxKind.ConditionalExpression: return true; } return false; diff --git a/tests/cases/fourslash/formattingConditionals.ts b/tests/cases/fourslash/formattingConditionals.ts new file mode 100644 index 00000000000..65029134b87 --- /dev/null +++ b/tests/cases/fourslash/formattingConditionals.ts @@ -0,0 +1,23 @@ +/// + + +////var v = +/////*0*/a === b +/////*1*/? c +/////*2*/: d; + +////var v = a === b +/////*3*/? c +/////*4*/: d; + +function verifyLine(marker: string, content: string) { + goTo.marker(marker); + verify.currentLineContentIs(content); +} + +format.document(); +verifyLine("0", " a === b"); +verifyLine("1", " ? c"); +verifyLine("2", " : d;"); +verifyLine("3", " ? c"); +verifyLine("4", " : d;"); From 10d08b816e2988e9e3d35b9eb07a8e5917b91481 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 10 Dec 2014 15:08:26 -0800 Subject: [PATCH 2/3] do not indent leading comments that attached to tokens with errors --- src/services/formatting.ts | 13 +++++++++--- .../formattingCommentsBeforeErrors.ts | 20 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/formattingCommentsBeforeErrors.ts diff --git a/src/services/formatting.ts b/src/services/formatting.ts index 0c2dd4c51dc..48d3c80445c 100644 --- a/src/services/formatting.ts +++ b/src/services/formatting.ts @@ -622,14 +622,21 @@ module ts.formatting { var tokenStart = sourceFile.getLineAndCharacterFromPosition(currentTokenInfo.token.pos); if (isTokenInRange) { + var rangeHasError = rangeContainsError(currentTokenInfo.token); // save prevStartLine since processRange will overwrite this value with current ones var prevStartLine = previousRangeStartLine; lineAdded = processRange(currentTokenInfo.token, tokenStart, parent, childContextNode, dynamicIndentation); - if (lineAdded !== undefined) { - indentToken = lineAdded; + if (rangeHasError) { + // do not indent comments\token if token range overlaps with some error + indentToken = false; } else { - indentToken = lastTriviaWasNewLine && tokenStart.line !== prevStartLine; + if (lineAdded !== undefined) { + indentToken = lineAdded; + } + else { + indentToken = lastTriviaWasNewLine && tokenStart.line !== prevStartLine; + } } } diff --git a/tests/cases/fourslash/formattingCommentsBeforeErrors.ts b/tests/cases/fourslash/formattingCommentsBeforeErrors.ts new file mode 100644 index 00000000000..bb7e616b267 --- /dev/null +++ b/tests/cases/fourslash/formattingCommentsBeforeErrors.ts @@ -0,0 +1,20 @@ +/// + +////module A { +//// interface B { +//// // a +//// // b +//// baz(); +/////*0*/ // d /*1*/asd a +//// // e +//// foo(); +//// // f asd +//// // g as +//// bar(); +//// } +////} + +goTo.marker("1"); +edit.insert("\n"); +goTo.marker("0"); +verify.currentLineContentIs(" // d "); \ No newline at end of file From d69ba56ece6ece08051877d37f93d6906435b495 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 10 Dec 2014 22:01:34 -0800 Subject: [PATCH 3/3] added test for inherited indentation --- .../cases/fourslash/formattingConditionals.ts | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/tests/cases/fourslash/formattingConditionals.ts b/tests/cases/fourslash/formattingConditionals.ts index 65029134b87..cb6edfcd56b 100644 --- a/tests/cases/fourslash/formattingConditionals.ts +++ b/tests/cases/fourslash/formattingConditionals.ts @@ -10,14 +10,35 @@ /////*3*/? c /////*4*/: d; +////var x = +/////*5*/a +/////*6*/? function(){ +/////*7*/var z = 1 +/////*8*/} +/////*9*/: function(){ +/////*10*/var z = 2 +/////*11*/} + + + + function verifyLine(marker: string, content: string) { goTo.marker(marker); verify.currentLineContentIs(content); } format.document(); -verifyLine("0", " a === b"); -verifyLine("1", " ? c"); -verifyLine("2", " : d;"); -verifyLine("3", " ? c"); -verifyLine("4", " : d;"); +verifyLine("0", " a === b"); +verifyLine("1", " ? c"); +verifyLine("2", " : d;"); + +verifyLine("3", " ? c"); +verifyLine("4", " : d;"); + +verifyLine("5", " a"); +verifyLine("6", " ? function() {"); +verifyLine("7", " var z = 1"); +verifyLine("8", " }"); +verifyLine("9", " : function() {"); +verifyLine("10", " var z = 2"); +verifyLine("11", " }");