Skip to content

Commit 43d7092

Browse files
committed
Add references command to context menus.
1 parent f63202e commit 43d7092

File tree

11 files changed

+176
-88
lines changed

11 files changed

+176
-88
lines changed

Rubberduck.Core/UI/CodeExplorer/CodeExplorerControl.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,14 @@
285285
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=Rename}"
286286
Command="{Binding RenameCommand}"
287287
CommandParameter="{Binding SelectedItem}" />
288+
<Separator />
288289
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=References_Caption}"
289290
Command="{Binding AddRemoveReferencesCommand}"
290291
CommandParameter="{Binding SelectedItem}">
291292
<MenuItem.Icon>
292293
<Image Source="{StaticResource AddRemoveReferencesImage}" />
293294
</MenuItem.Icon>
294295
</MenuItem>
295-
<Separator />
296-
<Separator />
297296
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.CodeExplorer.CodeExplorerUI, Key=CodeExplorer_SetAsStartupProject}"
298297
Command="{Binding SetAsStartupProjectCommand}"
299298
CommandParameter="{Binding SelectedItem}"

Rubberduck.Core/UI/CodeExplorer/Commands/AddRemoveReferencesCommand.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System.Runtime.InteropServices;
2+
using NLog;
3+
using Rubberduck.AddRemoveReferences;
4+
using Rubberduck.Navigation.CodeExplorer;
5+
using Rubberduck.Parsing.Symbols;
6+
using Rubberduck.Parsing.VBA;
7+
using Rubberduck.UI.AddRemoveReferences;
8+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
9+
10+
namespace Rubberduck.UI.Command
11+
{
12+
[ComVisible(false)]
13+
public class AddRemoveReferencesCommand : CommandBase
14+
{
15+
private readonly IVBE _vbe;
16+
private readonly RubberduckParserState _state;
17+
private readonly IAddRemoveReferencesPresenterFactory _factory;
18+
private readonly IReferenceReconciler _reconciler;
19+
20+
public AddRemoveReferencesCommand(IVBE vbe,
21+
RubberduckParserState state,
22+
IAddRemoveReferencesPresenterFactory factory,
23+
IReferenceReconciler reconciler)
24+
: base(LogManager.GetCurrentClassLogger())
25+
{
26+
_vbe = vbe;
27+
_state = state;
28+
_factory = factory;
29+
_reconciler = reconciler;
30+
}
31+
32+
protected override void OnExecute(object parameter)
33+
{
34+
if (_state.Status != ParserState.Ready)
35+
{
36+
return;
37+
}
38+
39+
var declaration = parameter is CodeExplorerItemViewModel explorerItem
40+
? GetDeclaration(explorerItem)
41+
: GetDeclaration();
42+
43+
if (!(Declaration.GetProjectParent(declaration) is ProjectDeclaration project))
44+
{
45+
return;
46+
}
47+
48+
var dialog = _factory.Create(project);
49+
var model = dialog.Show();
50+
if (model is null)
51+
{
52+
return;
53+
}
54+
55+
_reconciler.ReconcileReferences(model);
56+
_state.OnParseRequested(this);
57+
}
58+
59+
protected override bool EvaluateCanExecute(object parameter)
60+
{
61+
if (_state.Status != ParserState.Ready)
62+
{
63+
return false;
64+
}
65+
66+
if (parameter is CodeExplorerItemViewModel explorerNode)
67+
{
68+
return GetDeclaration(explorerNode) is ProjectDeclaration;
69+
}
70+
71+
using (var project = _vbe.ActiveVBProject)
72+
{
73+
return !(project is null);
74+
}
75+
}
76+
77+
private Declaration GetDeclaration(CodeExplorerItemViewModel node)
78+
{
79+
while (node != null && !(node is ICodeExplorerDeclarationViewModel))
80+
{
81+
node = node.Parent;
82+
}
83+
84+
return (node as ICodeExplorerDeclarationViewModel)?.Declaration;
85+
}
86+
87+
private Declaration GetDeclaration()
88+
{
89+
using (var project = _vbe.ActiveVBProject)
90+
{
91+
if (project is null || project.IsWrappingNullReference)
92+
{
93+
return null;
94+
}
95+
return _state.DeclarationFinder.FindProject(project.Name);
96+
}
97+
}
98+
}
99+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Rubberduck.Parsing.VBA;
2+
using Rubberduck.UI.Command.MenuItems.ParentMenus;
3+
4+
namespace Rubberduck.UI.Command.MenuItems
5+
{
6+
public abstract class AddRemoveReferencesCommandMenuItemBase : CommandMenuItemBase
7+
{
8+
protected AddRemoveReferencesCommandMenuItemBase(AddRemoveReferencesCommand command) : base(command) { }
9+
10+
public override string Key => "AddRemoveReferences";
11+
public override bool BeginGroup => true;
12+
13+
public override bool EvaluateCanExecute(RubberduckParserState state)
14+
{
15+
return state != null && Command.CanExecute(null);
16+
}
17+
}
18+
19+
public class ToolMenuAddRemoveReferencesCommandMenuItem : AddRemoveReferencesCommandMenuItemBase
20+
{
21+
public override int DisplayOrder => (int)ToolsMenuItemDisplayOrder.AddRemoveReferences;
22+
23+
public ToolMenuAddRemoveReferencesCommandMenuItem(AddRemoveReferencesCommand command) : base(command) { }
24+
}
25+
26+
public class ProjectExplorerAddRemoveReferencesCommandMenuItem : AddRemoveReferencesCommandMenuItemBase
27+
{
28+
public override int DisplayOrder => (int)RefactoringsMenuItemDisplayOrder.AddRemoveReferences;
29+
30+
public ProjectExplorerAddRemoveReferencesCommandMenuItem(AddRemoveReferencesCommand command) : base(command) { }
31+
}
32+
}

Rubberduck.Core/UI/Command/MenuItems/ParentMenus/RefactoringsParentMenu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ public enum RefactoringsMenuItemDisplayOrder
2424
EncapsulateField,
2525
IntroduceParameter,
2626
IntroduceField,
27+
AddRemoveReferences
2728
}
2829
}

Rubberduck.Core/UI/Command/MenuItems/ParentMenus/ToolsParentMenu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public enum ToolsMenuItemDisplayOrder
1818
ToDoExplorer,
1919
RegexAssistant,
2020
ExportAll,
21+
AddRemoveReferences
2122
}
2223
}

Rubberduck.Core/UI/Command/Refactorings/ProjectExplorerRefactorRenameCommand.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,32 @@ protected override void OnExecute(object parameter)
4848

4949
private Declaration GetTarget()
5050
{
51-
if (Vbe.SelectedVBComponent == null)
51+
string selectedComponentName;
52+
using (var selectedComponent = Vbe.SelectedVBComponent)
5253
{
53-
return
54-
_state.AllUserDeclarations.SingleOrDefault(d =>
55-
d.DeclarationType == DeclarationType.Project && d.IdentifierName == Vbe.ActiveVBProject.Name);
54+
selectedComponentName = selectedComponent?.Name;
5655
}
57-
58-
return _state.AllUserDeclarations.SingleOrDefault(
59-
t => t.IdentifierName == Vbe.SelectedVBComponent.Name &&
60-
t.ProjectId == Vbe.ActiveVBProject.ProjectId &&
61-
new[]
62-
{
63-
DeclarationType.ClassModule,
64-
DeclarationType.Document,
65-
DeclarationType.ProceduralModule,
66-
DeclarationType.UserForm
67-
}.Contains(t.DeclarationType));
56+
57+
string activeProjectId;
58+
using (var activeProject = Vbe.ActiveVBProject)
59+
{
60+
activeProjectId = activeProject?.ProjectId;
61+
}
62+
63+
if (activeProjectId == null)
64+
{
65+
return null;
66+
}
67+
68+
if (selectedComponentName == null)
69+
{
70+
return _state.DeclarationFinder.UserDeclarations(DeclarationType.Project)
71+
.SingleOrDefault(d => d.ProjectId == activeProjectId);
72+
}
73+
74+
return _state.DeclarationFinder.UserDeclarations(DeclarationType.Module)
75+
.SingleOrDefault(t => t.IdentifierName == selectedComponentName
76+
&& t.ProjectId == activeProjectId);
6877
}
6978
}
7079
}

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ private Type[] ProjectWindowContextMenuItems()
501501
typeof(ProjectExplorerRefactorRenameCommandMenuItem),
502502
typeof(FindSymbolCommandMenuItem),
503503
typeof(FindAllReferencesCommandMenuItem),
504-
typeof(FindAllImplementationsCommandMenuItem)
504+
typeof(FindAllImplementationsCommandMenuItem),
505+
typeof(ProjectExplorerAddRemoveReferencesCommandMenuItem)
505506
};
506507
}
507508

@@ -605,7 +606,8 @@ private Type[] ToolsMenuItems()
605606
typeof(RegexAssistantCommandMenuItem),
606607
typeof(ToDoExplorerCommandMenuItem),
607608
typeof(CodeMetricsCommandMenuItem),
608-
typeof(ExportAllCommandMenuItem)
609+
typeof(ExportAllCommandMenuItem),
610+
typeof(ToolMenuAddRemoveReferencesCommandMenuItem)
609611
};
610612

611613
return items.ToArray();

Rubberduck.Resources/Menus/RubberduckMenus.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/Menus/RubberduckMenus.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,7 @@
235235
<value>Re&amp;order Parameters</value>
236236
<comment>TC: different hotkey</comment>
237237
</data>
238+
<data name="AddRemoveReferences" xml:space="preserve">
239+
<value>Add/Remove References...</value>
240+
</data>
238241
</root>

0 commit comments

Comments
 (0)