1
1
using System ;
2
2
using Microsoft . Vbe . Interop ;
3
- using System . IO ;
4
3
using System . Linq ;
5
4
6
5
namespace Rubberduck . VBEditor
@@ -12,94 +11,18 @@ public struct QualifiedModuleName
12
11
{
13
12
private static string GetDisplayName ( VBProject project )
14
13
{
15
-
16
- if ( project . Protection == vbext_ProjectProtection . vbext_pp_none )
17
- {
18
- //Try reading the top-most document-type component's Properties("Name") value
19
- //Eg. A Workbook's parent is the application, so read the workbook's name
20
- try
21
- {
22
- var component = project . VBComponents . Cast < VBComponent > ( )
23
- . FirstOrDefault ( comp => comp . Type == vbext_ComponentType . vbext_ct_Document
24
- && comp . Properties . Item ( "Name" ) . Value != null
25
- && comp . Properties . Item ( "Parent" )
26
- . Object . Equals ( comp . Properties . Item ( "Application" ) . Object ) ) ;
27
-
28
- if ( component == null ) { return null ; }
29
-
30
- var nameProperty = component . Properties . Cast < Property > ( ) . FirstOrDefault ( property => property . Name == "Name" ) ;
31
- return nameProperty == null
32
- ? null
33
- : nameProperty . Value . ToString ( ) ;
34
- }
35
- catch
36
- {
37
- //The Properties collection either wasn't available, or didn't have the expected properties
38
- }
39
-
40
- //Try reading the top-most document-type component's parent's Properties("Name") value
41
- // Eg. A PowerPoint Slide is top level, but it's parent is a Presentation (that is NOT a vbComponent)
42
- try
43
- {
44
- var firstOrDefault = project . VBComponents . Cast < VBComponent > ( )
45
- . FirstOrDefault ( comp => comp . Type == vbext_ComponentType . vbext_ct_Document
46
- && comp . Properties . Item ( "Parent" ) . Value != null ) ;
47
- if ( firstOrDefault != null )
48
- {
49
- var parentProp = firstOrDefault
50
- . Properties . Cast < Property > ( ) . FirstOrDefault ( property => property . Name == "Parent" ) ;
51
-
52
- Property nameProperty = null ;
53
- if ( parentProp != null && parentProp . Value is Properties )
54
- {
55
- var props = ( Properties ) parentProp . Value ;
56
- nameProperty = props . Cast < Property > ( ) . FirstOrDefault ( property => property . Name == "Name" ) ;
57
- }
58
-
59
- return nameProperty == null
60
- ? null
61
- : nameProperty . Value . ToString ( ) ;
62
- }
63
- }
64
- catch
65
- {
66
- //The Properties collection either wasn't available, or didn't have the expected properties
67
- }
68
- }
69
-
70
- //Try reading the filename
71
14
try
72
15
{
73
- if ( ! string . IsNullOrEmpty ( Path . GetDirectoryName ( project . BuildFileName ) ) )
16
+ if ( project . HelpFile != project . VBE . ActiveVBProject . HelpFile )
74
17
{
75
- return Path . GetFileName ( project . FileName ) ;
18
+ project . VBE . ActiveVBProject = project ;
76
19
}
20
+ return project . VBE . MainWindow . Caption . Split ( ' ' ) . Last ( ) ;
77
21
}
78
22
catch
79
- { //The GetFileName getter probably threw
80
- }
81
-
82
- return null ;
83
- }
84
-
85
- private static string GetDisplayName ( VBComponent component )
86
- {
87
- if ( component . Type == vbext_ComponentType . vbext_ct_Document )
88
23
{
89
- //Check for a valid properties collection (some hosts don't validate the Properties method unless the component's designer is open in the host
90
- try
91
- {
92
- var nameProperty = component . Properties . Item ( "Name" ) ;
93
- return nameProperty == null
94
- ? null
95
- : nameProperty . Value . ToString ( ) ;
96
- }
97
- catch
98
- {
99
- //The component isn't open in the host, the Properties Collection is probably inaccessible
100
- }
24
+ return string . Empty ;
101
25
}
102
- return null ;
103
26
}
104
27
105
28
public static string GetProjectId ( VBProject project )
@@ -128,7 +51,6 @@ public QualifiedModuleName(VBProject project)
128
51
{
129
52
_component = null ;
130
53
_componentName = null ;
131
- _componentDisplayName = null ;
132
54
_project = project ;
133
55
_projectName = project . Name ;
134
56
_projectPath = string . Empty ;
@@ -143,7 +65,6 @@ public QualifiedModuleName(VBComponent component)
143
65
144
66
_component = component ;
145
67
_componentName = component == null ? string . Empty : component . Name ;
146
- _componentDisplayName = GetDisplayName ( component ) ;
147
68
_project = component == null ? null : component . Collection . Parent ;
148
69
_projectName = _project == null ? string . Empty : _project . Name ;
149
70
_projectDisplayName = GetDisplayName ( _project ) ;
@@ -163,26 +84,6 @@ public QualifiedModuleName(VBComponent component)
163
84
: 0 ;
164
85
}
165
86
166
- /// <summary>
167
- /// Creates a QualifiedModuleName for removing renamed declarations.
168
- /// Do not use this overload.
169
- /// </summary>
170
- public QualifiedModuleName ( VBComponent component , string oldComponentName )
171
- {
172
- _project = null ; // field is only assigned when the instance refers to a VBProject.
173
-
174
- _component = component ;
175
- _componentName = oldComponentName ;
176
- _componentDisplayName = GetDisplayName ( component ) ;
177
- _project = component == null ? null : component . Collection . Parent ;
178
- _projectName = _project == null ? string . Empty : _project . Name ;
179
- _projectDisplayName = GetDisplayName ( _project ) ;
180
- _projectPath = string . Empty ;
181
- _projectId = GetProjectId ( _project ) ;
182
-
183
- _contentHashCode = 0 ;
184
- }
185
-
186
87
/// <summary>
187
88
/// Creates a QualifiedModuleName for a built-in declaration.
188
89
/// Do not use this overload for user declarations.
@@ -196,7 +97,6 @@ public QualifiedModuleName(string projectName, string projectPath, string compon
196
97
_projectId = ( _projectName + ";" + _projectPath ) . GetHashCode ( ) . ToString ( ) ;
197
98
_componentName = componentName ;
198
99
_component = null ;
199
- _componentDisplayName = null ;
200
100
_contentHashCode = 0 ;
201
101
}
202
102
@@ -222,39 +122,9 @@ public QualifiedMemberName QualifyMemberName(string member)
222
122
223
123
public string Name { get { return ToString ( ) ; } }
224
124
225
- private readonly string _componentDisplayName ;
226
- public string ComponentDisplayName { get { return _componentDisplayName ; } }
227
-
228
125
private readonly string _projectDisplayName ;
229
126
public string ProjectDisplayName { get { return _projectDisplayName ; } }
230
-
231
-
232
- /// <summary>
233
- /// returns: "ComponentName (DisplayName)" as typically displayed in VBE Project Explorer
234
- /// </summary>
235
- public string ComponentTitle {
236
- get {
237
- if ( _project != null && _component == null )
238
- {
239
- //handle display of Project component
240
- return _projectName + ( _projectDisplayName != null ? " (" + _projectDisplayName + ")" : string . Empty ) ;
241
- }
242
- else
243
- {
244
- if ( _componentDisplayName == _projectDisplayName )
245
- {
246
- //handle display of main documents, like ThisWorkbook and ThisDocument
247
- return _componentName ;
248
- }
249
- else
250
- {
251
- //handle display of all other components
252
- return _componentName + ( _componentDisplayName != null ? " (" + _componentDisplayName + ")" : string . Empty ) ;
253
- }
254
- }
255
- }
256
- }
257
-
127
+
258
128
/// <summary>
259
129
/// returns: "ProjectName (DisplayName)" as typically displayed in VBE Project Explorer
260
130
/// </summary>
0 commit comments