Skip to content

Commit 5c095a8

Browse files
committed
Merge pull request #8901 from zhengbli/formatOnTypeCurrentLine1
Not let the "Format on enter" feature delete the indentation of the current line
2 parents 35b8b42 + 58fdd01 commit 5c095a8

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/harness/fourslash.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,12 @@ namespace FourSlash {
14861486
this.fixCaretPosition();
14871487
}
14881488

1489+
public formatOnType(pos: number, key: string) {
1490+
const edits = this.languageService.getFormattingEditsAfterKeystroke(this.activeFile.fileName, pos, key, this.formatCodeOptions);
1491+
this.currentCaretPosition += this.applyEdits(this.activeFile.fileName, edits, /*isFormattingEdit*/ true);
1492+
this.fixCaretPosition();
1493+
}
1494+
14891495
private updateMarkersForEdit(fileName: string, minChar: number, limChar: number, text: string) {
14901496
for (let i = 0; i < this.testData.markers.length; i++) {
14911497
const marker = this.testData.markers[i];
@@ -3223,6 +3229,10 @@ namespace FourSlashInterface {
32233229
this.state.formatSelection(this.state.getMarkerByName(startMarker).position, this.state.getMarkerByName(endMarker).position);
32243230
}
32253231

3232+
public onType(posMarker: string, key: string) {
3233+
this.state.formatOnType(this.state.getMarkerByName(posMarker).position, key);
3234+
}
3235+
32263236
public setOption(name: string, value: number): void;
32273237
public setOption(name: string, value: string): void;
32283238
public setOption(name: string, value: boolean): void;

src/services/formatting/formatting.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ namespace ts.formatting {
8181
while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) {
8282
endOfFormatSpan--;
8383
}
84+
// 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
85+
// touch the current line at all. Also, on some OSes the line break consists of two characters (\r\n), we should test if the
86+
// previous character before the end of format span is line break character as well.
87+
if (isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) {
88+
endOfFormatSpan--;
89+
}
8490
const span = {
8591
// get start position for the previous line
8692
pos: getStartPositionOfLine(line - 1, sourceFile),

tests/cases/fourslash/fourslash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ declare namespace FourSlashInterface {
246246
copyFormatOptions(): FormatCodeOptions;
247247
setFormatOptions(options: FormatCodeOptions): any;
248248
selection(startMarker: string, endMarker: string): void;
249+
onType(posMarker: string, key: string): void;
249250
setOption(name: string, value: number): any;
250251
setOption(name: string, value: string): any;
251252
setOption(name: string, value: boolean): any;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts"/>
2+
3+
/////*3*/function listAPIFiles (path : string): string[] {
4+
//// /*1*/
5+
//// /*2*/
6+
////}
7+
8+
goTo.marker("1");
9+
format.onType("1", "\n");
10+
verify.currentLineContentIs(" ");
11+
12+
goTo.marker("2");
13+
format.onType("2", "\n");
14+
verify.currentLineContentIs(" ");
15+
16+
goTo.marker("3");
17+
verify.currentLineContentIs("function listAPIFiles(path: string): string[] {");

0 commit comments

Comments
 (0)