Skip to content

Commit 8786af5

Browse files
committed
simplified /DRY-up resolver
1 parent b1c46c9 commit 8786af5

File tree

1 file changed

+45
-79
lines changed

1 file changed

+45
-79
lines changed

Rubberduck.Parsing/Symbols/IdentifierReferenceResolver.cs

Lines changed: 45 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,29 @@ private Declaration ResolveInternal(VBAParser.ICS_B_ProcedureCallContext context
410410
return callee;
411411
}
412412

413+
private void ResolveIdentifier(VBAParser.AmbiguousIdentifierContext context)
414+
{
415+
if (context == null)
416+
{
417+
return;
418+
}
419+
420+
var identifier = ResolveInternal(context, _currentScope);
421+
if (identifier == null)
422+
{
423+
return;
424+
}
425+
426+
identifier.AddReference(CreateReference(context, identifier));
427+
}
428+
413429
public void Resolve(VBAParser.ICS_B_ProcedureCallContext context)
414430
{
431+
if (_alreadyResolved.Contains(context))
432+
{
433+
return;
434+
}
435+
415436
ResolveInternal(context);
416437
}
417438

@@ -454,58 +475,27 @@ public void Resolve(VBAParser.ICS_B_MemberProcedureCallContext context)
454475

455476
public void Resolve(VBAParser.ICS_S_VariableOrProcedureCallContext context)
456477
{
457-
if (_alreadyResolved.Contains(context))
458-
{
459-
return;
460-
}
461-
462-
try
463-
{
464-
ResolveInternal(context, _currentScope);
465-
}
466-
catch (InvalidOperationException)
467-
{
468-
// bug: more than a single match was found.
469-
}
478+
TryResolve(context);
470479
}
471480

472481
public void Resolve(VBAParser.ICS_S_ProcedureOrArrayCallContext context)
473482
{
474-
if (_alreadyResolved.Contains(context))
475-
{
476-
return;
477-
}
478-
479-
try
480-
{
481-
ResolveInternal(context, _currentScope);
482-
}
483-
catch (InvalidOperationException)
484-
{
485-
// bug: more than a single match was found.
486-
}
483+
TryResolve(context);
487484
}
488485

489486
public void Resolve(VBAParser.ICS_S_MembersCallContext context)
490487
{
491-
if (_alreadyResolved.Contains(context))
492-
{
493-
return;
494-
}
495-
496-
try
497-
{
498-
ResolveInternal(context, _currentScope);
499-
}
500-
catch (InvalidOperationException)
501-
{
502-
// bug: more than a single match was found.
503-
}
488+
TryResolve(context);
504489
}
505490

506491
public void Resolve(VBAParser.ICS_S_DictionaryCallContext context)
507492
{
508-
if (_alreadyResolved.Contains(context))
493+
TryResolve(context);
494+
}
495+
496+
private void TryResolve<TContext>(TContext context) where TContext : ParserRuleContext
497+
{
498+
if (context == null || _alreadyResolved.Contains(context))
509499
{
510500
return;
511501
}
@@ -598,56 +588,32 @@ public void Resolve(VBAParser.ForEachStmtContext context)
598588

599589
public void Resolve(VBAParser.ImplementsStmtContext context)
600590
{
601-
var identifierContext = context.ambiguousIdentifier();
602-
var identifier = ResolveInternal(identifierContext, _currentScope);
603-
identifier.AddReference(CreateReference(identifierContext, identifier));
591+
ResolveIdentifier(context.ambiguousIdentifier());
604592
}
605593

606594
public void Resolve(VBAParser.RaiseEventStmtContext context)
607595
{
608-
var identifierContext = context.ambiguousIdentifier();
609-
var identifier = ResolveInternal(identifierContext, _currentScope);
610-
identifier.AddReference(CreateReference(identifierContext, identifier));
596+
ResolveIdentifier(context.ambiguousIdentifier());
611597
}
612598

613599
public void Resolve(VBAParser.ResumeStmtContext context)
614600
{
615-
var identifierContext = context.ambiguousIdentifier();
616-
var identifier = ResolveInternal(identifierContext, _currentScope);
617-
identifier.AddReference(CreateReference(identifierContext, identifier));
601+
ResolveIdentifier(context.ambiguousIdentifier());
618602
}
619603

620604
public void Resolve(VBAParser.FileNumberContext context)
621605
{
622-
var identifierContext = context.ambiguousIdentifier();
623-
if (identifierContext == null)
624-
{
625-
return;
626-
}
627-
var identifier = ResolveInternal(identifierContext, _currentScope);
628-
identifier.AddReference(CreateReference(identifierContext, identifier));
606+
ResolveIdentifier(context.ambiguousIdentifier());
629607
}
630608

631609
public void Resolve(VBAParser.ArgDefaultValueContext context)
632610
{
633-
var identifierContext = context.ambiguousIdentifier();
634-
if (identifierContext == null)
635-
{
636-
return;
637-
}
638-
var identifier = ResolveInternal(identifierContext, _currentScope);
639-
identifier.AddReference(CreateReference(identifierContext, identifier));
611+
ResolveIdentifier(context.ambiguousIdentifier());
640612
}
641613

642614
public void Resolve(VBAParser.FieldLengthContext context)
643615
{
644-
var identifierContext = context.ambiguousIdentifier();
645-
if (identifierContext == null)
646-
{
647-
return;
648-
}
649-
var identifier = ResolveInternal(identifierContext, _currentScope);
650-
identifier.AddReference(CreateReference(identifierContext, identifier));
616+
ResolveIdentifier(context.ambiguousIdentifier());
651617
}
652618

653619
public void Resolve(VBAParser.VsAssignContext context)
@@ -706,6 +672,15 @@ private Declaration FindModuleScopeProcedure(string identifierName, Declaration
706672
return result.SingleOrDefault();
707673
}
708674

675+
private Declaration FindProjectScopeDeclaration(string identifierName)
676+
{
677+
// assume unqualified variable call, i.e. unique declaration.
678+
return _declarations[identifierName].SingleOrDefault(item =>
679+
(item.Accessibility == Accessibility.Public
680+
|| item.Accessibility == Accessibility.Global
681+
|| _moduleTypes.Contains(item.DeclarationType)));
682+
}
683+
709684
private bool IsProcedure(Declaration item)
710685
{
711686
return item.DeclarationType == DeclarationType.Procedure
@@ -741,14 +716,5 @@ private bool IsPropertyAccessor(Declaration item, ContextAccessorType accessorTy
741716
item.DeclarationType == DeclarationType.PropertyGet &&
742717
!isAssignmentTarget);
743718
}
744-
745-
private Declaration FindProjectScopeDeclaration(string identifierName)
746-
{
747-
// assume unqualified variable call, i.e. unique declaration.
748-
return _declarations[identifierName].SingleOrDefault(item =>
749-
(item.Accessibility == Accessibility.Public
750-
|| item.Accessibility == Accessibility.Global
751-
|| item.DeclarationType == DeclarationType.Module));
752-
}
753719
}
754720
}

0 commit comments

Comments
 (0)