Skip to content

Commit

Permalink
Add references command to context menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
comintern committed Dec 10, 2018
1 parent f63202e commit 43d7092
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 88 deletions.
3 changes: 1 addition & 2 deletions Rubberduck.Core/UI/CodeExplorer/CodeExplorerControl.xaml
Expand Up @@ -285,15 +285,14 @@
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=Rename}"
Command="{Binding RenameCommand}"
CommandParameter="{Binding SelectedItem}" />
<Separator />
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=References_Caption}"
Command="{Binding AddRemoveReferencesCommand}"
CommandParameter="{Binding SelectedItem}">
<MenuItem.Icon>
<Image Source="{StaticResource AddRemoveReferencesImage}" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<Separator />
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.CodeExplorer.CodeExplorerUI, Key=CodeExplorer_SetAsStartupProject}"
Command="{Binding SetAsStartupProjectCommand}"
CommandParameter="{Binding SelectedItem}"
Expand Down

This file was deleted.

99 changes: 99 additions & 0 deletions Rubberduck.Core/UI/Command/AddRemoveReferencesCommand.cs
@@ -0,0 +1,99 @@
using System.Runtime.InteropServices;
using NLog;
using Rubberduck.AddRemoveReferences;
using Rubberduck.Navigation.CodeExplorer;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.AddRemoveReferences;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;

namespace Rubberduck.UI.Command
{
[ComVisible(false)]
public class AddRemoveReferencesCommand : CommandBase
{
private readonly IVBE _vbe;
private readonly RubberduckParserState _state;
private readonly IAddRemoveReferencesPresenterFactory _factory;
private readonly IReferenceReconciler _reconciler;

public AddRemoveReferencesCommand(IVBE vbe,
RubberduckParserState state,
IAddRemoveReferencesPresenterFactory factory,
IReferenceReconciler reconciler)
: base(LogManager.GetCurrentClassLogger())
{
_vbe = vbe;
_state = state;
_factory = factory;
_reconciler = reconciler;
}

protected override void OnExecute(object parameter)
{
if (_state.Status != ParserState.Ready)
{
return;
}

var declaration = parameter is CodeExplorerItemViewModel explorerItem
? GetDeclaration(explorerItem)
: GetDeclaration();

if (!(Declaration.GetProjectParent(declaration) is ProjectDeclaration project))
{
return;
}

var dialog = _factory.Create(project);
var model = dialog.Show();
if (model is null)
{
return;
}

_reconciler.ReconcileReferences(model);
_state.OnParseRequested(this);
}

protected override bool EvaluateCanExecute(object parameter)
{
if (_state.Status != ParserState.Ready)
{
return false;
}

if (parameter is CodeExplorerItemViewModel explorerNode)
{
return GetDeclaration(explorerNode) is ProjectDeclaration;
}

using (var project = _vbe.ActiveVBProject)
{
return !(project is null);
}
}

private Declaration GetDeclaration(CodeExplorerItemViewModel node)
{
while (node != null && !(node is ICodeExplorerDeclarationViewModel))
{
node = node.Parent;
}

return (node as ICodeExplorerDeclarationViewModel)?.Declaration;
}

private Declaration GetDeclaration()
{
using (var project = _vbe.ActiveVBProject)
{
if (project is null || project.IsWrappingNullReference)
{
return null;
}
return _state.DeclarationFinder.FindProject(project.Name);
}
}
}
}
@@ -0,0 +1,32 @@
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.Command.MenuItems.ParentMenus;

namespace Rubberduck.UI.Command.MenuItems
{
public abstract class AddRemoveReferencesCommandMenuItemBase : CommandMenuItemBase
{
protected AddRemoveReferencesCommandMenuItemBase(AddRemoveReferencesCommand command) : base(command) { }

public override string Key => "AddRemoveReferences";
public override bool BeginGroup => true;

public override bool EvaluateCanExecute(RubberduckParserState state)
{
return state != null && Command.CanExecute(null);
}
}

public class ToolMenuAddRemoveReferencesCommandMenuItem : AddRemoveReferencesCommandMenuItemBase
{
public override int DisplayOrder => (int)ToolsMenuItemDisplayOrder.AddRemoveReferences;

public ToolMenuAddRemoveReferencesCommandMenuItem(AddRemoveReferencesCommand command) : base(command) { }
}

public class ProjectExplorerAddRemoveReferencesCommandMenuItem : AddRemoveReferencesCommandMenuItemBase
{
public override int DisplayOrder => (int)RefactoringsMenuItemDisplayOrder.AddRemoveReferences;

public ProjectExplorerAddRemoveReferencesCommandMenuItem(AddRemoveReferencesCommand command) : base(command) { }
}
}
Expand Up @@ -24,5 +24,6 @@ public enum RefactoringsMenuItemDisplayOrder
EncapsulateField,
IntroduceParameter,
IntroduceField,
AddRemoveReferences
}
}
Expand Up @@ -18,5 +18,6 @@ public enum ToolsMenuItemDisplayOrder
ToDoExplorer,
RegexAssistant,
ExportAll,
AddRemoveReferences
}
}
Expand Up @@ -48,23 +48,32 @@ protected override void OnExecute(object parameter)

private Declaration GetTarget()
{
if (Vbe.SelectedVBComponent == null)
string selectedComponentName;
using (var selectedComponent = Vbe.SelectedVBComponent)
{
return
_state.AllUserDeclarations.SingleOrDefault(d =>
d.DeclarationType == DeclarationType.Project && d.IdentifierName == Vbe.ActiveVBProject.Name);
selectedComponentName = selectedComponent?.Name;
}

return _state.AllUserDeclarations.SingleOrDefault(
t => t.IdentifierName == Vbe.SelectedVBComponent.Name &&
t.ProjectId == Vbe.ActiveVBProject.ProjectId &&
new[]
{
DeclarationType.ClassModule,
DeclarationType.Document,
DeclarationType.ProceduralModule,
DeclarationType.UserForm
}.Contains(t.DeclarationType));

string activeProjectId;
using (var activeProject = Vbe.ActiveVBProject)
{
activeProjectId = activeProject?.ProjectId;
}

if (activeProjectId == null)
{
return null;
}

if (selectedComponentName == null)
{
return _state.DeclarationFinder.UserDeclarations(DeclarationType.Project)
.SingleOrDefault(d => d.ProjectId == activeProjectId);
}

return _state.DeclarationFinder.UserDeclarations(DeclarationType.Module)
.SingleOrDefault(t => t.IdentifierName == selectedComponentName
&& t.ProjectId == activeProjectId);
}
}
}
6 changes: 4 additions & 2 deletions Rubberduck.Main/Root/RubberduckIoCInstaller.cs
Expand Up @@ -501,7 +501,8 @@ private Type[] ProjectWindowContextMenuItems()
typeof(ProjectExplorerRefactorRenameCommandMenuItem),
typeof(FindSymbolCommandMenuItem),
typeof(FindAllReferencesCommandMenuItem),
typeof(FindAllImplementationsCommandMenuItem)
typeof(FindAllImplementationsCommandMenuItem),
typeof(ProjectExplorerAddRemoveReferencesCommandMenuItem)
};
}

Expand Down Expand Up @@ -605,7 +606,8 @@ private Type[] ToolsMenuItems()
typeof(RegexAssistantCommandMenuItem),
typeof(ToDoExplorerCommandMenuItem),
typeof(CodeMetricsCommandMenuItem),
typeof(ExportAllCommandMenuItem)
typeof(ExportAllCommandMenuItem),
typeof(ToolMenuAddRemoveReferencesCommandMenuItem)
};

return items.ToArray();
Expand Down
11 changes: 10 additions & 1 deletion Rubberduck.Resources/Menus/RubberduckMenus.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Rubberduck.Resources/Menus/RubberduckMenus.resx
Expand Up @@ -235,4 +235,7 @@
<value>Re&amp;order Parameters</value>
<comment>TC: different hotkey</comment>
</data>
<data name="AddRemoveReferences" xml:space="preserve">
<value>Add/Remove References...</value>
</data>
</root>
2 changes: 1 addition & 1 deletion Rubberduck.Resources/RubberduckUI.resx
Expand Up @@ -1348,7 +1348,7 @@ NOTE: Restart is required for the setting to take effect.</value>
<value>Type Libraries (*.olb;*.tlb;*.dll)|*.olb;*.tlb;*.dll</value>
</data>
<data name="References_Caption" xml:space="preserve">
<value>Add/Remove References</value>
<value>Add/Remove References...</value>
</data>
<data name="References_Locale" xml:space="preserve">
<value>Locale:</value>
Expand Down

0 comments on commit 43d7092

Please sign in to comment.