Skip to content

Commit 42bc64b

Browse files
committed
Merge pull request microsoft#2125 from Microsoft/updateProjectStructure
Update project structure on idle after change
2 parents 0162330 + 68049ea commit 42bc64b

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed

src/server/editorServices.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/server/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ module ts.server {
206206
}
207207

208208
};
209-
209+
var ioSession = new IOSession(ts.sys, logger);
210+
process.on('uncaughtException', function(err: Error) {
211+
ioSession.logError(err, "unknown");
212+
});
210213
// Start listening
211-
new IOSession(ts.sys, logger).listen();
214+
ioSession.listen();
212215
}

src/server/session.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,29 @@ module ts.server {
181181
}
182182

183183
semanticCheck(file: string, project: Project) {
184-
var diags = project.compilerService.languageService.getSemanticDiagnostics(file);
185-
if (diags) {
186-
var bakedDiags = diags.map((diag) => formatDiag(file, project, diag));
187-
this.event({ file: file, diagnostics: bakedDiags }, "semanticDiag");
184+
try {
185+
var diags = project.compilerService.languageService.getSemanticDiagnostics(file);
186+
187+
if (diags) {
188+
var bakedDiags = diags.map((diag) => formatDiag(file, project, diag));
189+
this.event({ file: file, diagnostics: bakedDiags }, "semanticDiag");
190+
}
191+
}
192+
catch (err) {
193+
this.logError(err, "semantic check");
188194
}
189195
}
190196

191197
syntacticCheck(file: string, project: Project) {
192-
var diags = project.compilerService.languageService.getSyntacticDiagnostics(file);
193-
if (diags) {
194-
var bakedDiags = diags.map((diag) => formatDiag(file, project, diag));
195-
this.event({ file: file, diagnostics: bakedDiags }, "syntaxDiag");
198+
try {
199+
var diags = project.compilerService.languageService.getSyntacticDiagnostics(file);
200+
if (diags) {
201+
var bakedDiags = diags.map((diag) => formatDiag(file, project, diag));
202+
this.event({ file: file, diagnostics: bakedDiags }, "syntaxDiag");
203+
}
204+
}
205+
catch (err) {
206+
this.logError(err, "syntactic check");
196207
}
197208
}
198209

@@ -553,10 +564,7 @@ module ts.server {
553564
compilerService.host.editScript(file, start, end, insertString);
554565
this.changeSeq++;
555566
}
556-
// update project structure on idle commented out
557-
// until we can have the host return only the root files
558-
// from getScriptFileNames()
559-
//this.updateProjectStructure(this.changeSeq, (n) => n == this.changeSeq);
567+
this.updateProjectStructure(this.changeSeq, (n) => n == this.changeSeq);
560568
}
561569
}
562570

0 commit comments

Comments
 (0)