Skip to content

Commit 85ef392

Browse files
committed
#973 (partial fix)
1 parent 5754575 commit 85ef392

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

Rubberduck.Parsing/Symbols/IdentifierReferenceResolver.cs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,20 @@ private string FindAnnotations(int line)
168168
return null;
169169
}
170170

171-
private Declaration ResolveType(VBAParser.ComplexTypeContext context)
171+
private void ResolveType(VBAParser.ICS_S_MembersCallContext context)
172+
{
173+
var first = context.iCS_S_VariableOrProcedureCall().ambiguousIdentifier();
174+
var identifiers = new[] {first}.Concat(context.iCS_S_MemberCall()
175+
.Select(member => member.iCS_S_VariableOrProcedureCall().ambiguousIdentifier()))
176+
.ToList();
177+
ResolveType(identifiers);
178+
}
179+
180+
private void ResolveType(VBAParser.ComplexTypeContext context)
172181
{
173182
if (context == null)
174183
{
175-
return null;
184+
return;
176185
}
177186

178187
var identifiers = context.ambiguousIdentifier()
@@ -182,10 +191,16 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
182191
// if there's only 1 identifier, resolve to the tightest-scope match:
183192
if (identifiers.Count == 1)
184193
{
185-
return ResolveInScopeType(identifiers.Single().GetText(), _currentScope);
194+
ResolveInScopeType(identifiers.Single().GetText(), _currentScope);
195+
return;
186196
}
187197

188198
// if there's 2 or more identifiers, resolve to the deepest path:
199+
ResolveType(identifiers);
200+
}
201+
202+
private void ResolveType(IList<VBAParser.AmbiguousIdentifierContext> identifiers)
203+
{
189204
var first = identifiers[0].GetText();
190205
var projectMatch = _currentScope.ProjectName == first
191206
? _declarations.SingleOrDefault(declaration =>
@@ -223,22 +238,25 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
223238
if (udtMatch != null)
224239
{
225240
var udtReference = CreateReference(identifiers[2], udtMatch);
226-
241+
227242
projectMatch.AddReference(projectReference);
228243
_alreadyResolved.Add(projectReference.Context);
229244

230245
moduleMatch.AddReference(moduleReference);
231246
_alreadyResolved.Add(moduleReference.Context);
232-
247+
233248
udtMatch.AddReference(udtReference);
234249
_alreadyResolved.Add(udtReference.Context);
235-
236-
return udtMatch;
250+
251+
return;
237252
}
238253
}
239254
}
240255
else
241256
{
257+
projectMatch.AddReference(projectReference);
258+
_alreadyResolved.Add(projectReference.Context);
259+
242260
var match = _declarations.SingleOrDefault(declaration =>
243261
!declaration.IsBuiltIn && declaration.ParentDeclaration != null
244262
&& declaration.ParentDeclaration.Equals(projectMatch)
@@ -248,20 +266,19 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
248266
if (match != null)
249267
{
250268
var reference = CreateReference(identifiers[1], match);
251-
match.AddReference(reference);
252-
_alreadyResolved.Add(reference.Context);
253-
return match;
269+
if (reference != null)
270+
{
271+
match.AddReference(reference);
272+
_alreadyResolved.Add(reference.Context);
273+
return;
274+
}
254275
}
255276
}
256277
}
257278

258279
// first identifier didn't match current project.
259280
// if there are 3 identifiers, type isn't in current project.
260-
if (identifiers.Count == 3)
261-
{
262-
return null;
263-
}
264-
else
281+
if (identifiers.Count != 3)
265282
{
266283
var moduleMatch = _declarations.SingleOrDefault(declaration =>
267284
!declaration.IsBuiltIn && declaration.ParentDeclaration != null
@@ -288,13 +305,9 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
288305

289306
udtMatch.AddReference(udtReference);
290307
_alreadyResolved.Add(udtReference.Context);
291-
292-
return udtMatch;
293308
}
294309
}
295310
}
296-
297-
return null;
298311
}
299312

300313
private IEnumerable<Declaration> FindMatchingTypes(string identifier)
@@ -306,12 +319,12 @@ private IEnumerable<Declaration> FindMatchingTypes(string identifier)
306319
.ToList();
307320
}
308321

309-
private Declaration ResolveInScopeType(string identifier, Declaration scope)
322+
private void ResolveInScopeType(string identifier, Declaration scope)
310323
{
311324
var matches = FindMatchingTypes(identifier).ToList();
312325
if (matches.Count == 1)
313326
{
314-
return matches.Single();
327+
return;
315328
}
316329

317330
// more than one matching identifiers found.
@@ -324,12 +337,12 @@ private Declaration ResolveInScopeType(string identifier, Declaration scope)
324337

325338
if (sameScopeUdt.Count == 1)
326339
{
327-
return sameScopeUdt.Single();
340+
return;
328341
}
329342

330343
// todo: try to resolve identifier using referenced projects
331344

332-
return null; // match is ambiguous or unknown, return null
345+
return;
333346
}
334347

335348

@@ -709,6 +722,13 @@ public void Resolve(VBAParser.ICS_S_MembersCallContext context)
709722
return;
710723
}
711724

725+
if (context.Parent.Parent.Parent is VBAParser.VsNewContext)
726+
{
727+
// if we're in a ValueStatement/New context, we're actually resolving for a type:
728+
ResolveType(context);
729+
return;
730+
}
731+
712732
Declaration parent;
713733
if (_withBlockQualifiers.Any())
714734
{
@@ -721,7 +741,7 @@ public void Resolve(VBAParser.ICS_S_MembersCallContext context)
721741
else
722742
{
723743
parent = ResolveInternal(context.iCS_S_ProcedureOrArrayCall(), _currentScope)
724-
?? ResolveInternal(context.iCS_S_VariableOrProcedureCall(), _currentScope);
744+
?? ResolveInternal(context.iCS_S_VariableOrProcedureCall(), _currentScope);
725745
parent = ResolveType(parent);
726746
}
727747

0 commit comments

Comments
 (0)