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