@@ -158,9 +158,23 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
158
158
// note: inter-project references won't work, but we can qualify VbaStandardLib types:
159
159
if ( libraryName == _qualifiedModuleName . ProjectName || libraryName == "VBA" )
160
160
{
161
- return _declarations [ identifier . GetText ( ) ] . SingleOrDefault ( item =>
162
- item . ProjectName == libraryName
163
- && ( _moduleTypes . Contains ( item . DeclarationType ) ) || item . DeclarationType == DeclarationType . UserDefinedType ) ;
161
+ var matches = _declarations [ identifier . GetText ( ) ] ;
162
+ try
163
+ {
164
+ return matches . SingleOrDefault ( item =>
165
+ item . ProjectName == libraryName
166
+ && ( item . Accessibility == Accessibility . Public
167
+ || item . Accessibility == Accessibility . Global
168
+ || item . Accessibility == Accessibility . Friend
169
+ || item . Accessibility == Accessibility . Implicit )
170
+ && ( _moduleTypes . Contains ( item . DeclarationType ) )
171
+ || ( item . DeclarationType == DeclarationType . UserDefinedType
172
+ && item . ComponentName == _currentScope . ComponentName ) ) ;
173
+ }
174
+ catch ( InvalidOperationException )
175
+ {
176
+ return null ;
177
+ }
164
178
}
165
179
166
180
return null ;
@@ -240,12 +254,15 @@ private Declaration ResolveInternal(ParserRuleContext callSiteContext, Declarati
240
254
}
241
255
242
256
var identifierName = callSiteContext . GetText ( ) ;
243
- Declaration callee ;
257
+ Declaration callee = null ;
244
258
if ( localScope . DeclarationType == DeclarationType . Variable )
245
259
{
246
- // localScope is a UDT
260
+ // localScope is probably a UDT
247
261
var udt = ResolveType ( localScope ) ;
248
- callee = _declarations [ identifierName ] . SingleOrDefault ( item => item . Context != null && item . Context . Parent == udt . Context ) ;
262
+ if ( udt != null )
263
+ {
264
+ callee = _declarations [ identifierName ] . SingleOrDefault ( item => item . Context != null && item . Context . Parent == udt . Context ) ;
265
+ }
249
266
}
250
267
else
251
268
{
@@ -689,7 +706,7 @@ public void Resolve(VBAParser.VsAssignContext context)
689
706
{
690
707
// named parameter reference must be scoped to called procedure
691
708
var callee = FindParentCall ( context ) ;
692
- ResolveInternal ( context . implicitCallStmt_InStmt ( ) , callee ) ;
709
+ ResolveInternal ( context . implicitCallStmt_InStmt ( ) , callee , ContextAccessorType . GetValueOrReference ) ;
693
710
}
694
711
695
712
private Declaration FindParentCall ( VBAParser . VsAssignContext context )
@@ -738,11 +755,18 @@ private Declaration FindLocalScopeDeclaration(string identifierName, Declaration
738
755
}
739
756
740
757
var matches = _declarations [ identifierName ] ;
741
- var parent = matches . SingleOrDefault ( item =>
742
- item . ParentScope == localScope . Scope
743
- && ! _moduleTypes . Contains ( item . DeclarationType ) ) ;
744
758
745
- return parent ;
759
+ try
760
+ {
761
+ return matches . SingleOrDefault ( item =>
762
+ item . ParentScope == localScope . Scope
763
+ && localScope . Context . GetSelection ( ) . Contains ( item . Selection )
764
+ && ! _moduleTypes . Contains ( item . DeclarationType ) ) ;
765
+ }
766
+ catch ( InvalidOperationException )
767
+ {
768
+ return null ;
769
+ }
746
770
}
747
771
748
772
private Declaration FindModuleScopeDeclaration ( string identifierName , Declaration localScope = null )
@@ -753,10 +777,17 @@ private Declaration FindModuleScopeDeclaration(string identifierName, Declaratio
753
777
}
754
778
755
779
var matches = _declarations [ identifierName ] ;
756
- return matches . SingleOrDefault ( item =>
757
- item . ParentScope == localScope . ParentScope
758
- && ! item . DeclarationType . HasFlag ( DeclarationType . Member )
759
- && ! _moduleTypes . Contains ( item . DeclarationType ) ) ;
780
+ try
781
+ {
782
+ return matches . SingleOrDefault ( item =>
783
+ item . ParentScope == localScope . ParentScope
784
+ && ! item . DeclarationType . HasFlag ( DeclarationType . Member )
785
+ && ! _moduleTypes . Contains ( item . DeclarationType ) ) ;
786
+ }
787
+ catch ( InvalidOperationException )
788
+ {
789
+ return null ;
790
+ }
760
791
}
761
792
762
793
private Declaration FindModuleScopeProcedure ( string identifierName , Declaration localScope , ContextAccessorType accessorType , bool isAssignmentTarget = false )
@@ -774,20 +805,27 @@ private Declaration FindModuleScopeProcedure(string identifierName, Declaration
774
805
&& item . ComponentName == localScope . ComponentName
775
806
&& ( IsProcedure ( item , localScope ) || IsPropertyAccessor ( item , accessorType , localScope , isAssignmentTarget ) ) ) ;
776
807
}
777
- catch ( InvalidOperationException e )
808
+ catch ( InvalidOperationException )
778
809
{
779
810
return null ;
780
811
}
781
812
}
782
813
783
814
private Declaration FindProjectScopeDeclaration ( string identifierName )
784
815
{
785
- // assume unqualified variable call, i.e. unique declaration.
786
- return _declarations [ identifierName ] . SingleOrDefault ( item =>
787
- ! item . DeclarationType . HasFlag ( DeclarationType . Member )
788
- && ( item . Accessibility == Accessibility . Public
789
- || item . Accessibility == Accessibility . Global
790
- || _moduleTypes . Contains ( item . DeclarationType ) /* because static classes are accessed just like modules */ ) ) ;
816
+ var matches = _declarations [ identifierName ] ;
817
+ try
818
+ {
819
+ return matches . SingleOrDefault ( item =>
820
+ ! item . DeclarationType . HasFlag ( DeclarationType . Member )
821
+ && ( item . Accessibility == Accessibility . Public
822
+ || item . Accessibility == Accessibility . Global
823
+ || _moduleTypes . Contains ( item . DeclarationType ) /* because static classes are accessed just like modules */ ) ) ;
824
+ }
825
+ catch ( InvalidOperationException )
826
+ {
827
+ return null ;
828
+ }
791
829
}
792
830
793
831
private bool IsProcedure ( Declaration item , Declaration localScope )
0 commit comments