@@ -101,13 +101,7 @@ module ts.server {
101101 }
102102
103103 getScriptFileNames ( ) {
104- var filenames : string [ ] = [ ] ;
105- for ( var filename in this . filenameToScript ) {
106- if ( this . filenameToScript [ filename ] && this . filenameToScript [ filename ] . isOpen ) {
107- filenames . push ( filename ) ;
108- }
109- }
110- return filenames ;
104+ return this . roots . map ( root => root . fileName ) ;
111105 }
112106
113107 getScriptVersion ( filename : string ) {
@@ -536,15 +530,35 @@ module ts.server {
536530 updateProjectStructure ( ) {
537531 this . log ( "updating project structure from ..." , "Info" ) ;
538532 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+
539+ var openFilesReferenced : ScriptInfo [ ] = [ ] ;
540+ var unattachedOpenFiles : ScriptInfo [ ] = [ ] ;
541+
539542 for ( var i = 0 , len = this . openFilesReferenced . length ; i < len ; i ++ ) {
540- var refdFile = this . openFilesReferenced [ i ] ;
541- refdFile . defaultProject . updateGraph ( ) ;
542- var sourceFile = refdFile . defaultProject . getSourceFile ( refdFile ) ;
543- if ( ! sourceFile ) {
544- this . openFilesReferenced = copyListRemovingItem ( refdFile , this . openFilesReferenced ) ;
545- this . addOpenFile ( refdFile ) ;
543+ var referencedFile = this . openFilesReferenced [ i ] ;
544+ referencedFile . defaultProject . updateGraph ( ) ;
545+ var sourceFile = referencedFile . defaultProject . getSourceFile ( referencedFile ) ;
546+ if ( sourceFile ) {
547+ openFilesReferenced . push ( referencedFile ) ;
548+ }
549+ else {
550+ unattachedOpenFiles . push ( referencedFile ) ;
546551 }
547552 }
553+ 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.
548562 var openFileRoots : ScriptInfo [ ] = [ ] ;
549563 for ( var i = 0 , len = this . openFileRoots . length ; i < len ; i ++ ) {
550564 var rootFile = this . openFileRoots [ i ] ;
@@ -555,12 +569,19 @@ module ts.server {
555569 openFileRoots . push ( rootFile ) ;
556570 }
557571 else {
558- // remove project from inferred projects list
572+ // remove project from inferred projects list because root captured
559573 this . inferredProjects = copyListRemovingItem ( rootedProject , this . inferredProjects ) ;
560574 this . openFilesReferenced . push ( rootFile ) ;
561575 }
562576 }
563577 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.
582+ for ( var i = 0 , len = unattachedOpenFiles . length ; i < len ; i ++ ) {
583+ this . addOpenFile ( unattachedOpenFiles [ i ] ) ;
584+ }
564585 this . printProjects ( ) ;
565586 }
566587
0 commit comments