Skip to content

Commit 286a12e

Browse files
authored
defer settings format options on file until it is explicitly requested (#10971)
1 parent 121b04e commit 286a12e

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,4 +1909,45 @@ namespace ts.projectSystem {
19091909
checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, barTypings.path]);
19101910
});
19111911
});
1912+
1913+
describe("format settings", () => {
1914+
it("can be set globally", () => {
1915+
const f1 = {
1916+
path: "/a/b/app.ts",
1917+
content: "let x;"
1918+
};
1919+
const host = createServerHost([f1]);
1920+
const projectService = createProjectService(host);
1921+
projectService.openClientFile(f1.path);
1922+
1923+
const defaultSettings = projectService.getFormatCodeOptions();
1924+
1925+
// set global settings
1926+
const newGlobalSettings1 = clone(defaultSettings);
1927+
newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks = !newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks;
1928+
projectService.setHostConfiguration({ formatOptions: newGlobalSettings1 });
1929+
1930+
// get format options for file - should be equal to new global settings
1931+
const s1 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
1932+
assert.deepEqual(s1, newGlobalSettings1, "file settings should be the same with global settings");
1933+
1934+
// set per file format options
1935+
const newPerFileSettings = clone(defaultSettings);
1936+
newPerFileSettings.insertSpaceAfterCommaDelimiter = !newPerFileSettings.insertSpaceAfterCommaDelimiter;
1937+
projectService.setHostConfiguration({ formatOptions: newPerFileSettings, file: f1.path });
1938+
1939+
// get format options for file - should be equal to new per-file settings
1940+
const s2 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
1941+
assert.deepEqual(s2, newPerFileSettings, "file settings should be the same with per-file settings");
1942+
1943+
// set new global settings - they should not affect ones that were set per-file
1944+
const newGlobalSettings2 = clone(defaultSettings);
1945+
newGlobalSettings2.insertSpaceAfterSemicolonInForStatements = !newGlobalSettings2.insertSpaceAfterSemicolonInForStatements;
1946+
projectService.setHostConfiguration({ formatOptions: newGlobalSettings2 });
1947+
1948+
// get format options for file - should be equal to new per-file settings
1949+
const s3 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
1950+
assert.deepEqual(s3, newPerFileSettings, "file settings should still be the same with per-file settings");
1951+
});
1952+
});
19121953
}

src/server/editorServices.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,14 @@ namespace ts.server {
290290
}
291291

292292
getFormatCodeOptions(file?: NormalizedPath) {
293+
let formatCodeSettings: FormatCodeSettings;
293294
if (file) {
294295
const info = this.getScriptInfoForNormalizedPath(file);
295296
if (info) {
296-
return info.formatCodeSettings;
297+
formatCodeSettings = info.getFormatCodeSettings();
297298
}
298299
}
299-
return this.hostConfiguration.formatCodeOptions;
300+
return formatCodeSettings || this.hostConfiguration.formatCodeOptions;
300301
}
301302

302303
private updateProjectGraphs(projects: Project[]) {
@@ -969,7 +970,6 @@ namespace ts.server {
969970
}
970971
if (content !== undefined) {
971972
info = new ScriptInfo(this.host, fileName, content, scriptKind, openedByClient, hasMixedContent);
972-
info.setFormatOptions(toEditorSettings(this.getFormatCodeOptions()));
973973
// do not watch files with mixed content - server doesn't know how to interpret it
974974
this.filenameToScriptInfo.set(info.path, info);
975975
if (!info.isOpen && !hasMixedContent) {

src/server/scriptInfo.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace ts.server {
77
* All projects that include this file
88
*/
99
readonly containingProjects: Project[] = [];
10-
readonly formatCodeSettings: ts.FormatCodeSettings;
10+
private formatCodeSettings: ts.FormatCodeSettings;
1111
readonly path: Path;
1212

1313
private fileWatcher: FileWatcher;
@@ -24,12 +24,15 @@ namespace ts.server {
2424

2525
this.path = toPath(fileName, host.getCurrentDirectory(), createGetCanonicalFileName(host.useCaseSensitiveFileNames));
2626
this.svc = ScriptVersionCache.fromString(host, content);
27-
this.formatCodeSettings = getDefaultFormatCodeSettings(this.host);
2827
this.scriptKind = scriptKind
2928
? scriptKind
3029
: getScriptKindFromFileName(fileName);
3130
}
3231

32+
getFormatCodeSettings() {
33+
return this.formatCodeSettings;
34+
}
35+
3336
attachToProject(project: Project): boolean {
3437
const isNew = !this.isAttached(project);
3538
if (isNew) {
@@ -90,6 +93,9 @@ namespace ts.server {
9093

9194
setFormatOptions(formatSettings: protocol.FormatOptions): void {
9295
if (formatSettings) {
96+
if (!this.formatCodeSettings) {
97+
this.formatCodeSettings = getDefaultFormatCodeSettings(this.host);
98+
}
9399
mergeMaps(this.formatCodeSettings, formatSettings);
94100
}
95101
}

0 commit comments

Comments
 (0)