Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay the Misc Files warning toast. #7689

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/lsptoolshost/languageStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ function combineDocumentSelectors(...selectors: vscode.DocumentSelector[]): vsco

class WorkspaceStatus {
static createStatusItem(context: vscode.ExtensionContext, languageServerEvents: RoslynLanguageServerEvents) {
const documentSelector = combineDocumentSelectors(
languageServerOptions.documentSelector,
RazorLanguage.documentSelector
);
const documentSelector = combineDocumentSelectors(languageServerOptions.documentSelector);
const openSolutionCommand = {
command: 'dotnet.openSolution',
title: vscode.l10n.t('Open solution'),
Expand Down Expand Up @@ -79,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
50 changes: 46 additions & 4 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 @@ -47,7 +53,7 @@ export class ProjectContextService {
public async refresh() {
const textEditor = vscode.window.activeTextEditor;
const languageId = textEditor?.document?.languageId;
if (languageId !== 'csharp' && languageId !== 'aspnetcorerazor') {
if (languageId !== 'csharp') {
return;
}

Expand All @@ -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
Loading