1
- using System ;
2
- using System . Collections . Generic ;
3
1
using System . Linq ;
4
- using Antlr4 . Runtime ;
5
2
using Microsoft . Vbe . Interop ;
6
3
using Rubberduck . Parsing ;
7
- using Rubberduck . Parsing . Nodes ;
8
4
using Rubberduck . VBEHost ;
9
5
10
6
namespace Rubberduck . Extensions
11
7
{
12
8
public static class VbeExtensions
13
9
{
14
- public static IEnumerable < CodeModule > FindCodeModules ( this VBE vbe , QualifiedModuleName qualifiedName )
10
+ public static CodeModule FindCodeModule ( this VBE vbe , QualifiedModuleName qualifiedName )
15
11
{
16
- return FindCodeModules ( vbe , qualifiedName . ProjectName , qualifiedName . ModuleName ) ;
17
- }
18
-
19
- /// <summary>
20
- /// Finds all code modules that match the specified project and component names.
21
- /// </summary>
22
- /// <param name="vbe"></param>
23
- /// <param name="projectName"></param>
24
- /// <param name="componentName"></param>
25
- /// <returns></returns>
26
- public static IEnumerable < CodeModule > FindCodeModules ( this VBE vbe , string projectName , string componentName )
27
- {
28
- var matches =
29
- vbe . VBProjects . Cast < VBProject > ( )
30
- . Where ( project => project . Protection != vbext_ProjectProtection . vbext_pp_locked && project . Name == projectName )
31
- . SelectMany ( project => project . VBComponents . Cast < VBComponent > ( )
32
- . Where ( component => component . Name == componentName ) )
33
- . Select ( component => component . CodeModule ) ;
34
- return matches ;
35
- }
36
-
37
- public static CodeModuleSelection FindInstruction ( this VBE vbe , CommentNode comment )
38
- {
39
- var modules = FindCodeModules ( vbe , comment . QualifiedSelection . QualifiedName ) ;
40
- foreach ( var module in modules )
41
- {
42
- var selection = comment . QualifiedSelection . Selection ;
43
-
44
- if ( module . Lines [ selection . StartLine , selection . LineCount ]
45
- . Replace ( " _\n " , " " ) . Contains ( comment . Comment ) )
46
- {
47
- return new CodeModuleSelection ( module , selection ) ;
48
- }
49
- }
50
-
51
- return null ;
12
+ var vbComponent = vbe . VBProjects . Cast < VBProject > ( )
13
+ . Where ( project => project . Protection != vbext_ProjectProtection . vbext_pp_locked
14
+ && project . Equals ( qualifiedName . Project ) )
15
+ . SelectMany ( project => project . VBComponents . Cast < VBComponent > ( ) )
16
+ . SingleOrDefault ( component => component . Equals ( qualifiedName . Component ) ) ;
17
+
18
+ return vbComponent == null
19
+ ? null
20
+ : vbComponent . CodeModule ;
52
21
}
53
22
54
23
public static void SetSelection ( this VBE vbe , QualifiedSelection selection )
@@ -62,7 +31,7 @@ public static void SetSelection(this VBE vbe, QualifiedSelection selection)
62
31
if ( project != null )
63
32
{
64
33
component = project . VBComponents . Cast < VBComponent > ( )
65
- . FirstOrDefault ( c => c . Name == selection . QualifiedName . ModuleName ) ;
34
+ . FirstOrDefault ( c => c . Equals ( selection . QualifiedName . Component ) ) ;
66
35
}
67
36
68
37
if ( component == null )
@@ -73,61 +42,38 @@ public static void SetSelection(this VBE vbe, QualifiedSelection selection)
73
42
component . CodeModule . CodePane . SetSelection ( selection . Selection ) ;
74
43
}
75
44
76
- [ Obsolete ]
77
- public static CodeModuleSelection FindInstruction ( this VBE vbe , QualifiedModuleName qualifiedModuleName , ParserRuleContext context )
45
+ public static CodeModuleSelection FindInstruction ( this VBE vbe , QualifiedModuleName qualifiedModuleName , Selection selection )
78
46
{
79
- var projectName = qualifiedModuleName . ProjectName ;
80
- var componentName = qualifiedModuleName . ModuleName ;
81
-
82
- var modules = FindCodeModules ( vbe , projectName , componentName ) . ToList ( ) ;
83
- foreach ( var module in modules )
47
+ var module = FindCodeModule ( vbe , qualifiedModuleName ) ;
48
+ if ( module == null )
84
49
{
85
- Selection selection ;
86
- var text = " " ;
87
- if ( context == null )
88
- {
89
- selection = Selection . Home ;
90
- }
91
- else
92
- {
93
- selection = context . GetSelection ( ) ;
94
- text = context . GetText ( ) ;
95
- }
96
-
97
- if ( module . Lines [ selection . StartLine , selection . LineCount ]
98
- . Replace ( " _\n " , " " ) . Contains ( text ) )
99
- {
100
- return new CodeModuleSelection ( module , selection ) ;
101
- }
50
+ return null ;
102
51
}
103
52
104
- return new CodeModuleSelection ( modules . First ( ) , Selection . Home ) ;
105
- }
106
-
107
- public static CodeModuleSelection FindInstruction ( this VBE vbe , QualifiedModuleName qualifiedModuleName , Selection selection )
108
- {
109
- var projectName = qualifiedModuleName . ProjectName ;
110
- var componentName = qualifiedModuleName . ModuleName ;
111
-
112
- var modules = FindCodeModules ( vbe , projectName , componentName ) . ToList ( ) ;
113
-
114
- return new CodeModuleSelection ( modules . First ( ) , selection ) ;
53
+ return new CodeModuleSelection ( module , selection ) ;
115
54
}
116
55
117
56
/// <summary> Returns the type of Office Application that is hosting the VBE. </summary>
118
57
/// <returns> Returns null if Unit Testing does not support Host Application.</returns>
119
58
public static IHostApplication HostApplication ( this VBE vbe )
120
59
{
121
- foreach ( Reference reference in vbe . ActiveVBProject . References )
60
+ foreach ( var reference in vbe . ActiveVBProject . References . Cast < Reference > ( )
61
+ . Where ( reference => reference . BuiltIn && reference . Name != "VBA" ) )
122
62
{
123
- if ( reference . BuiltIn && reference . Name != "VBA" )
63
+ switch ( reference . Name )
124
64
{
125
- if ( reference . Name == "Excel" ) return new ExcelApp ( ) ;
126
- if ( reference . Name == "Access" ) return new AccessApp ( ) ;
127
- if ( reference . Name == "Word" ) return new WordApp ( ) ;
128
- if ( reference . Name == "PowerPoint" ) return new PowerPointApp ( ) ;
129
- if ( reference . Name == "Outlook" ) return new OutlookApp ( ) ;
130
- if ( reference . Name == "Publisher" ) return new PublisherApp ( ) ;
65
+ case "Excel" :
66
+ return new ExcelApp ( ) ;
67
+ case "Access" :
68
+ return new AccessApp ( ) ;
69
+ case "Word" :
70
+ return new WordApp ( ) ;
71
+ case "PowerPoint" :
72
+ return new PowerPointApp ( ) ;
73
+ case "Outlook" :
74
+ return new OutlookApp ( ) ;
75
+ case "Publisher" :
76
+ return new PublisherApp ( ) ;
131
77
}
132
78
}
133
79
0 commit comments