Skip to content

Commit bcdb06c

Browse files
committed
introduce methods that allow to get project without refreshing inferred projects
1 parent aea1534 commit bcdb06c

2 files changed

Lines changed: 35 additions & 23 deletions

File tree

src/server/editorServices.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,24 @@ namespace ts.server {
172172
return undefined;
173173
}
174174
if (isInferredProjectName(projectName)) {
175+
this.ensureInferredProjectsUpToDate();
175176
return findProjectByName(projectName, this.inferredProjects);
176177
}
177178
return this.findExternalProjectByProjectName(projectName) || this.findConfiguredProjectByProjectName(toNormalizedPath(projectName));
178179
}
179180

181+
getDefaultProjectForFile(fileName: NormalizedPath, refreshInferredProjects: boolean) {
182+
if (refreshInferredProjects) {
183+
this.ensureInferredProjectsUpToDate();
184+
}
185+
const scriptInfo = this.getScriptInfoForNormalizedPath(fileName);
186+
return scriptInfo && scriptInfo.getDefaultProject();
187+
}
188+
189+
private ensureInferredProjectsUpToDate() {
190+
191+
}
192+
180193
private findContainingConfiguredProject(info: ScriptInfo): ConfiguredProject {
181194
for (const proj of this.configuredProjects) {
182195
if (proj.containsScriptInfo(info)) {
@@ -941,11 +954,6 @@ namespace ts.server {
941954
this.printProjects();
942955
}
943956

944-
getDefaultProjectForFile(fileName: NormalizedPath) {
945-
const scriptInfo = this.getScriptInfoForNormalizedPath(fileName);
946-
return scriptInfo && scriptInfo.getDefaultProject();
947-
}
948-
949957
private collectChanges(lastKnownProjectVersions: protocol.ProjectVersionInfo[], currentProjects: Project[], result: protocol.ProjectFiles[]): void {
950958
for (const proj of currentProjects) {
951959
const knownProject = forEach(lastKnownProjectVersions, p => p.projectName === proj.getProjectName() && p);

src/server/session.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ namespace ts.server {
505505
}
506506

507507
private getProjectInfoWorker(uncheckedFileName: string, projectFileName: string, needFileNameList: boolean) {
508-
const { file, project } = this.getFileAndProjectWorker(uncheckedFileName, projectFileName, /*errorOnMissingProject*/ true);
508+
const { file, project } = this.getFileAndProjectWorker(uncheckedFileName, projectFileName, /*refreshInferredProjects*/ true, /*errorOnMissingProject*/ true);
509509
const projectInfo = {
510510
configFileName: project.getProjectName(),
511511
languageServiceDisabled: !project.languageServiceEnabled,
@@ -730,20 +730,24 @@ namespace ts.server {
730730
}
731731

732732
private getFileAndProject(args: protocol.FileRequestArgs, errorOnMissingProject = true) {
733-
return this.getFileAndProjectWorker(args.file, args.projectFileName, errorOnMissingProject);
733+
return this.getFileAndProjectWorker(args.file, args.projectFileName, /*refreshInferredProjects*/ true, errorOnMissingProject);
734734
}
735735

736-
private getFileAndProjectWorker(uncheckedFileName: string, projectFileName: string, errorOnMissingProject: boolean) {
736+
private getFileAndProjectWithoutRefreshingInferredProjects(args: protocol.FileRequestArgs, errorOnMissingProject = true) {
737+
return this.getFileAndProjectWorker(args.file, args.projectFileName, /*refreshInferredProjects*/ false, errorOnMissingProject);
738+
}
739+
740+
private getFileAndProjectWorker(uncheckedFileName: string, projectFileName: string, refreshInferredProjects: boolean, errorOnMissingProject: boolean) {
737741
const file = toNormalizedPath(uncheckedFileName);
738-
const project: Project = this.getProject(projectFileName) || this.projectService.getDefaultProjectForFile(file);
742+
const project: Project = this.getProject(projectFileName) || this.projectService.getDefaultProjectForFile(file, refreshInferredProjects);
739743
if (!project && errorOnMissingProject) {
740744
throw Errors.NoProject;
741745
}
742746
return { file, project };
743747
}
744748

745749
private getOutliningSpans(args: protocol.FileRequestArgs) {
746-
const { file, project } = this.getFileAndProject(args);
750+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
747751
return project.languageService.getOutliningSpans(file);
748752
}
749753

@@ -753,34 +757,34 @@ namespace ts.server {
753757
}
754758

755759
private getDocCommentTemplate(args: protocol.FileLocationRequestArgs) {
756-
const { file, project } = this.getFileAndProject(args);
760+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
757761
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
758762
const position = this.getPosition(args, scriptInfo);
759763
return project.languageService.getDocCommentTemplateAtPosition(file, position);
760764
}
761765

762766
private getIndentation(args: protocol.IndentationRequestArgs) {
763-
const { file, project } = this.getFileAndProject(args);
767+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
764768
const position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file));
765769
const options = args.options || this.projectService.getFormatCodeOptions(file);
766770
const indentation = project.languageService.getIndentationAtPosition(file, position, options);
767771
return { position, indentation };
768772
}
769773

770774
private getBreakpointStatement(args: protocol.FileLocationRequestArgs) {
771-
const { file, project } = this.getFileAndProject(args);
775+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
772776
const position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file));
773777
return project.languageService.getBreakpointStatementAtPosition(file, position);
774778
}
775779

776780
private getNameOrDottedNameSpan(args: protocol.FileLocationRequestArgs) {
777-
const { file, project } = this.getFileAndProject(args);
781+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
778782
const position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file));
779783
return project.languageService.getNameOrDottedNameSpan(file, position, position);
780784
}
781785

782786
private isValidBraceCompletion(args: protocol.BraceCompletionRequestArgs) {
783-
const { file, project } = this.getFileAndProject(args);
787+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
784788
const position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file));
785789
return project.languageService.isValidBraceCompletionAtPosition(file, position, args.openingBrace.charCodeAt(0));
786790
}
@@ -811,7 +815,7 @@ namespace ts.server {
811815
}
812816

813817
private getFormattingEditsForRange(args: protocol.FormatRequestArgs): protocol.CodeEdit[] {
814-
const { file, project } = this.getFileAndProject(args);
818+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
815819
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
816820

817821
const startPosition = scriptInfo.lineOffsetToPosition(args.line, args.offset);
@@ -834,25 +838,25 @@ namespace ts.server {
834838
}
835839

836840
private getFormattingEditsForRangeFull(args: protocol.FormatRequestArgs) {
837-
const { file, project } = this.getFileAndProject(args);
841+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
838842
const options = args.options || this.projectService.getFormatCodeOptions(file);
839843
return project.languageService.getFormattingEditsForRange(file, args.position, args.endPosition, options);
840844
}
841845

842846
private getFormattingEditsForDocumentFull(args: protocol.FormatRequestArgs) {
843-
const { file, project } = this.getFileAndProject(args);
847+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
844848
const options = args.options || this.projectService.getFormatCodeOptions(file);
845849
return project.languageService.getFormattingEditsForDocument(file, options);
846850
}
847851

848852
private getFormattingEditsAfterKeystrokeFull(args: protocol.FormatOnKeyRequestArgs) {
849-
const { file, project } = this.getFileAndProject(args);
853+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
850854
const options = args.options || this.projectService.getFormatCodeOptions(file);
851855
return project.languageService.getFormattingEditsAfterKeystroke(file, args.position, args.key, options);
852856
}
853857

854858
private getFormattingEditsAfterKeystroke(args: protocol.FormatOnKeyRequestArgs): protocol.CodeEdit[] {
855-
const { file, project } = this.getFileAndProject(args);
859+
const { file, project } = this.getFileAndProjectWithoutRefreshingInferredProjects(args);
856860
const scriptInfo = project.getScriptInfoForNormalizedPath(file);
857861
const position = scriptInfo.lineOffsetToPosition(args.line, args.offset);
858862
const formatOptions = this.projectService.getFormatCodeOptions(file);
@@ -976,7 +980,7 @@ namespace ts.server {
976980
private getDiagnostics(delay: number, fileNames: string[]) {
977981
const checkList = fileNames.reduce((accum: PendingErrorCheck[], uncheckedFileName: string) => {
978982
const fileName = toNormalizedPath(uncheckedFileName);
979-
const project = this.projectService.getDefaultProjectForFile(fileName);
983+
const project = this.projectService.getDefaultProjectForFile(fileName, /*refreshInferredProjects*/ true);
980984
if (project) {
981985
accum.push({ fileName, project });
982986
}
@@ -1004,7 +1008,7 @@ namespace ts.server {
10041008

10051009
private reload(args: protocol.ReloadRequestArgs, reqSeq: number) {
10061010
const file = toNormalizedPath(args.file);
1007-
const project = this.projectService.getDefaultProjectForFile(file);
1011+
const project = this.projectService.getDefaultProjectForFile(file, /*refreshInferredProjects*/ true);
10081012
if (project) {
10091013
this.changeSeq++;
10101014
// make sure no changes happen before this one is finished
@@ -1177,7 +1181,7 @@ namespace ts.server {
11771181
const lowPriorityFiles: NormalizedPath[] = [];
11781182
const veryLowPriorityFiles: NormalizedPath[] = [];
11791183
const normalizedFileName = toNormalizedPath(fileName);
1180-
const project = this.projectService.getDefaultProjectForFile(normalizedFileName);
1184+
const project = this.projectService.getDefaultProjectForFile(normalizedFileName, /*refreshInferredProjects*/ true);
11811185
for (const fileNameInProject of fileNamesInProject) {
11821186
if (this.getCanonicalFileName(fileNameInProject) == this.getCanonicalFileName(fileName))
11831187
highPriorityFiles.push(fileNameInProject);

0 commit comments

Comments
 (0)