Skip to content

Commit 7c0927d

Browse files
committed
formatting
1 parent a8dc65d commit 7c0927d

4 files changed

Lines changed: 53 additions & 4 deletions

File tree

src/server/editorServices.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,8 @@ namespace ts.server {
15191519
for (const file of changedFiles) {
15201520
const scriptInfo = this.getScriptInfo(file.fileName);
15211521
Debug.assert(!!scriptInfo);
1522-
for (const change of file.changes) {
1522+
for (let i = file.changes.length - 1; i >= 0; i--) {
1523+
const change = file.changes[i];
15231524
scriptInfo.editContent(change.span.start, change.span.start + change.span.length, change.newText);
15241525
}
15251526
}

src/server/protocol.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@ declare namespace ts.server.protocol {
735735
* Character offset on last line of range for which to format text in file.
736736
*/
737737
endOffset: number;
738+
739+
endPosition?: number;
740+
options?: ts.FormatCodeOptions;
738741
}
739742

740743
/**
@@ -788,6 +791,8 @@ declare namespace ts.server.protocol {
788791
* Key pressed (';', '\n', or '}').
789792
*/
790793
key: string;
794+
795+
options?: ts.FormatCodeOptions;
791796
}
792797

793798
/**

src/server/session.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ namespace ts.server {
112112
export const Exit = "exit";
113113
export const Format = "format";
114114
export const Formatonkey = "formatonkey";
115+
export const FormatFull = "format-full";
116+
export const FormatonkeyFull = "formatonkey-full";
117+
export const FormatRangeFull = "formatRange-full";
115118
export const Geterr = "geterr";
116119
export const GeterrForProject = "geterrForProject";
117120
export const SemanticDiagnosticsFull = "semanticDiagnostics-full";
@@ -720,6 +723,36 @@ namespace ts.server {
720723
});
721724
}
722725

726+
private getFormattingEditsForRangeFull(args: protocol.FormatRequestArgs) {
727+
const file = ts.normalizePath(args.file);
728+
const project = this.projectService.getProjectForFile(file);
729+
if (!project) {
730+
throw Errors.NoProject;
731+
}
732+
733+
return project.languageService.getFormattingEditsForRange(file, args.position, args.endPosition, args.options);
734+
}
735+
736+
private getFormattingEditsForDocumentFull(args: protocol.FormatRequestArgs) {
737+
const file = ts.normalizePath(args.file);
738+
const project = this.projectService.getProjectForFile(file);
739+
if (!project) {
740+
throw Errors.NoProject;
741+
}
742+
743+
return project.languageService.getFormattingEditsForDocument(file, args.options);
744+
}
745+
746+
private getFormattingEditsAfterKeystrokeFull(args: protocol.FormatOnKeyRequestArgs) {
747+
const file = ts.normalizePath(args.file);
748+
const project = this.projectService.getProjectForFile(file);
749+
if (!project) {
750+
throw Errors.NoProject;
751+
}
752+
753+
return project.languageService.getFormattingEditsAfterKeystroke(file, args.position, args.key, args.options);
754+
}
755+
723756
private getFormattingEditsAfterKeystroke(line: number, offset: number, key: string, fileName: string): protocol.CodeEdit[] {
724757
const file = ts.normalizePath(fileName);
725758

@@ -1180,11 +1213,20 @@ namespace ts.server {
11801213
},
11811214
[CommandNames.Format]: (request: protocol.Request) => {
11821215
const formatArgs = <protocol.FormatRequestArgs>request.arguments;
1183-
return { response: this.getFormattingEditsForRange(formatArgs.line, formatArgs.offset, formatArgs.endLine, formatArgs.endOffset, formatArgs.file), responseRequired: true };
1216+
return this.requiredResponse(this.getFormattingEditsForRange(formatArgs.line, formatArgs.offset, formatArgs.endLine, formatArgs.endOffset, formatArgs.file));
11841217
},
11851218
[CommandNames.Formatonkey]: (request: protocol.Request) => {
11861219
const formatOnKeyArgs = <protocol.FormatOnKeyRequestArgs>request.arguments;
1187-
return { response: this.getFormattingEditsAfterKeystroke(formatOnKeyArgs.line, formatOnKeyArgs.offset, formatOnKeyArgs.key, formatOnKeyArgs.file), responseRequired: true };
1220+
return this.requiredResponse(this.getFormattingEditsAfterKeystroke(formatOnKeyArgs.line, formatOnKeyArgs.offset, formatOnKeyArgs.key, formatOnKeyArgs.file));
1221+
},
1222+
[CommandNames.FormatFull]: (request: protocol.FormatRequest) => {
1223+
return this.requiredResponse(this.getFormattingEditsForDocumentFull(request.arguments));
1224+
},
1225+
[CommandNames.FormatonkeyFull]: (request: protocol.FormatOnKeyRequest) => {
1226+
return this.requiredResponse(this.getFormattingEditsAfterKeystrokeFull(request.arguments));
1227+
},
1228+
[CommandNames.FormatRangeFull]: (request: protocol.FormatRequest) => {
1229+
return this.requiredResponse(this.getFormattingEditsForRangeFull(request.arguments));
11881230
},
11891231
[CommandNames.Completions]: (request: protocol.CompletionDetailsRequest) => {
11901232
return this.requiredResponse(this.getCompletions(request.arguments, /*simplifiedResult*/ true));

src/services/services.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,7 @@ namespace ts {
18941894
}
18951895
}
18961896

1897+
let x = 1;
18971898
class SyntaxTreeCache {
18981899
// For our syntactic only features, we also keep a cache of the syntax tree for the
18991900
// currently edited file.
@@ -1926,7 +1927,7 @@ namespace ts {
19261927
sourceFile = updateLanguageServiceSourceFile(this.currentSourceFile, scriptSnapshot, version, editRange);
19271928
}
19281929

1929-
if (sourceFile) {
1930+
if (sourceFile) {
19301931
// All done, ensure state is up to date
19311932
this.currentFileVersion = version;
19321933
this.currentFileName = fileName;

0 commit comments

Comments
 (0)