Skip to content

Commit 6a4e321

Browse files
committed
Delay the Misc Files warning toast.
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.
1 parent cef8fbf commit 6a4e321

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/lsptoolshost/languageStatusBar.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ class ProjectContextStatus {
7676
// Show a warning when the active file is part of the Miscellaneous File workspace and
7777
// project initialization is complete.
7878
if (languageServer.state === ServerState.ProjectInitializationComplete) {
79-
item.severity = e.context._vs_is_miscellaneous
80-
? vscode.LanguageStatusSeverity.Warning
81-
: vscode.LanguageStatusSeverity.Information;
79+
item.severity =
80+
e.context._vs_is_miscellaneous && e.isVerified
81+
? vscode.LanguageStatusSeverity.Warning
82+
: vscode.LanguageStatusSeverity.Information;
8283
} else {
8384
item.severity = vscode.LanguageStatusSeverity.Information;
8485
}

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: 45 additions & 3 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();
@@ -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)