Change the design to track addRoot and removeRoot

This commit is contained in:
zhengbli
2016-12-29 17:00:04 -08:00
parent 09fc3b3a18
commit 8ac22ecbb0
4 changed files with 66 additions and 17 deletions

View File

@@ -1072,10 +1072,6 @@ namespace ts.server {
? this.inferredProjects[0]
: new InferredProject(this, this.documentRegistry, this.compilerOptionsForInferredProjects);
if (root.scriptKind === ScriptKind.JS || root.scriptKind === ScriptKind.JSX) {
project.setAsJsInferredProject();
}
project.addRoot(root);
this.directoryWatchers.startWatchingContainingDirectoriesForFile(

View File

@@ -395,7 +395,9 @@ namespace ts.server {
}
removeFile(info: ScriptInfo, detachFromProject = true) {
this.removeRootFileIfNecessary(info);
if (this.isRoot(info)) {
this.removeRoot(info);
}
this.lsHost.notifyFileRemoved(info);
this.cachedUnresolvedImportsPerFile.remove(info.path);
@@ -693,11 +695,9 @@ namespace ts.server {
}
// remove a root file from project
private removeRootFileIfNecessary(info: ScriptInfo): void {
if (this.isRoot(info)) {
remove(this.rootFiles, info);
this.rootFilesMap.remove(info.path);
}
protected removeRoot(info: ScriptInfo): void {
remove(this.rootFiles, info);
this.rootFilesMap.remove(info.path);
}
}
@@ -714,13 +714,16 @@ namespace ts.server {
private _isJsInferredProject = false;
setAsJsInferredProject() {
this._isJsInferredProject = true;
this.setCompilerOptions();
toggleJsInferredProject(isJsInferredProject: boolean) {
if (isJsInferredProject !== this._isJsInferredProject) {
this._isJsInferredProject = isJsInferredProject;
this.setCompilerOptions();
}
}
setCompilerOptions(newOptions?: CompilerOptions) {
newOptions = newOptions ? newOptions : this.getCompilerOptions();
setCompilerOptions(options?: CompilerOptions) {
// Avoid manipulating the given options directly
const newOptions = options ? clone(options) : this.getCompilerOptions();
if (!newOptions) {
return;
}
@@ -728,6 +731,9 @@ namespace ts.server {
if (this._isJsInferredProject && typeof newOptions.maxNodeModuleJsDepth !== "number") {
newOptions.maxNodeModuleJsDepth = 2;
}
else if (!this._isJsInferredProject) {
newOptions.maxNodeModuleJsDepth = undefined;
}
newOptions.allowJs = true;
super.setCompilerOptions(newOptions);
}
@@ -746,6 +752,22 @@ namespace ts.server {
/*compileOnSaveEnabled*/ false);
}
addRoot(info: ScriptInfo) {
if (!this._isJsInferredProject && info.isJavaScript()) {
this.toggleJsInferredProject(/*isJsInferredProject*/ true);
}
super.addRoot(info);
}
removeRoot(info: ScriptInfo) {
if (this._isJsInferredProject && info.isJavaScript()) {
if (filter(this.getRootScriptInfos(), info => info.isJavaScript()).length === 0) {
this.toggleJsInferredProject(/*isJsInferredProject*/ false);
}
}
super.removeRoot(info);
}
getProjectRootPath() {
// Single inferred project does not have a project root.
if (this.projectService.useSingleInferredProject) {

View File

@@ -355,5 +355,9 @@ namespace ts.server {
positionToLineOffset(position: number): ILineInfo {
return this.textStorage.positionToLineOffset(position);
}
public isJavaScript() {
return this.scriptKind === ScriptKind.JS || this.scriptKind === ScriptKind.JSX;
}
}
}