Skip to content

Commit d41e7ba

Browse files
committed
Sort-of fixes #977
1 parent 002b44d commit d41e7ba

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

RetailCoder.VBE/UI/Command/Refactorings/RefactorRenameCommand.cs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using Microsoft.Vbe.Interop;
23
using Rubberduck.VBEditor;
34
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
@@ -24,17 +25,13 @@ public RefactorRenameCommand(VBE vbe, RubberduckParserState state, IActiveCodePa
2425

2526
public override void Execute(object parameter)
2627
{
27-
if (Vbe.ActiveCodePane == null)
28-
{
29-
return;
30-
}
31-
3228
using (var view = new RenameDialog())
3329
{
3430
var factory = new RenamePresenterFactory(Vbe, view, _state, new MessageBox(), _wrapperWrapperFactory);
3531
var refactoring = new RenameRefactoring(factory, Editor, new MessageBox(), _state);
3632

37-
var target = parameter as Declaration;
33+
var target = GetTarget(parameter);
34+
3835
if (target == null)
3936
{
4037
refactoring.Refactor();
@@ -45,5 +42,64 @@ public override void Execute(object parameter)
4542
}
4643
}
4744
}
45+
46+
private Declaration GetTarget(object parameter)
47+
{
48+
var target = parameter as Declaration;
49+
if (target != null)
50+
{
51+
return target;
52+
}
53+
54+
// rename project
55+
if (Vbe.SelectedVBComponent == null)
56+
{
57+
return
58+
_state.AllUserDeclarations.SingleOrDefault(d =>
59+
d.DeclarationType == DeclarationType.Project && d.IdentifierName == Vbe.ActiveVBProject.Name);
60+
}
61+
62+
// selected component is not active
63+
if (Vbe.ActiveCodePane == null || Vbe.ActiveCodePane.CodeModule != Vbe.SelectedVBComponent.CodeModule)
64+
{
65+
// selected pane is userform - see if there are selected controls
66+
if (Vbe.SelectedVBComponent.Designer != null)
67+
{
68+
var designer = (dynamic)Vbe.SelectedVBComponent.Designer;
69+
70+
foreach (var control in designer.Controls)
71+
{
72+
if (!control.InSelection)
73+
{
74+
continue;
75+
}
76+
77+
target = _state.AllUserDeclarations
78+
.FirstOrDefault(item => item.IdentifierName == control.Name &&
79+
item.ComponentName == Vbe.SelectedVBComponent.Name &&
80+
Vbe.ActiveVBProject.Equals(item.Project));
81+
82+
break;
83+
}
84+
}
85+
86+
// user form is not designer or there were no selected controls
87+
if (target == null)
88+
{
89+
target = _state.AllUserDeclarations.SingleOrDefault(
90+
t => t.IdentifierName == Vbe.SelectedVBComponent.Name &&
91+
t.Project == Vbe.ActiveVBProject &&
92+
new[]
93+
{
94+
DeclarationType.Class,
95+
DeclarationType.Document,
96+
DeclarationType.Module,
97+
DeclarationType.UserForm
98+
}.Contains(t.DeclarationType));
99+
}
100+
}
101+
102+
return target;
103+
}
48104
}
49105
}

0 commit comments

Comments
 (0)