Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1909,4 +1909,45 @@ namespace ts.projectSystem {
checkProjectActualFiles(projectService.configuredProjects[0], [f1.path, barTypings.path]);
});
});

describe("format settings", () => {
it("can be set globally", () => {
const f1 = {
path: "/a/b/app.ts",
content: "let x;"
};
const host = createServerHost([f1]);
const projectService = createProjectService(host);
projectService.openClientFile(f1.path);

const defaultSettings = projectService.getFormatCodeOptions();

// set global settings
const newGlobalSettings1 = clone(defaultSettings);
newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks = !newGlobalSettings1.placeOpenBraceOnNewLineForControlBlocks;
projectService.setHostConfiguration({ formatOptions: newGlobalSettings1 });

// get format options for file - should be equal to new global settings
const s1 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
assert.deepEqual(s1, newGlobalSettings1, "file settings should be the same with global settings");

// set per file format options
const newPerFileSettings = clone(defaultSettings);
newPerFileSettings.insertSpaceAfterCommaDelimiter = !newPerFileSettings.insertSpaceAfterCommaDelimiter;
projectService.setHostConfiguration({ formatOptions: newPerFileSettings, file: f1.path });

// get format options for file - should be equal to new per-file settings
const s2 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
assert.deepEqual(s2, newPerFileSettings, "file settings should be the same with per-file settings");

// set new global settings - they should not affect ones that were set per-file
const newGlobalSettings2 = clone(defaultSettings);
newGlobalSettings2.insertSpaceAfterSemicolonInForStatements = !newGlobalSettings2.insertSpaceAfterSemicolonInForStatements;
projectService.setHostConfiguration({ formatOptions: newGlobalSettings2 });

// get format options for file - should be equal to new per-file settings
const s3 = projectService.getFormatCodeOptions(server.toNormalizedPath(f1.path));
assert.deepEqual(s3, newPerFileSettings, "file settings should still be the same with per-file settings");
});
});
}
6 changes: 3 additions & 3 deletions src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,14 @@ namespace ts.server {
}

getFormatCodeOptions(file?: NormalizedPath) {
let formatCodeSettings: FormatCodeSettings;
if (file) {
const info = this.getScriptInfoForNormalizedPath(file);
if (info) {
return info.formatCodeSettings;
formatCodeSettings = info.getFormatCodeSettings();
}
}
return this.hostConfiguration.formatCodeOptions;
return formatCodeSettings || this.hostConfiguration.formatCodeOptions;
}

private updateProjectGraphs(projects: Project[]) {
Expand Down Expand Up @@ -969,7 +970,6 @@ namespace ts.server {
}
if (content !== undefined) {
info = new ScriptInfo(this.host, fileName, content, scriptKind, openedByClient, hasMixedContent);
info.setFormatOptions(toEditorSettings(this.getFormatCodeOptions()));
// do not watch files with mixed content - server doesn't know how to interpret it
this.filenameToScriptInfo.set(info.path, info);
if (!info.isOpen && !hasMixedContent) {
Expand Down
10 changes: 8 additions & 2 deletions src/server/scriptInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ts.server {
* All projects that include this file
*/
readonly containingProjects: Project[] = [];
readonly formatCodeSettings: ts.FormatCodeSettings;
private formatCodeSettings: ts.FormatCodeSettings;
readonly path: Path;

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

this.path = toPath(fileName, host.getCurrentDirectory(), createGetCanonicalFileName(host.useCaseSensitiveFileNames));
this.svc = ScriptVersionCache.fromString(host, content);
this.formatCodeSettings = getDefaultFormatCodeSettings(this.host);
this.scriptKind = scriptKind
? scriptKind
: getScriptKindFromFileName(fileName);
}

getFormatCodeSettings() {
return this.formatCodeSettings;
}

attachToProject(project: Project): boolean {
const isNew = !this.isAttached(project);
if (isNew) {
Expand Down Expand Up @@ -90,6 +93,9 @@ namespace ts.server {

setFormatOptions(formatSettings: protocol.FormatOptions): void {
if (formatSettings) {
if (!this.formatCodeSettings) {
this.formatCodeSettings = getDefaultFormatCodeSettings(this.host);
}
mergeMaps(this.formatCodeSettings, formatSettings);
}
}
Expand Down