From 2a2595fc5fe997df5659a1f867a2688ada68e1e1 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Mon, 19 Jun 2017 10:57:47 -0700 Subject: [PATCH 1/3] apply formatting after parse error --- src/services/formatting/formatting.ts | 4 +--- tests/cases/fourslash/formatOnEnterEmptyBlock.ts | 16 ++++++++++++++++ tests/cases/fourslash/fourslash.ts | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 tests/cases/fourslash/formatOnEnterEmptyBlock.ts diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 531b768f6d..2450cdca60 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -363,7 +363,6 @@ namespace ts.formatting { // formatting context is used by rules provider const formattingContext = new FormattingContext(sourceFile, requestKind, options); - let previousRangeHasError: boolean; let previousRange: TextRangeWithKind; let previousParent: Node; let previousRangeStartLine: number; @@ -848,7 +847,7 @@ namespace ts.formatting { const rangeHasError = rangeContainsError(range); let lineAdded: boolean; - if (!rangeHasError && !previousRangeHasError) { + if (!rangeHasError) { if (!previousRange) { // trim whitespaces starting from the beginning of the span up to the current line const originalStart = sourceFile.getLineAndCharacterOfPosition(originalRange.pos); @@ -863,7 +862,6 @@ namespace ts.formatting { previousRange = range; previousParent = parent; previousRangeStartLine = rangeStart.line; - previousRangeHasError = rangeHasError; return lineAdded; } diff --git a/tests/cases/fourslash/formatOnEnterEmptyBlock.ts b/tests/cases/fourslash/formatOnEnterEmptyBlock.ts new file mode 100644 index 0000000000..fd24edf4e5 --- /dev/null +++ b/tests/cases/fourslash/formatOnEnterEmptyBlock.ts @@ -0,0 +1,16 @@ +/// + +//// if (true) { +//// } +//// if () { +//// } + +format.setOption("PlaceOpenBraceOnNewLineForControlBlocks", true); +format.document(); +verify.currentFileContentIs( +`if (true) +{ +} +if () +{ +}`); diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 767701a2af..69a1551635 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -329,7 +329,7 @@ declare namespace FourSlashInterface { setFormatOptions(options: FormatCodeOptions): any; selection(startMarker: string, endMarker: string): void; onType(posMarker: string, key: string): void; - setOption(name: string, value: number | string | boolean): void; + setOption(name: keyof FormatCodeOptions, value: number | string | boolean): void; } class cancellation { resetCancelled(): void; From 0dcc8deace445bf7ac069e226025e224abccfe04 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Mon, 19 Jun 2017 11:13:58 -0700 Subject: [PATCH 2/3] update tests --- tests/cases/fourslash/formatEmptyParamList.ts | 2 +- tests/cases/fourslash/formattingOnConstructorSignature.ts | 4 ++-- tests/cases/fourslash/formattingSkippedTokens.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/cases/fourslash/formatEmptyParamList.ts b/tests/cases/fourslash/formatEmptyParamList.ts index a5372010ba..46c5e0072b 100644 --- a/tests/cases/fourslash/formatEmptyParamList.ts +++ b/tests/cases/fourslash/formatEmptyParamList.ts @@ -2,4 +2,4 @@ ////function f( f: function){/*1*/ goTo.marker("1"); edit.insert("}"); -verify.currentLineContentIs("function f(f: function){ }") \ No newline at end of file +verify.currentLineContentIs("function f(f: function) { }") \ No newline at end of file diff --git a/tests/cases/fourslash/formattingOnConstructorSignature.ts b/tests/cases/fourslash/formattingOnConstructorSignature.ts index 6b91396e53..209527e825 100644 --- a/tests/cases/fourslash/formattingOnConstructorSignature.ts +++ b/tests/cases/fourslash/formattingOnConstructorSignature.ts @@ -4,6 +4,6 @@ /////*2*/type Stylet = { new () {} } format.document(); goTo.marker("1"); -verify.currentLineContentIs("interface Gourai { new() {} }"); +verify.currentLineContentIs("interface Gourai { new() { } }"); goTo.marker("2"); -verify.currentLineContentIs("type Stylet = { new() {} }"); \ No newline at end of file +verify.currentLineContentIs("type Stylet = { new() { } }"); \ No newline at end of file diff --git a/tests/cases/fourslash/formattingSkippedTokens.ts b/tests/cases/fourslash/formattingSkippedTokens.ts index d3e76e9735..caf65d1dc2 100644 --- a/tests/cases/fourslash/formattingSkippedTokens.ts +++ b/tests/cases/fourslash/formattingSkippedTokens.ts @@ -14,7 +14,7 @@ verify.currentLineContentIs('foo(): Bar { }'); goTo.marker('2'); verify.currentLineContentIs('function Foo() # { }'); goTo.marker('3'); -verify.currentLineContentIs('4 +:5'); +verify.currentLineContentIs('4 +: 5'); goTo.marker('4'); verify.currentLineContentIs(' : T) { }'); goTo.marker('5'); From 37d4116ae0e69f31425cbd560e0fdf2ad61d10b6 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Mon, 19 Jun 2017 14:22:56 -0700 Subject: [PATCH 3/3] rename test --- ...tOnEnterEmptyBlock.ts => formatIfWithEmptyCondition.ts} | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) rename tests/cases/fourslash/{formatOnEnterEmptyBlock.ts => formatIfWithEmptyCondition.ts} (74%) diff --git a/tests/cases/fourslash/formatOnEnterEmptyBlock.ts b/tests/cases/fourslash/formatIfWithEmptyCondition.ts similarity index 74% rename from tests/cases/fourslash/formatOnEnterEmptyBlock.ts rename to tests/cases/fourslash/formatIfWithEmptyCondition.ts index fd24edf4e5..6e949900e2 100644 --- a/tests/cases/fourslash/formatOnEnterEmptyBlock.ts +++ b/tests/cases/fourslash/formatIfWithEmptyCondition.ts @@ -1,16 +1,11 @@ /// -//// if (true) { -//// } //// if () { //// } format.setOption("PlaceOpenBraceOnNewLineForControlBlocks", true); format.document(); verify.currentFileContentIs( -`if (true) -{ -} -if () +`if () { }`);