defer settings format options on file until it is explicitly requested (#10971)

This commit is contained in:
Vladimir Matveev 2016-09-18 21:14:39 -07:00 committed by GitHub
parent 121b04ee36
commit 286a12edd1
3 changed files with 52 additions and 5 deletions

View file

@ -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");
});
});
}

View file

@ -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[]) {
@ -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) {

View file

@ -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;
@ -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) {
@ -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);
}
}