Fix formatOnEnter for double newlines
This commit is contained in:
parent
35b8b42b55
commit
3433a7800a
4 changed files with 34 additions and 0 deletions
|
@ -1486,6 +1486,12 @@ namespace FourSlash {
|
||||||
this.fixCaretPosition();
|
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) {
|
private updateMarkersForEdit(fileName: string, minChar: number, limChar: number, text: string) {
|
||||||
for (let i = 0; i < this.testData.markers.length; i++) {
|
for (let i = 0; i < this.testData.markers.length; i++) {
|
||||||
const marker = this.testData.markers[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);
|
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: number): void;
|
||||||
public setOption(name: string, value: string): void;
|
public setOption(name: string, value: string): void;
|
||||||
public setOption(name: string, value: boolean): void;
|
public setOption(name: string, value: boolean): void;
|
||||||
|
|
|
@ -81,6 +81,12 @@ namespace ts.formatting {
|
||||||
while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) {
|
while (isWhiteSpace(sourceFile.text.charCodeAt(endOfFormatSpan)) && !isLineBreak(sourceFile.text.charCodeAt(endOfFormatSpan))) {
|
||||||
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 = {
|
const span = {
|
||||||
// get start position for the previous line
|
// get start position for the previous line
|
||||||
pos: getStartPositionOfLine(line - 1, sourceFile),
|
pos: getStartPositionOfLine(line - 1, sourceFile),
|
||||||
|
|
17
tests/cases/fourslash/formatOnEnter.ts
Normal file
17
tests/cases/fourslash/formatOnEnter.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/// <reference path="fourslash.ts"/>
|
||||||
|
|
||||||
|
/////*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[] {");
|
|
@ -246,6 +246,7 @@ declare namespace FourSlashInterface {
|
||||||
copyFormatOptions(): FormatCodeOptions;
|
copyFormatOptions(): FormatCodeOptions;
|
||||||
setFormatOptions(options: FormatCodeOptions): any;
|
setFormatOptions(options: FormatCodeOptions): any;
|
||||||
selection(startMarker: string, endMarker: string): void;
|
selection(startMarker: string, endMarker: string): void;
|
||||||
|
onType(posMarker: string, key: string): void;
|
||||||
setOption(name: string, value: number): any;
|
setOption(name: string, value: number): any;
|
||||||
setOption(name: string, value: string): any;
|
setOption(name: string, value: string): any;
|
||||||
setOption(name: string, value: boolean): any;
|
setOption(name: string, value: boolean): any;
|
||||||
|
|
Loading…
Reference in a new issue