@@ -196,6 +196,10 @@ private VBAParser.AmbiguousIdentifierContext FindAssignmentTarget(VBAParser.Impl
196
196
{
197
197
EnterIdentifier ( reference , reference . GetSelection ( ) ) ;
198
198
}
199
+ else
200
+ {
201
+ break ;
202
+ }
199
203
}
200
204
201
205
var varOrProcCall = member . iCS_S_VariableOrProcedureCall ( ) ;
@@ -208,28 +212,48 @@ private VBAParser.AmbiguousIdentifierContext FindAssignmentTarget(VBAParser.Impl
208
212
{
209
213
EnterIdentifier ( reference , reference . GetSelection ( ) ) ;
210
214
}
215
+ else
216
+ {
217
+ break ;
218
+ }
211
219
}
212
220
}
213
221
else
214
222
{
215
223
var procOrArrayCall = member . iCS_S_ProcedureOrArrayCall ( ) ;
216
224
if ( procOrArrayCall != null )
217
225
{
218
- return EnterDictionaryCall ( procOrArrayCall . dictionaryCallStmt ( ) , procOrArrayCall . ambiguousIdentifier ( ) )
219
- ?? procOrArrayCall . ambiguousIdentifier ( ) ;
226
+ var identifier = procOrArrayCall . ambiguousIdentifier ( ) ;
227
+ if ( _declarations . Items . Any ( item => item . IdentifierName == identifier . GetText ( ) ) )
228
+ {
229
+ return EnterDictionaryCall ( procOrArrayCall . dictionaryCallStmt ( ) , identifier )
230
+ ?? identifier ;
231
+ }
232
+ }
233
+ else
234
+ {
235
+ break ;
220
236
}
221
237
222
238
var varOrProcCall = member . iCS_S_VariableOrProcedureCall ( ) ;
223
239
if ( varOrProcCall != null )
224
240
{
225
- return EnterDictionaryCall ( varOrProcCall . dictionaryCallStmt ( ) , varOrProcCall . ambiguousIdentifier ( ) )
226
- ?? varOrProcCall . ambiguousIdentifier ( ) ;
241
+ var identifier = varOrProcCall . ambiguousIdentifier ( ) ;
242
+ if ( _declarations . Items . Any ( item => item . IdentifierName == identifier . GetText ( ) ) )
243
+ {
244
+ return EnterDictionaryCall ( varOrProcCall . dictionaryCallStmt ( ) , identifier )
245
+ ?? identifier ;
246
+ }
247
+ }
248
+ else
249
+ {
250
+ break ;
227
251
}
228
252
}
229
253
}
230
254
}
231
255
232
- return null ; // not possible unless grammar is modified.
256
+ return null ;
233
257
}
234
258
235
259
private VBAParser . AmbiguousIdentifierContext EnterDictionaryCall ( VBAParser . DictionaryCallStmtContext dictionaryCall , VBAParser . AmbiguousIdentifierContext parentIdentifier = null )
@@ -241,15 +265,37 @@ private VBAParser.AmbiguousIdentifierContext EnterDictionaryCall(VBAParser.Dicti
241
265
242
266
if ( parentIdentifier != null )
243
267
{
244
- EnterIdentifier ( parentIdentifier , parentIdentifier . GetSelection ( ) ) ; // we're referencing "member" in "member!field"
268
+ if ( ! EnterIdentifier ( parentIdentifier , parentIdentifier . GetSelection ( ) ) )
269
+ // we're referencing "member" in "member!field"
270
+ {
271
+ return null ;
272
+ }
245
273
}
246
-
247
- return dictionaryCall . ambiguousIdentifier ( ) ;
274
+
275
+ var identifier = dictionaryCall . ambiguousIdentifier ( ) ;
276
+ if ( _declarations . Items . Any ( item => item . IdentifierName == identifier . GetText ( ) ) )
277
+ {
278
+ return identifier ;
279
+ }
280
+
281
+ return null ;
248
282
}
249
283
284
+ public override void EnterComplexType ( VBAParser . ComplexTypeContext context )
285
+ {
286
+ var identifiers = context . ambiguousIdentifier ( ) ;
287
+ _skipIdentifiers = ! identifiers . All ( identifier => _declarations . Items . Any ( declaration => declaration . IdentifierName == identifier . GetText ( ) ) ) ;
288
+ }
289
+
290
+ public override void ExitComplexType ( VBAParser . ComplexTypeContext context )
291
+ {
292
+ _skipIdentifiers = false ;
293
+ }
294
+
295
+ private bool _skipIdentifiers ;
250
296
public override void EnterAmbiguousIdentifier ( VBAParser . AmbiguousIdentifierContext context )
251
297
{
252
- if ( IsDeclarativeContext ( context ) )
298
+ if ( _skipIdentifiers || IsDeclarativeContext ( context ) )
253
299
{
254
300
return ;
255
301
}
@@ -279,7 +325,7 @@ public override void EnterCertainIdentifier(VBAParser.CertainIdentifierContext c
279
325
EnterIdentifier ( context , selection ) ;
280
326
}
281
327
282
- private void EnterIdentifier ( ParserRuleContext context , Selection selection , bool isAssignmentTarget = false , bool hasExplicitLetStatement = false )
328
+ private bool EnterIdentifier ( ParserRuleContext context , Selection selection , bool isAssignmentTarget = false , bool hasExplicitLetStatement = false )
283
329
{
284
330
var name = context . GetText ( ) ;
285
331
var matches = _declarations [ name ] . Where ( IsInScope ) ;
@@ -292,9 +338,12 @@ private void EnterIdentifier(ParserRuleContext context, Selection selection, boo
292
338
if ( ! declaration . References . Select ( r => r . Context ) . Contains ( reference . Context ) )
293
339
{
294
340
declaration . AddReference ( reference ) ;
341
+ return true ;
295
342
}
296
343
// note: non-matching names are not necessarily undeclared identifiers, e.g. "String" in "Dim foo As String".
297
344
}
345
+
346
+ return false ;
298
347
}
299
348
300
349
public override void EnterVsAssign ( VBAParser . VsAssignContext context )
0 commit comments