@@ -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+
2026export 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