Skip to content

Commit b6c6cbe

Browse files
author
Keen Yee Liau
committed
feat: Disable language service for non-Angular projects
Disable the langauge service for a particular project if it is not an Angular project. The detection mechanism relies on the presence of `@angular/core/core.d.ts` file in the program. PR closes #391
1 parent d0b257d commit b6c6cbe

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

server/src/session.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,17 @@ export class Session {
8181
this.connection.sendNotification(
8282
projectLoadingNotification.start, event.data.project.projectName);
8383
break;
84-
case ts.server.ProjectLoadingFinishEvent:
84+
case ts.server.ProjectLoadingFinishEvent: {
85+
const {project} = event.data;
86+
if (!isAngularProject(project)) {
87+
project.disableLanguageService();
88+
this.connection.console.info(`Disabling language service for ${
89+
project.projectName} because it is not an Angular project.`);
90+
}
8591
this.connection.sendNotification(
8692
projectLoadingNotification.finish, event.data.project.projectName);
8793
break;
94+
}
8895
case ts.server.ProjectsUpdatedInBackgroundEvent:
8996
// ProjectsUpdatedInBackgroundEvent is sent whenever diagnostics are
9097
// requested via project.refreshDiagnostics()
@@ -159,7 +166,6 @@ export class Session {
159166
this.connection.console.error(`Failed to find project for ${filePath}`);
160167
return;
161168
}
162-
project.markAsDirty(); // Must mark project as dirty to rebuild the program.
163169
project.refreshDiagnostics(); // Show initial diagnostics
164170
}
165171

@@ -373,3 +379,20 @@ export class Session {
373379
this.connection.listen();
374380
}
375381
}
382+
383+
/**
384+
* Return true if the specified `project` contains the Angular core declaration.
385+
* @param project
386+
*/
387+
function isAngularProject(project: ts.server.Project): boolean {
388+
project.markAsDirty(); // Must mark project as dirty to rebuild the program.
389+
if (project.isNonTsProject()) {
390+
return false;
391+
}
392+
for (const fileName of project.getFileNames()) {
393+
if (fileName.endsWith('@angular/core/core.d.ts')) {
394+
return true;
395+
}
396+
}
397+
return false;
398+
}

0 commit comments

Comments
 (0)