From 3433a7800a01e045486e48bc06fffc551295086f Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 31 May 2016 12:35:12 -0700 Subject: [PATCH 1/2] Fix formatOnEnter for double newlines --- src/harness/fourslash.ts | 10 ++++++++++ src/services/formatting/formatting.ts | 6 ++++++ tests/cases/fourslash/formatOnEnter.ts | 17 +++++++++++++++++ tests/cases/fourslash/fourslash.ts | 1 + 4 files changed, 34 insertions(+) create mode 100644 tests/cases/fourslash/formatOnEnter.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 4a756ab3b1e..bd556c659c7 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -1486,6 +1486,12 @@ namespace FourSlash { this.fixCaretPosition(); } + public formatOnType(pos: number, key: string) { + const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, pos, key, this.formatCodeOptions); + this.currentCaretPosition += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true); + this.fixCaretPosition(); + } + private updateMarkersForEdit(fileName: string, minChar: number, limChar: number, text: string) { for (let i = 0; i < this.testData.markers.length; i++) { const marker = this.testData.markers[i]; @@ -3223,6 +3229,10 @@ namespace FourSlashInterface { this.state.formatSelection(this.state.getMarkerByName(startMarker).position, this.state.getMarkerByName(endMarker).position); } + public onType(posMarker: string, key: string) { + this.state.formatOnType(this.state.getMarkerByName(posMarker).position, key); + } + public setOption(name: string, value: number): void; public setOption(name: string, value: string): void; public setOption(name: string, value: boolean): void; diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 2f5f10752f4..7127eb98006 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -81,6 +81,12 @@ namespace ts.formatting { while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { endOfFormatSpan--; } + // if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to + // touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the + // previous character before the end of format span is line break character as well. + while (isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { + endOfFormatSpan--; + } const span = { // get start position for the previous line pos: getStartPositionOfLine(line - 1, sourceFile), diff --git a/tests/cases/fourslash/formatOnEnter.ts b/tests/cases/fourslash/formatOnEnter.ts new file mode 100644 index 00000000000..f505cf6cec7 --- /dev/null +++ b/tests/cases/fourslash/formatOnEnter.ts @@ -0,0 +1,17 @@ +/// + +/////*3*/function listAPIFiles (path : string): string[] { +//// /*1*/ +//// /*2*/ +////} + +goTo.marker("1"); +format.onType("1", "\n"); +verify.currentLineContentIs(" "); + +goTo.marker("2"); +format.onType("2", "\n"); +verify.currentLineContentIs(" "); + +goTo.marker("3"); +verify.currentLineContentIs("function listAPIFiles(path: string): string[] {"); \ No newline at end of file diff --git a/tests/cases/fourslash/fourslash.ts b/tests/cases/fourslash/fourslash.ts index 8a0d2d36364..71e1c3cc5d7 100644 --- a/tests/cases/fourslash/fourslash.ts +++ b/tests/cases/fourslash/fourslash.ts @@ -246,6 +246,7 @@ declare namespace FourSlashInterface { copyFormatOptions(): FormatCodeOptions; setFormatOptions(options: FormatCodeOptions): any; selection(startMarker: string, endMarker: string): void; + onType(posMarker: string, key: string): void; setOption(name: string, value: number): any; setOption(name: string, value: string): any; setOption(name: string, value: boolean): any; From 58fdd011df74f226f1909a2155896e43fc5c2042 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 31 May 2016 14:08:48 -0700 Subject: [PATCH 2/2] avoid eating all preceding empty lines --- src/services/formatting/formatting.ts | 2 +- tests/cases/fourslash/{ => server}/formatOnEnter.ts | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/cases/fourslash/{ => server}/formatOnEnter.ts (100%) diff --git a/src/services/formatting/formatting.ts b/src/services/formatting/formatting.ts index 7127eb98006..ef8fddcfb3a 100644 --- a/src/services/formatting/formatting.ts +++ b/src/services/formatting/formatting.ts @@ -84,7 +84,7 @@ namespace ts.formatting { // if the character at the end of the span is a line break, we shouldn't include it, because it indicates we don't want to // touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the // previous character before the end of format span is line break character as well. - while (isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { + if (isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) { endOfFormatSpan--; } const span = { diff --git a/tests/cases/fourslash/formatOnEnter.ts b/tests/cases/fourslash/server/formatOnEnter.ts similarity index 100% rename from tests/cases/fourslash/formatOnEnter.ts rename to tests/cases/fourslash/server/formatOnEnter.ts