@@ -160,6 +160,14 @@ namespace ts.server {
160160 this . roots . push ( info ) ;
161161 }
162162 }
163+
164+ removeRoot ( info : ScriptInfo ) {
165+ var scriptInfo = ts . lookUp ( this . filenameToScript , info . fileName ) ;
166+ if ( scriptInfo ) {
167+ this . filenameToScript [ info . fileName ] = undefined ;
168+ this . roots = copyListRemovingItem ( info , this . roots ) ;
169+ }
170+ }
163171
164172 saveTo ( filename : string , tmpfilename : string ) {
165173 var script = this . getScriptInfo ( filename ) ;
@@ -289,7 +297,7 @@ namespace ts.server {
289297 openRefCount = 0 ;
290298
291299 constructor ( public projectService : ProjectService , public projectOptions ?: ProjectOptions ) {
292- this . compilerService = new CompilerService ( this , projectOptions && projectOptions . compilerOptions ) ;
300+ this . compilerService = new CompilerService ( this , projectOptions && projectOptions . compilerOptions ) ;
293301 }
294302
295303 addOpenRef ( ) {
@@ -360,6 +368,12 @@ namespace ts.server {
360368 info . defaultProject = this ;
361369 this . compilerService . host . addRoot ( info ) ;
362370 }
371+
372+ // remove a root file from project
373+ removeRoot ( info : ScriptInfo ) {
374+ info . defaultProject = undefined ;
375+ this . compilerService . host . removeRoot ( info ) ;
376+ }
363377
364378 filesToString ( ) {
365379 var strBuilder = "" ;
@@ -692,13 +706,13 @@ namespace ts.server {
692706 this . log ( "updating project structure from ..." , "Info" ) ;
693707 this . printProjects ( ) ;
694708
695- let openFileRootsConfigured : ScriptInfo [ ] ;
709+ let openFileRootsConfigured : ScriptInfo [ ] = [ ] ;
696710 for ( let info of this . openFileRootsConfigured ) {
697- let configFileName = info . defaultProject . projectFilename ;
698- let project = this . findConfiguredProjectByConfigFile ( configFileName ) ;
711+ let project = info . defaultProject ;
699712 if ( ! project || ! ( project . getSourceFile ( info ) ) ) {
700713 info . defaultProject = undefined ;
701714 this . createInferredProject ( info ) ;
715+ this . openFileRoots . push ( info ) ;
702716 }
703717 else {
704718 openFileRootsConfigured . push ( info ) ;
@@ -1009,6 +1023,35 @@ namespace ts.server {
10091023 return error ;
10101024 }
10111025 else {
1026+ let oldFileNames = project . compilerService . host . roots . map ( info => info . fileName ) ;
1027+ let newFileNames = projectOptions . files ;
1028+ let fileNamesToRemove = oldFileNames . filter ( f => newFileNames . indexOf ( f ) < 0 ) ;
1029+ let fileNamesToAdd = newFileNames . filter ( f => oldFileNames . indexOf ( f ) < 0 ) ;
1030+
1031+ for ( let fileName of fileNamesToRemove ) {
1032+ let info = this . getScriptInfo ( fileName ) ;
1033+ project . removeRoot ( info ) ;
1034+ }
1035+
1036+ for ( let fileName of fileNamesToAdd ) {
1037+ let info = this . getScriptInfo ( fileName ) ;
1038+ if ( ! info ) {
1039+ info = this . openFile ( fileName , false ) ;
1040+ }
1041+ else {
1042+ // if the root file was opened by client, it would belong to either
1043+ // openFileRoots or openFileReferenced.
1044+ if ( this . openFileRoots . indexOf ( info ) >= 0 ) {
1045+ this . openFileRoots = copyListRemovingItem ( info , this . openFileRoots ) ;
1046+ }
1047+ if ( this . openFilesReferenced . indexOf ( info ) >= 0 ) {
1048+ this . openFilesReferenced = copyListRemovingItem ( info , this . openFilesReferenced ) ;
1049+ }
1050+ this . openFileRootsConfigured . push ( info ) ;
1051+ }
1052+ project . addRoot ( info ) ;
1053+ }
1054+
10121055 project . setProjectOptions ( projectOptions ) ;
10131056 project . finishGraph ( ) ;
10141057 }
0 commit comments