@@ -3,7 +3,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
33import { LiteralContext } from './antlr/out/vbaParser' ;
44import { SemanticToken , sortSemanticTokens } from './capabilities/vbaSemanticTokens' ;
55import { sleep , rangeIsChildOfElement } from './utils/helpers' ;
6- import { IdentifierElement , MethodElement , ModuleAttribute , ModuleElement , SyntaxElement , VariableAssignElement , VariableDeclarationElement , VariableStatementElement } from './utils/vbaSyntaxElements' ;
6+ import { IdentifiableSyntaxElement , IdentifierElement , MethodElement , ModuleAttribute , ModuleElement , SyntaxElement , VariableDeclarationElement , VariableStatementElement } from './utils/vbaSyntaxElements' ;
77import { ResultsContainer , SyntaxParser } from './utils/vbaSyntaxParser' ;
88
99
@@ -151,15 +151,14 @@ export class DocumentInformation implements ResultsContainer {
151151 elements : SyntaxElement [ ] = [ ] ;
152152 attrubutes : Map < string , string > = new Map ( ) ;
153153 isBusy = true ;
154+ scope : Scope ;
154155
155156 private docUri : string ;
156157 private ancestors : SyntaxElement [ ] = [ ] ;
157- private localNames : Map < string , SyntaxElement > = new Map ( ) ;
158- private documentScope : Scope ;
159158
160159 constructor ( scope : Scope , docUri : string ) {
161160 scope . links . set ( docUri , new Map ( ) ) ;
162- this . documentScope = scope ;
161+ this . scope = scope ;
163162 this . docUri = docUri ;
164163 }
165164
@@ -208,15 +207,27 @@ export class DocumentInformation implements ResultsContainer {
208207 // emt.hoverText = hoverText;
209208 // }
210209
211- addScopeDeclaration ( emt : MethodElement | VariableDeclarationElement ) {
210+ /**
211+ * Use this method to set as the current scope.
212+ * @param emt The function, sub, or property to scope to.
213+ */
214+ pushScopeElement ( emt : MethodElement ) {
215+ this . addScopedDeclaration ( emt ) ;
216+ }
217+
218+ popScopeElement ( ) {
219+ //
220+ }
221+
222+ addScopedDeclaration ( emt : MethodElement | VariableDeclarationElement ) {
212223 // Add a declared scope.
213224 // this.addElement(emt);
214225 const elId = emt . identifier ! . text ;
215226 const link = this . getNameLink ( elId , emt . parent ?. fqName ?? '' , emt . hasPrivateModifier ) ;
216227 link . declarations . push ( emt ) ;
217228
218229 // Check the undeclared links and merge if found.
219- const undeclaredScope = this . documentScope . getScope ( `undeclared|${ this . docUri } ` ) ;
230+ const undeclaredScope = this . scope . getScope ( `undeclared|${ this . docUri } ` ) ;
220231 const undeclaredLink = undeclaredScope . get ( elId ) ;
221232 if ( undeclaredLink ) {
222233 link . merge ( undeclaredLink ) ;
@@ -226,7 +237,7 @@ export class DocumentInformation implements ResultsContainer {
226237 this . addElement ( emt ) ;
227238 }
228239
229- addScopeReference ( emt : VariableAssignElement ) {
240+ addScopedReference ( emt : IdentifierElement ) {
230241 const link = this . getNameLink ( emt . identifier ! . text , emt . parent ?. fqName ?? '' , false , true ) ;
231242 link . references . push ( emt ) ;
232243 this . addElement ( emt ) ;
@@ -247,8 +258,8 @@ export class DocumentInformation implements ResultsContainer {
247258 }
248259
249260 private getScope ( fqName : string , isPrivate : boolean ) : Map < string , NameLink > {
250- const globalScope = this . documentScope . getScope ( 'global' ) ;
251- const localScope = this . documentScope . getScope ( this . docUri ) ;
261+ const globalScope = this . scope . getScope ( 'global' ) ;
262+ const localScope = this . scope . getScope ( this . docUri ) ;
252263
253264 const isAtModuleLevel = ( fqName ?? '' ) === this . docUri ;
254265 return ( isAtModuleLevel && ! isPrivate ) ? globalScope : localScope ;
@@ -257,17 +268,17 @@ export class DocumentInformation implements ResultsContainer {
257268 private searchScopes ( identifier : string ) : Map < string , NameLink > {
258269 const scopeNames = [ this . docUri , 'global' ] ;
259270 for ( let i = 0 ; i < scopeNames . length ; i ++ ) {
260- const scope = this . documentScope . getScope ( scopeNames [ i ] ) ;
271+ const scope = this . scope . getScope ( scopeNames [ i ] ) ;
261272 if ( scope . has ( identifier ) ) {
262273 return scope ;
263274 }
264275 }
265- return this . documentScope . getScope ( `undeclared|${ this . docUri } ` ) ;
276+ return this . scope . getScope ( `undeclared|${ this . docUri } ` ) ;
266277 }
267278
268279
269280 finalise ( ) {
270- this . documentScope . processLinks ( this . docUri , true ) ;
281+ this . scope . processLinks ( this . docUri , true ) ;
271282 this . isBusy = false ;
272283 }
273284
@@ -366,18 +377,32 @@ export class DocumentInformation implements ResultsContainer {
366377class Scope {
367378 // { docUri: { identifier: [ declarationElement, elementLink... ] } }
368379 links : Map < string , Map < string , NameLink > > = new Map ( ) ;
380+ private subScopes : string [ ] = [ ] ;
381+ private currentDoc = '' ;
369382
370383 constructor ( ) {
371384 this . links . set ( 'global' , new Map ( ) ) ;
372385 }
373386
374387 getScope ( key : string ) : Map < string , NameLink > {
388+ if ( key !== this . currentDoc ) {
389+ this . currentDoc = key ;
390+ this . subScopes = [ 'module' ] ;
391+ }
375392 if ( ! this . links . has ( key ) ) {
376393 this . links . set ( key , new Map ( ) ) ;
377394 }
378395 return this . links . get ( key ) ! ;
379396 }
380397
398+ pushSubScope ( namespace : string ) {
399+ this . subScopes . push ( namespace ) ;
400+ }
401+
402+ popSubScope ( ) {
403+ this . subScopes . pop ( ) ;
404+ }
405+
381406 processLinks ( key : string , optExplicit = false ) {
382407 // TODO: check global for undeclareds
383408 const undeclared = this . getScope ( `undeclared|${ key } ` ) ;
@@ -482,4 +507,31 @@ class NameLink {
482507 private validateMethodSignatures ( ) {
483508 // TODO: implement.
484509 }
510+ }
511+
512+ class Scope2 {
513+ context : SyntaxElement ;
514+ parent ?: Scope2 ;
515+ nameRefs : Map < string , NameLink > = new Map ( ) ;
516+
517+ constructor ( ctx : SyntaxElement ) ;
518+ constructor ( ctx : SyntaxElement , pnt : Scope2 ) ;
519+ constructor ( ctx : SyntaxElement , pnt ?: Scope2 ) {
520+ this . context = ctx ;
521+ this . parent = pnt ;
522+ }
523+
524+ addRef ( ctx : IdentifiableSyntaxElement ) {
525+ const nameLink = this . getName ( ctx . identifier . text ) ;
526+ // if dec
527+
528+ // if not dec
529+ }
530+
531+ getName ( identifier : string ) : NameLink {
532+ if ( ! this . nameRefs . has ( identifier ) ) {
533+ this . nameRefs . set ( identifier , new NameLink ( ) ) ;
534+ }
535+ return this . nameRefs . get ( identifier ) ! ;
536+ }
485537}
0 commit comments