Skip to content

Commit

Permalink
Delay the Misc Files warning toast.
Browse files Browse the repository at this point in the history
Give the project system time to update with any newly added or renamed files. Then, refresh the active document's project context before displaying a warning toast.
  • Loading branch information
JoeRobich committed Oct 25, 2024
1 parent cef8fbf commit 6a4e321
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/lsptoolshost/languageStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ class ProjectContextStatus {
// Show a warning when the active file is part of the Miscellaneous File workspace and
// project initialization is complete.
if (languageServer.state === ServerState.ProjectInitializationComplete) {
item.severity = e.context._vs_is_miscellaneous
? vscode.LanguageStatusSeverity.Warning
: vscode.LanguageStatusSeverity.Information;
item.severity =
e.context._vs_is_miscellaneous && e.isVerified
? vscode.LanguageStatusSeverity.Warning
: vscode.LanguageStatusSeverity.Information;
} else {
item.severity = vscode.LanguageStatusSeverity.Information;
}
Expand Down
5 changes: 3 additions & 2 deletions src/lsptoolshost/miscellaneousFileNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function registerMiscellaneousFileNotifier(
// Only warn for C# miscellaneous files when the workspace is fully initialized.
if (
e.languageId !== 'csharp' ||
!e.isVerified ||
!e.context._vs_is_miscellaneous ||
languageServer.state !== ServerState.ProjectInitializationComplete
) {
Expand All @@ -39,10 +40,10 @@ export function registerMiscellaneousFileNotifier(
const hash = createHash(e.uri.toString(/*skipEncoding:*/ true));
if (NotifiedDocuments.has(hash)) {
return;
} else {
NotifiedDocuments.add(hash);
}

NotifiedDocuments.add(hash);

const message = vscode.l10n.t(
'The active document is not part of the open workspace. Not all language features will be available.'
);
Expand Down
48 changes: 45 additions & 3 deletions src/lsptoolshost/services/projectContextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ export interface ProjectContextChangeEvent {
languageId: string;
uri: vscode.Uri;
context: VSProjectContext;
isVerified: boolean;
}

const VerificationDelay = 2 * 1000;

let _verifyTimeout: NodeJS.Timeout | undefined;
let _documentUriToVerify: vscode.Uri | undefined;

export class ProjectContextService {
private readonly _contextChangeEmitter = new vscode.EventEmitter<ProjectContextChangeEvent>();
private _source = new vscode.CancellationTokenSource();
Expand Down Expand Up @@ -57,19 +63,55 @@ export class ProjectContextService {

const uri = textEditor!.document.uri;

// Whether we have refreshed the active document's project context.
let isVerifyPass = false;

if (_verifyTimeout) {
// If we have changed active document then do not verify the previous one.
clearTimeout(_verifyTimeout);
_verifyTimeout = undefined;
}

if (_documentUriToVerify) {
if (uri.toString() === _documentUriToVerify.toString()) {
// We have rerequested project contexts for the active document
// and we can now notify if the document isn't part of the workspace.
isVerifyPass = true;
}

_documentUriToVerify = undefined;
}

if (!this._languageServer.isRunning()) {
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext });
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext, isVerified: false });
return;
}

const contextList = await this.getProjectContexts(uri, this._source.token);
if (!contextList) {
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext });
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext, isVerified: false });
return;
}

const context = contextList._vs_projectContexts[contextList._vs_defaultIndex];
this._contextChangeEmitter.fire({ languageId, uri, context });
const isVerified = !context._vs_is_miscellaneous || isVerifyPass;
this._contextChangeEmitter.fire({ languageId, uri, context, isVerified });

if (context._vs_is_miscellaneous && !isVerifyPass) {
// Request the active project context be refreshed but delay the request to give
// time for the project system to update with new files.
_verifyTimeout = setTimeout(() => {
_verifyTimeout = undefined;
_documentUriToVerify = uri;

// Trigger a refresh, but don't block on refresh completing.
this.refresh().catch((e) => {
throw new Error(`Error refreshing project context status ${e}`);
});
}, VerificationDelay);

return;
}
}

private async getProjectContexts(
Expand Down

0 comments on commit 6a4e321

Please sign in to comment.