1
1
using System . Linq ;
2
2
using System . Runtime . InteropServices ;
3
- using System . Windows . Forms ;
4
3
using Rubberduck . Parsing . Symbols ;
5
4
using Rubberduck . Parsing . VBA ;
6
5
using Rubberduck . Refactorings . Rename ;
7
6
using Rubberduck . UI . Refactorings . Rename ;
7
+ using Rubberduck . VBEditor ;
8
8
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
9
9
10
10
namespace Rubberduck . UI . Command . Refactorings
11
11
{
12
12
[ ComVisible ( false ) ]
13
13
public class FormDesignerRefactorRenameCommand : RefactorCommandBase
14
14
{
15
- private readonly IVBE _vbe ;
16
15
private readonly RubberduckParserState _state ;
17
16
private readonly IMessageBox _messageBox ;
18
17
19
18
public FormDesignerRefactorRenameCommand ( IVBE vbe , RubberduckParserState state , IMessageBox messageBox )
20
19
: base ( vbe )
21
20
{
22
- _vbe = vbe ;
23
21
_state = state ;
24
22
_messageBox = messageBox ;
25
23
}
26
24
27
25
protected override bool EvaluateCanExecute ( object parameter )
28
26
{
29
- return _state . Status == ParserState . Ready ;
27
+ return _state . Status == ParserState . Ready && GetTarget ( ) != null ;
30
28
}
31
29
32
30
protected override void OnExecute ( object parameter )
@@ -45,35 +43,38 @@ protected override void OnExecute(object parameter)
45
43
}
46
44
}
47
45
48
- private Declaration GetTarget ( )
46
+ private Declaration GetTarget ( QualifiedModuleName ? qualifiedModuleName = null )
49
47
{
50
- var project = _vbe . ActiveVBProject ;
51
- var component = _vbe . SelectedVBComponent ;
48
+ var projectId = qualifiedModuleName . HasValue
49
+ ? qualifiedModuleName . Value . ProjectId
50
+ : Vbe . ActiveVBProject . ProjectId ;
51
+
52
+ var component = qualifiedModuleName . HasValue
53
+ ? qualifiedModuleName . Value . Component
54
+ : Vbe . SelectedVBComponent ;
55
+
56
+ if ( component ? . HasDesigner ?? false )
52
57
{
53
- if ( Vbe . SelectedVBComponent != null && Vbe . SelectedVBComponent . HasDesigner )
58
+ if ( qualifiedModuleName . HasValue )
54
59
{
55
- var designer = ( ( dynamic ) component . Target ) . Designer ;
56
-
57
- if ( designer . selected . count == 1 )
58
- {
59
- var control = designer . selected . item ( 0 ) ;
60
- var result = _state . AllUserDeclarations
61
- . FirstOrDefault ( item => item . DeclarationType == DeclarationType . Control
62
- && project . HelpFile == item . ProjectId
63
- && item . ComponentName == component . Name
64
- && item . IdentifierName == control . Name ) ;
65
-
66
- Marshal . ReleaseComObject ( control ) ;
67
- Marshal . ReleaseComObject ( designer ) ;
68
- return result ;
69
- } else {
70
- var message = string . Format ( RubberduckUI . RenameDialog_AmbiguousSelection ) ;
71
- _messageBox . Show ( message , RubberduckUI . RenameDialog_Caption , MessageBoxButtons . OK ,
72
- MessageBoxIcon . Exclamation ) ;
73
- }
60
+ return _state . DeclarationFinder . MatchName ( qualifiedModuleName . Value . Name )
61
+ . SingleOrDefault ( m => m . ProjectId == projectId
62
+ && m . DeclarationType . HasFlag ( qualifiedModuleName . Value . ComponentType )
63
+ && m . ComponentName == component . Name ) ;
74
64
}
75
- }
76
-
65
+
66
+ var selectedCount = component . SelectedControls . Count ;
67
+ if ( selectedCount > 1 ) { return null ; }
68
+
69
+ // Cannot use DeclarationType.UserForm, parser only assigns UserForms the ClassModule flag
70
+ var selectedType = selectedCount == 0 ? DeclarationType . ClassModule : DeclarationType . Control ;
71
+ var selectedName = selectedCount == 0 ? component . Name : component . SelectedControls [ 0 ] . Name ;
72
+
73
+ return _state . DeclarationFinder . MatchName ( selectedName )
74
+ . SingleOrDefault ( m => m . ProjectId == projectId
75
+ && m . DeclarationType . HasFlag ( selectedType )
76
+ && m . ComponentName == component . Name ) ;
77
+ }
77
78
return null ;
78
79
}
79
80
}
0 commit comments