@@ -27,9 +27,11 @@ public class DeclarationSymbolsListener : VBAParserBaseListener
27
27
private readonly IDictionary < Tuple < string , DeclarationType > , Attributes > _attributes ;
28
28
private readonly HashSet < ReferencePriorityMap > _projectReferences ;
29
29
30
+ private readonly List < Declaration > _createdDeclarations = new List < Declaration > ( ) ;
31
+ public IReadOnlyList < Declaration > CreatedDeclarations { get { return _createdDeclarations ; } }
32
+
30
33
public DeclarationSymbolsListener (
31
34
QualifiedModuleName qualifiedName ,
32
- Accessibility componentAccessibility ,
33
35
vbext_ComponentType type ,
34
36
IEnumerable < CommentNode > comments ,
35
37
IEnumerable < IAnnotation > annotations ,
@@ -76,8 +78,13 @@ public DeclarationSymbolsListener(
76
78
moduleAttributes ,
77
79
hasDefaultInstanceVariable : hasDefaultInstanceVariable ) ;
78
80
}
79
-
80
81
SetCurrentScope ( ) ;
82
+ AddDeclaration ( _moduleDeclaration ) ;
83
+ var component = _moduleDeclaration . QualifiedName . QualifiedModuleName . Component ;
84
+ if ( component . Type == vbext_ComponentType . vbext_ct_MSForm || component . Designer != null )
85
+ {
86
+ DeclareControlsAsMembers ( component ) ;
87
+ }
81
88
}
82
89
83
90
private IEnumerable < IAnnotation > FindAnnotations ( )
@@ -117,27 +124,6 @@ private IEnumerable<IAnnotation> FindAnnotations(int line)
117
124
return annotations ;
118
125
}
119
126
120
- public void CreateModuleDeclarations ( )
121
- {
122
- OnNewDeclaration ( _moduleDeclaration ) ;
123
-
124
- var component = _moduleDeclaration . QualifiedName . QualifiedModuleName . Component ;
125
- if ( component . Type == vbext_ComponentType . vbext_ct_MSForm || component . Designer != null )
126
- {
127
- DeclareControlsAsMembers ( component ) ;
128
- }
129
- }
130
-
131
- public event EventHandler < DeclarationEventArgs > NewDeclaration ;
132
- private void OnNewDeclaration ( Declaration declaration )
133
- {
134
- var handler = NewDeclaration ;
135
- if ( handler != null )
136
- {
137
- handler . Invoke ( this , new DeclarationEventArgs ( declaration ) ) ;
138
- }
139
- }
140
-
141
127
/// <summary>
142
128
/// Scans form designer to create a public, self-assigned field for each control on a form.
143
129
/// </summary>
@@ -177,7 +163,7 @@ private void DeclareControlsAsMembers(VBComponent form)
177
163
false ,
178
164
null ,
179
165
false ) ;
180
- OnNewDeclaration ( declaration ) ;
166
+ AddDeclaration ( declaration ) ;
181
167
}
182
168
}
183
169
@@ -303,7 +289,6 @@ private Declaration CreateDeclaration(
303
289
( ( ClassModuleDeclaration ) _parentDeclaration ) . DefaultMember = result ;
304
290
}
305
291
}
306
- OnNewDeclaration ( result ) ;
307
292
return result ;
308
293
}
309
294
@@ -361,7 +346,7 @@ public override void EnterImplementsStmt(VBAParser.ImplementsStmtContext context
361
346
362
347
public override void EnterOptionBaseStmt ( VBAParser . OptionBaseStmtContext context )
363
348
{
364
- OnNewDeclaration ( CreateDeclaration (
349
+ AddDeclaration ( CreateDeclaration (
365
350
context . GetText ( ) ,
366
351
string . Empty ,
367
352
Accessibility . Implicit ,
@@ -375,7 +360,7 @@ public override void EnterOptionBaseStmt(VBAParser.OptionBaseStmtContext context
375
360
376
361
public override void EnterOptionCompareStmt ( VBAParser . OptionCompareStmtContext context )
377
362
{
378
- OnNewDeclaration ( CreateDeclaration (
363
+ AddDeclaration ( CreateDeclaration (
379
364
context . GetText ( ) ,
380
365
string . Empty ,
381
366
Accessibility . Implicit ,
@@ -389,7 +374,7 @@ public override void EnterOptionCompareStmt(VBAParser.OptionCompareStmtContext c
389
374
390
375
public override void EnterOptionExplicitStmt ( VBAParser . OptionExplicitStmtContext context )
391
376
{
392
- OnNewDeclaration ( CreateDeclaration (
377
+ AddDeclaration ( CreateDeclaration (
393
378
context . GetText ( ) ,
394
379
string . Empty ,
395
380
Accessibility . Implicit ,
@@ -407,7 +392,7 @@ public override void ExitOptionPrivateModuleStmt(VBAParser.OptionPrivateModuleSt
407
392
{
408
393
( ( ProceduralModuleDeclaration ) _moduleDeclaration ) . IsPrivateModule = true ;
409
394
}
410
- OnNewDeclaration (
395
+ AddDeclaration (
411
396
CreateDeclaration (
412
397
context . GetText ( ) ,
413
398
string . Empty ,
@@ -439,7 +424,7 @@ public override void EnterSubStmt(VBAParser.SubStmtContext context)
439
424
false ,
440
425
null ,
441
426
null ) ;
442
- OnNewDeclaration ( declaration ) ;
427
+ AddDeclaration ( declaration ) ;
443
428
SetCurrentScope ( declaration , name ) ;
444
429
}
445
430
@@ -474,7 +459,7 @@ public override void EnterFunctionStmt(VBAParser.FunctionStmtContext context)
474
459
isArray ,
475
460
asTypeClause ,
476
461
typeHint ) ;
477
- OnNewDeclaration ( declaration ) ;
462
+ AddDeclaration ( declaration ) ;
478
463
SetCurrentScope ( declaration , name ) ;
479
464
}
480
465
@@ -505,7 +490,7 @@ public override void EnterPropertyGetStmt(VBAParser.PropertyGetStmtContext conte
505
490
asTypeClause ,
506
491
typeHint ) ;
507
492
508
- OnNewDeclaration ( declaration ) ;
493
+ AddDeclaration ( declaration ) ;
509
494
SetCurrentScope ( declaration , name ) ;
510
495
}
511
496
@@ -533,7 +518,7 @@ public override void EnterPropertyLetStmt(VBAParser.PropertyLetStmtContext conte
533
518
false ,
534
519
null ,
535
520
null ) ;
536
- OnNewDeclaration ( declaration ) ;
521
+ AddDeclaration ( declaration ) ;
537
522
SetCurrentScope ( declaration , name ) ;
538
523
}
539
524
@@ -563,7 +548,7 @@ public override void EnterPropertySetStmt(VBAParser.PropertySetStmtContext conte
563
548
null ,
564
549
null ) ;
565
550
566
- OnNewDeclaration ( declaration ) ;
551
+ AddDeclaration ( declaration ) ;
567
552
SetCurrentScope ( declaration , name ) ;
568
553
}
569
554
@@ -593,7 +578,7 @@ public override void EnterEventStmt(VBAParser.EventStmtContext context)
593
578
null ,
594
579
null ) ;
595
580
596
- OnNewDeclaration ( declaration ) ;
581
+ AddDeclaration ( declaration ) ;
597
582
SetCurrentScope ( declaration , name ) ;
598
583
}
599
584
@@ -638,7 +623,7 @@ public override void EnterDeclareStmt(VBAParser.DeclareStmtContext context)
638
623
asTypeClause ,
639
624
typeHint ) ;
640
625
641
- OnNewDeclaration ( declaration ) ;
626
+ AddDeclaration ( declaration ) ;
642
627
SetCurrentScope ( declaration , name ) ; // treat like a procedure block, to correctly scope parameters.
643
628
}
644
629
@@ -659,7 +644,7 @@ public override void EnterArgList(VBAParser.ArgListContext context)
659
644
var identifier = argContext . unrestrictedIdentifier ( ) ;
660
645
string typeHint = Identifier . GetTypeHintValue ( identifier ) ;
661
646
bool isArray = argContext . LPAREN ( ) != null ;
662
- OnNewDeclaration (
647
+ AddDeclaration (
663
648
CreateDeclaration (
664
649
Identifier . GetName ( identifier ) ,
665
650
asTypeName ,
@@ -675,7 +660,7 @@ public override void EnterArgList(VBAParser.ArgListContext context)
675
660
676
661
public override void EnterStatementLabelDefinition ( VBAParser . StatementLabelDefinitionContext context )
677
662
{
678
- OnNewDeclaration (
663
+ AddDeclaration (
679
664
CreateDeclaration (
680
665
context . statementLabel ( ) . GetText ( ) ,
681
666
null ,
@@ -706,7 +691,7 @@ public override void EnterVariableSubStmt(VBAParser.VariableSubStmtContext conte
706
691
var withEvents = parent . WITHEVENTS ( ) != null ;
707
692
var isAutoObject = asTypeClause != null && asTypeClause . NEW ( ) != null ;
708
693
bool isArray = context . LPAREN ( ) != null ;
709
- OnNewDeclaration (
694
+ AddDeclaration (
710
695
CreateDeclaration (
711
696
name ,
712
697
asTypeName ,
@@ -747,7 +732,7 @@ public override void EnterConstSubStmt(VBAParser.ConstSubStmtContext context)
747
732
context ,
748
733
identifier . GetSelection ( ) ) ;
749
734
750
- OnNewDeclaration ( declaration ) ;
735
+ AddDeclaration ( declaration ) ;
751
736
}
752
737
753
738
public override void EnterTypeStmt ( VBAParser . TypeStmtContext context )
@@ -769,7 +754,7 @@ public override void EnterTypeStmt(VBAParser.TypeStmtContext context)
769
754
null ,
770
755
null ) ;
771
756
772
- OnNewDeclaration ( declaration ) ;
757
+ AddDeclaration ( declaration ) ;
773
758
_parentDeclaration = declaration ; // treat members as child declarations, but keep them scoped to module
774
759
}
775
760
@@ -786,7 +771,7 @@ public override void EnterTypeStmt_Element(VBAParser.TypeStmt_ElementContext con
786
771
: asTypeClause . type ( ) . GetText ( ) ;
787
772
bool isArray = context . LPAREN ( ) != null ;
788
773
string typeHint = Identifier . GetTypeHintValue ( context . identifier ( ) ) ;
789
- OnNewDeclaration (
774
+ AddDeclaration (
790
775
CreateDeclaration (
791
776
context . identifier ( ) . GetText ( ) ,
792
777
asTypeName ,
@@ -820,7 +805,7 @@ public override void EnterEnumerationStmt(VBAParser.EnumerationStmtContext conte
820
805
null ,
821
806
null ) ;
822
807
823
- OnNewDeclaration ( declaration ) ;
808
+ AddDeclaration ( declaration ) ;
824
809
_parentDeclaration = declaration ; // treat members as child declarations, but keep them scoped to module
825
810
}
826
811
@@ -830,19 +815,22 @@ public override void ExitEnumerationStmt(VBAParser.EnumerationStmtContext contex
830
815
}
831
816
832
817
public override void EnterEnumerationStmt_Constant ( VBAParser . EnumerationStmt_ConstantContext context )
833
- {
818
+ {
819
+ AddDeclaration ( CreateDeclaration (
820
+ context . identifier ( ) . GetText ( ) ,
821
+ "Long" ,
822
+ Accessibility . Implicit ,
823
+ DeclarationType . EnumerationMember ,
824
+ context ,
825
+ context . identifier ( ) . GetSelection ( ) ,
826
+ false ,
827
+ null ,
828
+ null ) ) ;
829
+ }
834
830
835
- OnNewDeclaration (
836
- CreateDeclaration (
837
- context . identifier ( ) . GetText ( ) ,
838
- "Long" ,
839
- Accessibility . Implicit ,
840
- DeclarationType . EnumerationMember ,
841
- context ,
842
- context . identifier ( ) . GetSelection ( ) ,
843
- false ,
844
- null ,
845
- null ) ) ;
831
+ private void AddDeclaration ( Declaration declaration )
832
+ {
833
+ _createdDeclarations . Add ( declaration ) ;
846
834
}
847
835
}
848
836
}
0 commit comments