@@ -168,11 +168,20 @@ private string FindAnnotations(int line)
168
168
return null ;
169
169
}
170
170
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 )
172
181
{
173
182
if ( context == null )
174
183
{
175
- return null ;
184
+ return ;
176
185
}
177
186
178
187
var identifiers = context . ambiguousIdentifier ( )
@@ -182,10 +191,16 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
182
191
// if there's only 1 identifier, resolve to the tightest-scope match:
183
192
if ( identifiers . Count == 1 )
184
193
{
185
- return ResolveInScopeType ( identifiers . Single ( ) . GetText ( ) , _currentScope ) ;
194
+ ResolveInScopeType ( identifiers . Single ( ) . GetText ( ) , _currentScope ) ;
195
+ return ;
186
196
}
187
197
188
198
// 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
+ {
189
204
var first = identifiers [ 0 ] . GetText ( ) ;
190
205
var projectMatch = _currentScope . ProjectName == first
191
206
? _declarations . SingleOrDefault ( declaration =>
@@ -223,22 +238,25 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
223
238
if ( udtMatch != null )
224
239
{
225
240
var udtReference = CreateReference ( identifiers [ 2 ] , udtMatch ) ;
226
-
241
+
227
242
projectMatch . AddReference ( projectReference ) ;
228
243
_alreadyResolved . Add ( projectReference . Context ) ;
229
244
230
245
moduleMatch . AddReference ( moduleReference ) ;
231
246
_alreadyResolved . Add ( moduleReference . Context ) ;
232
-
247
+
233
248
udtMatch . AddReference ( udtReference ) ;
234
249
_alreadyResolved . Add ( udtReference . Context ) ;
235
-
236
- return udtMatch ;
250
+
251
+ return ;
237
252
}
238
253
}
239
254
}
240
255
else
241
256
{
257
+ projectMatch . AddReference ( projectReference ) ;
258
+ _alreadyResolved . Add ( projectReference . Context ) ;
259
+
242
260
var match = _declarations . SingleOrDefault ( declaration =>
243
261
! declaration . IsBuiltIn && declaration . ParentDeclaration != null
244
262
&& declaration . ParentDeclaration . Equals ( projectMatch )
@@ -248,20 +266,19 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
248
266
if ( match != null )
249
267
{
250
268
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
+ }
254
275
}
255
276
}
256
277
}
257
278
258
279
// first identifier didn't match current project.
259
280
// 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 )
265
282
{
266
283
var moduleMatch = _declarations . SingleOrDefault ( declaration =>
267
284
! declaration . IsBuiltIn && declaration . ParentDeclaration != null
@@ -288,13 +305,9 @@ private Declaration ResolveType(VBAParser.ComplexTypeContext context)
288
305
289
306
udtMatch . AddReference ( udtReference ) ;
290
307
_alreadyResolved . Add ( udtReference . Context ) ;
291
-
292
- return udtMatch ;
293
308
}
294
309
}
295
310
}
296
-
297
- return null ;
298
311
}
299
312
300
313
private IEnumerable < Declaration > FindMatchingTypes ( string identifier )
@@ -306,12 +319,12 @@ private IEnumerable<Declaration> FindMatchingTypes(string identifier)
306
319
. ToList ( ) ;
307
320
}
308
321
309
- private Declaration ResolveInScopeType ( string identifier , Declaration scope )
322
+ private void ResolveInScopeType ( string identifier , Declaration scope )
310
323
{
311
324
var matches = FindMatchingTypes ( identifier ) . ToList ( ) ;
312
325
if ( matches . Count == 1 )
313
326
{
314
- return matches . Single ( ) ;
327
+ return ;
315
328
}
316
329
317
330
// more than one matching identifiers found.
@@ -324,12 +337,12 @@ private Declaration ResolveInScopeType(string identifier, Declaration scope)
324
337
325
338
if ( sameScopeUdt . Count == 1 )
326
339
{
327
- return sameScopeUdt . Single ( ) ;
340
+ return ;
328
341
}
329
342
330
343
// todo: try to resolve identifier using referenced projects
331
344
332
- return null ; // match is ambiguous or unknown, return null
345
+ return ;
333
346
}
334
347
335
348
@@ -709,6 +722,13 @@ public void Resolve(VBAParser.ICS_S_MembersCallContext context)
709
722
return ;
710
723
}
711
724
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
+
712
732
Declaration parent ;
713
733
if ( _withBlockQualifiers . Any ( ) )
714
734
{
@@ -721,7 +741,7 @@ public void Resolve(VBAParser.ICS_S_MembersCallContext context)
721
741
else
722
742
{
723
743
parent = ResolveInternal ( context . iCS_S_ProcedureOrArrayCall ( ) , _currentScope )
724
- ?? ResolveInternal ( context . iCS_S_VariableOrProcedureCall ( ) , _currentScope ) ;
744
+ ?? ResolveInternal ( context . iCS_S_VariableOrProcedureCall ( ) , _currentScope ) ;
725
745
parent = ResolveType ( parent ) ;
726
746
}
727
747
0 commit comments