@@ -530,8 +530,15 @@ module ts.server {
530530 updateProjectStructure ( ) {
531531 this . log ( "updating project structure from ..." , "Info" ) ;
532532 this . printProjects ( ) ;
533+
534+ // First loop through all open files that are referenced by projects but are not
535+ // project roots. For each referenced file, see if the default project still
536+ // references that file. If so, then just keep the file in the referenced list.
537+ // If not, add the file to an unattached list, to be rechecked later.
538+
533539 var openFilesReferenced : ScriptInfo [ ] = [ ] ;
534540 var unattachedOpenFiles : ScriptInfo [ ] = [ ] ;
541+
535542 for ( var i = 0 , len = this . openFilesReferenced . length ; i < len ; i ++ ) {
536543 var referencedFile = this . openFilesReferenced [ i ] ;
537544 referencedFile . defaultProject . updateGraph ( ) ;
@@ -544,6 +551,14 @@ module ts.server {
544551 }
545552 }
546553 this . openFilesReferenced = openFilesReferenced ;
554+
555+ // Then, loop through all of the open files that are project roots.
556+ // For each root file, note the project that it roots. Then see if
557+ // any other projects newly reference the file. If zero projects
558+ // newly reference the file, keep it as a root. If one or more
559+ // projects newly references the file, remove its project from the
560+ // inferred projects list (since it is no longer a root) and add
561+ // the file to the open, referenced file list.
547562 var openFileRoots : ScriptInfo [ ] = [ ] ;
548563 for ( var i = 0 , len = this . openFileRoots . length ; i < len ; i ++ ) {
549564 var rootFile = this . openFileRoots [ i ] ;
@@ -560,6 +575,10 @@ module ts.server {
560575 }
561576 }
562577 this . openFileRoots = openFileRoots ;
578+
579+ // Finally, if we found any open, referenced files that are no longer
580+ // referenced by their default project, treat them as newly opened
581+ // by the editor.
563582 for ( var i = 0 , len = unattachedOpenFiles . length ; i < len ; i ++ ) {
564583 this . addOpenFile ( unattachedOpenFiles [ i ] ) ;
565584 }
0 commit comments