diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 4a756ab3b1..bd556c659c 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 2f5f10752f..7127eb9800 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 0000000000..f505cf6cec --- /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 8a0d2d3636..71e1c3cc5d 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;