Skip to content

Commit

Permalink
refactor updateErrorCheck to delay work again
Browse files Browse the repository at this point in the history
  • Loading branch information
gabritto committed Jun 13, 2024
1 parent ec6772c commit 14bc9f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ export function formatDiagnosticToProtocol(diag: Diagnostic, includeFileName: bo

interface PendingErrorCheck {
fileName: NormalizedPath;
ranges?: TextRange[];
project?: Project;
project: Project;
}

function allEditsBeforePos(edits: readonly TextChange[], pos: number): boolean {
Expand Down Expand Up @@ -1143,7 +1142,7 @@ export class Session<TMessage = string> implements EventSender {
if (!this.suppressDiagnosticEvents && !this.noGetErrOnBackgroundUpdate) {
this.projectService.logger.info(`Queueing diagnostics update for ${openFiles}`);
// For now only queue error checking for open files. We can change this to include non open files as well
this.errorCheck.startNew(next => this.updateErrorCheck(next, mapDefined(openFiles, file => ({ fileName: toNormalizedPath(file) })), 100, /*requireOpen*/ true));
this.errorCheck.startNew(next => this.updateErrorCheck(next, openFiles, 100, /*requireOpen*/ true));
}

// Send project changed event
Expand Down Expand Up @@ -1336,7 +1335,7 @@ export class Session<TMessage = string> implements EventSender {
/** It is the caller's responsibility to verify that `!this.suppressDiagnosticEvents`. */
private updateErrorCheck(
next: NextStep,
checkList: PendingErrorCheck[],
checkList: PendingErrorCheck[] | (string | protocol.FileRangesRequestArgs)[],
ms: number,
requireOpen = true,
) {
Expand All @@ -1363,8 +1362,7 @@ export class Session<TMessage = string> implements EventSender {
}

if (this.getPreferences(fileName).disableSuggestions) {
goNext();
return;
return goNext();
}
next.immediate("suggestionCheck", () => {
this.suggestionCheck(fileName, project);
Expand All @@ -1377,11 +1375,23 @@ export class Session<TMessage = string> implements EventSender {
return;
}

const { fileName, project = this.projectService.tryGetDefaultProjectForFile(fileName), ranges } = checkList[index];
if (!project) {
return;
let ranges: protocol.FileRange[] | undefined;
let item: string | protocol.FileRangesRequestArgs | PendingErrorCheck | undefined = checkList[index];
if (isString(item)) {
item = this.toPendingErrorCheck(item);
}
// eslint-disable-next-line local/no-in-operator
else if ("ranges" in item) {
ranges = item.ranges;
item = this.toPendingErrorCheck(item.file);
}

if (!item) {
return goNext();
}

const { fileName, project } = item;

// Ensure the project is up to date before checking if this file is present in the project.
updateProjectIfDirty(project);
if (!project.containsFile(fileName, requireOpen)) {
Expand All @@ -1395,13 +1405,16 @@ export class Session<TMessage = string> implements EventSender {

// Don't provide semantic diagnostics unless we're in full semantic mode.
if (project.projectService.serverMode !== LanguageServiceMode.Semantic) {
goNext();
return;
return goNext();
}

// const ranges = checkList[index].ranges;
if (ranges) {
return next.immediate("regionSemanticCheck", () => {
this.regionSemanticCheck(fileName, project, ranges);
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(fileName);
if (scriptInfo) {
this.regionSemanticCheck(fileName, project, ranges.map(range => this.getRange({ file: fileName, ...range }, scriptInfo)));
}
if (this.changeSeq !== seq) {
return;
}
Expand Down Expand Up @@ -2560,28 +2573,19 @@ export class Session<TMessage = string> implements EventSender {
}
}

private toPendingErrorCheck(uncheckedFileName: string): PendingErrorCheck | undefined {
const fileName = toNormalizedPath(uncheckedFileName);
const project = this.projectService.tryGetDefaultProjectForFile(fileName);
return project && { fileName, project };
}

private getDiagnostics(next: NextStep, delay: number, fileArgs: (string | protocol.FileRangesRequestArgs)[]): void {
if (this.suppressDiagnosticEvents) {
return;
}

if (fileArgs.length > 0) {
const files = mapDefined(fileArgs, fileArg => {
if (isString(fileArg)) {
return { fileName: toNormalizedPath(fileArg) };
}
const fileName = toNormalizedPath(fileArg.file);
const scriptInfo = this.projectService.getScriptInfo(fileName);
if (!scriptInfo) {
return undefined;
}
const ranges = fileArg.ranges.map(range => this.getRange({ file: fileArg.file, ...range }, scriptInfo));
return {
fileName,
ranges,
};
});
this.updateErrorCheck(next, files, delay);
this.updateErrorCheck(next, fileArgs, delay);
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3548,6 +3548,7 @@ declare namespace ts {
private getCompileOnSaveAffectedFileList;
private emitFile;
private getSignatureHelpItems;
private toPendingErrorCheck;
private getDiagnostics;
private change;
private reload;
Expand Down

0 comments on commit 14bc9f5

Please sign in to comment.