13
13
using Rubberduck . Parsing . VBA . ReferenceManagement ;
14
14
using Rubberduck . VBEditor ;
15
15
using Rubberduck . VBEditor . Extensions ;
16
+ using Rubberduck . VBEditor . SafeComWrappers ;
16
17
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
17
18
18
19
namespace Rubberduck . Parsing . VBA . DeclarationCaching
@@ -49,9 +50,10 @@ public class DeclarationFinder
49
50
private Lazy < IDictionary < ClassModuleDeclaration , List < ClassModuleDeclaration > > > _interfaceImplementations ;
50
51
private Lazy < IDictionary < IInterfaceExposable , List < ModuleBodyElementDeclaration > > > _implementationsByMember ;
51
52
52
- private Lazy < List < Declaration > > _nonBaseAsType ;
53
+ private Lazy < List < Declaration > > _nonBaseAsType ;
53
54
private Lazy < List < Declaration > > _eventHandlers ;
54
55
private Lazy < List < Declaration > > _controlEventHandlers ;
56
+ private Lazy < List < Declaration > > _formEventHandlers ;
55
57
private Lazy < List < Declaration > > _projects ;
56
58
private Lazy < List < Declaration > > _classes ;
57
59
@@ -167,12 +169,13 @@ private void InitializeLazyCollections()
167
169
168
170
_eventHandlers = new Lazy < List < Declaration > > ( FindAllEventHandlers , true ) ;
169
171
_controlEventHandlers = new Lazy < List < Declaration > > ( FindAllFormControlHandlers , true ) ;
172
+ _formEventHandlers = new Lazy < List < Declaration > > ( FindAllFormEventHandlers , true ) ;
170
173
_projects = new Lazy < List < Declaration > > ( ( ) => DeclarationsWithType ( DeclarationType . Project ) . ToList ( ) , true ) ;
171
174
_classes = new Lazy < List < Declaration > > ( ( ) => DeclarationsWithType ( DeclarationType . ClassModule ) . ToList ( ) , true ) ;
172
175
_handlersByWithEventsField = new Lazy < IDictionary < Declaration , List < Declaration > > > ( FindAllHandlersByWithEventField , true ) ;
173
176
174
177
_implementingMembers = new Lazy < IDictionary < ( VBAParser . ImplementsStmtContext Context , Declaration Implementor ) , List < ModuleBodyElementDeclaration > > > ( FindAllImplementingMembers , true ) ;
175
- _interfaceMembers = new Lazy < IDictionary < ClassModuleDeclaration , List < Declaration > > > ( FindAllIinterfaceMembersByModule , true ) ;
178
+ _interfaceMembers = new Lazy < IDictionary < ClassModuleDeclaration , List < Declaration > > > ( FindAllInterfaceMembersByModule , true ) ;
176
179
_membersByImplementsContext = new Lazy < IDictionary < VBAParser . ImplementsStmtContext , List < ModuleBodyElementDeclaration > > > ( FindAllImplementingMembersByImplementsContext , true ) ;
177
180
_interfaceImplementations = new Lazy < IDictionary < ClassModuleDeclaration , List < ClassModuleDeclaration > > > ( FindAllImplementionsByInterface , true ) ;
178
181
_implementationsByMember = new Lazy < IDictionary < IInterfaceExposable , List < ModuleBodyElementDeclaration > > > ( FindAllImplementingMembersByMember , true ) ;
@@ -233,7 +236,7 @@ private IDictionary<IInterfaceExposable, List<ModuleBodyElementDeclaration>> Fin
233
236
return _implementingMembers . Value . ToDictionary ( pair => pair . Key . Context , pair => pair . Value ) ;
234
237
}
235
238
236
- private IDictionary < ClassModuleDeclaration , List < Declaration > > FindAllIinterfaceMembersByModule ( )
239
+ private IDictionary < ClassModuleDeclaration , List < Declaration > > FindAllInterfaceMembersByModule ( )
237
240
{
238
241
return UserDeclarations ( DeclarationType . ClassModule )
239
242
. Concat ( UserDeclarations ( DeclarationType . Document ) )
@@ -303,7 +306,7 @@ public IEnumerable<Declaration> FindEventHandlers()
303
306
{
304
307
return _eventHandlers . Value ;
305
308
}
306
-
309
+
307
310
public IEnumerable < Declaration > FindFormControlEventHandlers ( )
308
311
{
309
312
return _controlEventHandlers . Value ;
@@ -316,6 +319,11 @@ public IEnumerable<Declaration> FindFormControlEventHandlers(Declaration control
316
319
&& handlers . IdentifierName . StartsWith ( control . IdentifierName + "_" ) ) ;
317
320
}
318
321
322
+ public IEnumerable < Declaration > FindFormEventHandlers ( )
323
+ {
324
+ return _formEventHandlers . Value ;
325
+ }
326
+
319
327
public IEnumerable < Declaration > Classes => _classes . Value ;
320
328
public IEnumerable < Declaration > Projects => _projects . Value ;
321
329
@@ -1211,7 +1219,8 @@ private List<Declaration> FindAllEventHandlers()
1211
1219
. Where ( item => handlerNames . Contains ( item . IdentifierName ) )
1212
1220
)
1213
1221
. Concat ( _handlersByWithEventsField . Value . AllValues ( ) )
1214
- . Concat ( FindFormControlEventHandlers ( ) ) ;
1222
+ . Concat ( FindFormControlEventHandlers ( ) )
1223
+ . Concat ( FindFormEventHandlers ( ) ) ;
1215
1224
return handlers . ToList ( ) ;
1216
1225
1217
1226
// Local functions to help break up the complex logic in finding built-in handlers
@@ -1234,6 +1243,24 @@ bool IsHostSpecificHandler(Declaration item)
1234
1243
}
1235
1244
}
1236
1245
1246
+ private List < Declaration > FindAllFormEventHandlers ( )
1247
+ {
1248
+ var forms = DeclarationsWithType ( DeclarationType . ClassModule ) .
1249
+ Where ( declaration => declaration . QualifiedModuleName . ComponentType == ComponentType . UserForm ) ;
1250
+ var formScopes = forms
1251
+ . Select ( form => form . Scope )
1252
+ . ToHashSet ( ) ;
1253
+ var events = BuiltInDeclarations ( DeclarationType . Event )
1254
+ . Where ( item => item . ParentScope == "FM20.DLL;MSForms.FormEvents" ) ;
1255
+ var handlerNames = events
1256
+ . Select ( item => "UserForm_" + item . IdentifierName )
1257
+ . ToHashSet ( ) ;
1258
+ var handlers = UserDeclarations ( DeclarationType . Procedure )
1259
+ . Where ( procedure => handlerNames . Contains ( procedure . IdentifierName )
1260
+ && formScopes . Contains ( procedure . ParentScope ) ) ;
1261
+ return handlers . ToList ( ) ;
1262
+ }
1263
+
1237
1264
/// <summary>
1238
1265
/// Finds declarations that would be in conflict with the target declaration if renamed.
1239
1266
/// </summary>
0 commit comments