1- import { CompletionItem , Diagnostic , DiagnosticRelatedInformation , DiagnosticSeverity , DidChangeConfigurationParams , DidChangeWatchedFilesParams , DocumentSymbol , FoldingRange , Hover , HoverParams , Location , NotificationHandler , Position , Range , SemanticTokenModifiers , SemanticTokens , SemanticTokensParams , SemanticTokensRangeParams , SemanticTokensRequest , SymbolInformation , SymbolKind , TextDocumentPositionParams , TextDocuments , uinteger , _Connection } from 'vscode-languageserver' ;
1+ import { CompletionItem , Diagnostic , DiagnosticRelatedInformation , DiagnosticSeverity , DidChangeConfigurationParams , DidChangeWatchedFilesParams , DocumentSymbol , FoldingRange , Hover , HoverParams , Location , NotificationHandler , Position , PublishDiagnosticsParams , Range , SemanticTokenModifiers , SemanticTokens , SemanticTokensParams , SemanticTokensRangeParams , SemanticTokensRequest , SymbolInformation , SymbolKind , TextDocumentPositionParams , TextDocuments , uinteger , _Connection } from 'vscode-languageserver' ;
22import { TextDocument } from 'vscode-languageserver-textdocument' ;
33import { LiteralContext } from './antlr/out/vbaParser' ;
44import { SemanticToken , sortSemanticTokens } from './capabilities/vbaSemanticTokens' ;
@@ -105,7 +105,8 @@ export class ProjectInformation {
105105 return this . docInfos . get ( uri ) ?. getSemanticTokens ( range ) ?? null ;
106106 }
107107
108-
108+ sendDiagnostics = ( docInfo : DocumentInformation ) =>
109+ this . conn . sendDiagnostics ( docInfo . getDiagnostics ( ) ) ;
109110
110111 private getDocumentSettings ( docUri : string ) : Thenable < ExampleSettings > {
111112 if ( ! this . hasConfigurationCapability ) {
@@ -136,9 +137,11 @@ export class ProjectInformation {
136137 this . docs . onDidChangeContent ( change => {
137138 const doc = change . document ;
138139 const docInfo = new DocumentInformation ( this . scopes , doc . uri ) ;
140+ this . scopes . getScope ( `undeclared|${ doc . uri } ` ) . clear ( ) ;
139141 this . docInfos . set ( doc . uri , docInfo ) ;
140142 this . syntaxUtil . parse ( doc , docInfo ) ;
141143 docInfo . finalise ( ) ;
144+ this . sendDiagnostics ( docInfo ) ;
142145 } ) ;
143146 }
144147}
@@ -148,11 +151,11 @@ export class DocumentInformation implements ResultsContainer {
148151 elements : SyntaxElement [ ] = [ ] ;
149152 attrubutes : Map < string , string > = new Map ( ) ;
150153 isBusy = true ;
151-
154+
155+ private docUri : string ;
152156 private ancestors : SyntaxElement [ ] = [ ] ;
153157 private localNames : Map < string , SyntaxElement > = new Map ( ) ;
154158 private documentScope : Scope ;
155- private docUri : string ;
156159
157160 constructor ( scope : Scope , docUri : string ) {
158161 scope . links . set ( docUri , new Map ( ) ) ;
@@ -219,12 +222,14 @@ export class DocumentInformation implements ResultsContainer {
219222 link . merge ( undeclaredLink ) ;
220223 undeclaredScope . delete ( elId ) ;
221224 }
225+
226+ this . addElement ( emt ) ;
222227 }
223228
224229 addScopeReference ( emt : VariableAssignElement ) {
225- // this.addElement(emt);
226230 const link = this . getNameLink ( emt . identifier ! . text , emt . parent ?. fqName ?? '' , false , true ) ;
227231 link . references . push ( emt ) ;
232+ this . addElement ( emt ) ;
228233 }
229234
230235 private getNameLink ( identifier : string , fqName : string , isPrivate = false , searchScopes = false ) : NameLink {
@@ -257,7 +262,7 @@ export class DocumentInformation implements ResultsContainer {
257262 return scope ;
258263 }
259264 }
260- return this . documentScope . getScope ( `undefined |${ this . docUri } ` ) ;
265+ return this . documentScope . getScope ( `undeclared |${ this . docUri } ` ) ;
261266 }
262267
263268
@@ -341,6 +346,10 @@ export class DocumentInformation implements ResultsContainer {
341346 return results ;
342347 }
343348
349+ getDiagnostics ( ) : PublishDiagnosticsParams {
350+ return { uri : this . docUri , diagnostics : this . elements . map ( ( e ) => e . diagnostics ) . flat ( 1 ) } ;
351+ }
352+
344353 getSymbols = ( uri : string ) : SymbolInformation [ ] =>
345354 this . elements
346355 . filter ( ( x ) => ( ! ! x . identifier ) && ( x . identifier . text !== '' ) )
@@ -371,7 +380,10 @@ class Scope {
371380
372381 processLinks ( key : string , optExplicit = false ) {
373382 // TODO: check global for undeclareds
374- this . getScope ( key ) . forEach ( ( x ) => x . process ( optExplicit ) ) ;
383+ const undeclared = this . getScope ( `undeclared|${ key } ` ) ;
384+ const docScopes = this . getScope ( key ) ;
385+ undeclared . forEach ( ( v , k ) => docScopes . set ( k , v ) ) ;
386+ docScopes . forEach ( ( x ) => x . process ( optExplicit ) ) ;
375387 }
376388}
377389
@@ -404,7 +416,9 @@ class NameLink {
404416 this . processDiagnosticRelatedInformation ( ) ;
405417 this . validateDeclarationCount ( optExplicit ) ;
406418 this . validateMethodSignatures ( ) ;
419+
407420 this . assignSemanticTokens ( ) ;
421+ this . assignDiagnostics ( ) ;
408422 }
409423
410424 private processDiagnosticRelatedInformation ( ) {
@@ -417,7 +431,6 @@ class NameLink {
417431 }
418432
419433 private validateDeclarationCount ( optExplicit : boolean ) {
420- // Base case.
421434 if ( this . declarations . length === 1 ) {
422435 return ;
423436 }
@@ -452,10 +465,18 @@ class NameLink {
452465 return ;
453466 }
454467
455- this . references . forEach (
456- ( x ) => x . semanticToken = this . declarations [ 0 ]
457- . semanticToken
458- ?. toNewRange ( x . range ) ) ;
468+ this . references . forEach ( ( x ) => x . semanticToken =
469+ this . declarations [ 0 ]
470+ . semanticToken
471+ ?. toNewRange ( x . range ) ) ;
472+ }
473+
474+ private assignDiagnostics ( ) {
475+ if ( this . diagnostics . length === 0 ) {
476+ return ;
477+ }
478+ const els = this . declarations . concat ( this . references ) ;
479+ els . forEach ( ( x ) => x . addDiagnostics ( this . diagnostics ) ) ;
459480 }
460481
461482 private validateMethodSignatures ( ) {
0 commit comments