Skip to content

Commit a17016b

Browse files
committed
Closes #3389
1 parent a243ad3 commit a17016b

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

RetailCoder.VBE/UI/Command/MenuItems/FormDesignerRefactorRenameCommandMenuItem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public FormDesignerRefactorRenameCommandMenuItem(CommandBase command)
1515

1616
public override bool EvaluateCanExecute(RubberduckParserState state)
1717
{
18-
return state != null && state.Status == ParserState.Ready;
18+
return (state?.Status ?? ParserState.None) == ParserState.Ready
19+
&& Command.CanExecute(null);
1920
}
2021
}
2122
}

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

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
using System.Linq;
22
using System.Runtime.InteropServices;
3-
using System.Windows.Forms;
43
using Rubberduck.Parsing.Symbols;
54
using Rubberduck.Parsing.VBA;
65
using Rubberduck.Refactorings.Rename;
76
using Rubberduck.UI.Refactorings.Rename;
7+
using Rubberduck.VBEditor;
88
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
99

1010
namespace Rubberduck.UI.Command.Refactorings
1111
{
1212
[ComVisible(false)]
1313
public class FormDesignerRefactorRenameCommand : RefactorCommandBase
1414
{
15-
private readonly IVBE _vbe;
1615
private readonly RubberduckParserState _state;
1716
private readonly IMessageBox _messageBox;
1817

1918
public FormDesignerRefactorRenameCommand(IVBE vbe, RubberduckParserState state, IMessageBox messageBox)
2019
: base (vbe)
2120
{
22-
_vbe = vbe;
2321
_state = state;
2422
_messageBox = messageBox;
2523
}
2624

2725
protected override bool EvaluateCanExecute(object parameter)
2826
{
29-
return _state.Status == ParserState.Ready;
27+
return _state.Status == ParserState.Ready && GetTarget() != null;
3028
}
3129

3230
protected override void OnExecute(object parameter)
@@ -45,35 +43,38 @@ protected override void OnExecute(object parameter)
4543
}
4644
}
4745

48-
private Declaration GetTarget()
46+
private Declaration GetTarget(QualifiedModuleName? qualifiedModuleName = null)
4947
{
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)
5257
{
53-
if (Vbe.SelectedVBComponent != null && Vbe.SelectedVBComponent.HasDesigner)
58+
if (qualifiedModuleName.HasValue)
5459
{
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);
7464
}
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+
}
7778
return null;
7879
}
7980
}

0 commit comments

Comments
 (0)