Skip to content

Commit 087aa4a

Browse files
authored
Merge pull request #7689 from dotnet/dev/jorobich/delay-miscfiles-toast
Delay the Misc Files warning toast.
2 parents 665ce64 + 6a4e321 commit 087aa4a

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

src/lsptoolshost/languageStatusBar.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ function combineDocumentSelectors(...selectors: vscode.DocumentSelector[]): vsco
2929

3030
class WorkspaceStatus {
3131
static createStatusItem(context: vscode.ExtensionContext, languageServerEvents: RoslynLanguageServerEvents) {
32-
const documentSelector = combineDocumentSelectors(
33-
languageServerOptions.documentSelector,
34-
RazorLanguage.documentSelector
35-
);
32+
const documentSelector = combineDocumentSelectors(languageServerOptions.documentSelector);
3633
const openSolutionCommand = {
3734
command: 'dotnet.openSolution',
3835
title: vscode.l10n.t('Open solution'),
@@ -79,9 +76,10 @@ class ProjectContextStatus {
7976
// Show a warning when the active file is part of the Miscellaneous File workspace and
8077
// project initialization is complete.
8178
if (languageServer.state === ServerState.ProjectInitializationComplete) {
82-
item.severity = e.context._vs_is_miscellaneous
83-
? vscode.LanguageStatusSeverity.Warning
84-
: vscode.LanguageStatusSeverity.Information;
79+
item.severity =
80+
e.context._vs_is_miscellaneous && e.isVerified
81+
? vscode.LanguageStatusSeverity.Warning
82+
: vscode.LanguageStatusSeverity.Information;
8583
} else {
8684
item.severity = vscode.LanguageStatusSeverity.Information;
8785
}

src/lsptoolshost/miscellaneousFileNotifier.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function registerMiscellaneousFileNotifier(
2121
// Only warn for C# miscellaneous files when the workspace is fully initialized.
2222
if (
2323
e.languageId !== 'csharp' ||
24+
!e.isVerified ||
2425
!e.context._vs_is_miscellaneous ||
2526
languageServer.state !== ServerState.ProjectInitializationComplete
2627
) {
@@ -39,10 +40,10 @@ export function registerMiscellaneousFileNotifier(
3940
const hash = createHash(e.uri.toString(/*skipEncoding:*/ true));
4041
if (NotifiedDocuments.has(hash)) {
4142
return;
42-
} else {
43-
NotifiedDocuments.add(hash);
4443
}
4544

45+
NotifiedDocuments.add(hash);
46+
4647
const message = vscode.l10n.t(
4748
'The active document is not part of the open workspace. Not all language features will be available.'
4849
);

src/lsptoolshost/services/projectContextService.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ export interface ProjectContextChangeEvent {
1515
languageId: string;
1616
uri: vscode.Uri;
1717
context: VSProjectContext;
18+
isVerified: boolean;
1819
}
1920

21+
const VerificationDelay = 2 * 1000;
22+
23+
let _verifyTimeout: NodeJS.Timeout | undefined;
24+
let _documentUriToVerify: vscode.Uri | undefined;
25+
2026
export class ProjectContextService {
2127
private readonly _contextChangeEmitter = new vscode.EventEmitter<ProjectContextChangeEvent>();
2228
private _source = new vscode.CancellationTokenSource();
@@ -47,7 +53,7 @@ export class ProjectContextService {
4753
public async refresh() {
4854
const textEditor = vscode.window.activeTextEditor;
4955
const languageId = textEditor?.document?.languageId;
50-
if (languageId !== 'csharp' && languageId !== 'aspnetcorerazor') {
56+
if (languageId !== 'csharp') {
5157
return;
5258
}
5359

@@ -57,19 +63,55 @@ export class ProjectContextService {
5763

5864
const uri = textEditor!.document.uri;
5965

66+
// Whether we have refreshed the active document's project context.
67+
let isVerifyPass = false;
68+
69+
if (_verifyTimeout) {
70+
// If we have changed active document then do not verify the previous one.
71+
clearTimeout(_verifyTimeout);
72+
_verifyTimeout = undefined;
73+
}
74+
75+
if (_documentUriToVerify) {
76+
if (uri.toString() === _documentUriToVerify.toString()) {
77+
// We have rerequested project contexts for the active document
78+
// and we can now notify if the document isn't part of the workspace.
79+
isVerifyPass = true;
80+
}
81+
82+
_documentUriToVerify = undefined;
83+
}
84+
6085
if (!this._languageServer.isRunning()) {
61-
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext });
86+
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext, isVerified: false });
6287
return;
6388
}
6489

6590
const contextList = await this.getProjectContexts(uri, this._source.token);
6691
if (!contextList) {
67-
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext });
92+
this._contextChangeEmitter.fire({ languageId, uri, context: this._emptyProjectContext, isVerified: false });
6893
return;
6994
}
7095

7196
const context = contextList._vs_projectContexts[contextList._vs_defaultIndex];
72-
this._contextChangeEmitter.fire({ languageId, uri, context });
97+
const isVerified = !context._vs_is_miscellaneous || isVerifyPass;
98+
this._contextChangeEmitter.fire({ languageId, uri, context, isVerified });
99+
100+
if (context._vs_is_miscellaneous && !isVerifyPass) {
101+
// Request the active project context be refreshed but delay the request to give
102+
// time for the project system to update with new files.
103+
_verifyTimeout = setTimeout(() => {
104+
_verifyTimeout = undefined;
105+
_documentUriToVerify = uri;
106+
107+
// Trigger a refresh, but don't block on refresh completing.
108+
this.refresh().catch((e) => {
109+
throw new Error(`Error refreshing project context status ${e}`);
110+
});
111+
}, VerificationDelay);
112+
113+
return;
114+
}
73115
}
74116

75117
private async getProjectContexts(

0 commit comments

Comments
 (0)