/
CodeExplorerFindAllReferencesCommand.cs
98 lines (85 loc) · 3.47 KB
/
CodeExplorerFindAllReferencesCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using Rubberduck.AddRemoveReferences;
using Rubberduck.Navigation.CodeExplorer;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.Controls;
using Rubberduck.VBEditor.Events;
namespace Rubberduck.UI.CodeExplorer.Commands
{
public class CodeExplorerFindAllReferencesCommand : CodeExplorerCommandBase
{
private static readonly Type[] ApplicableNodes =
{
typeof(CodeExplorerReferenceViewModel),
typeof(CodeExplorerProjectViewModel),
typeof(CodeExplorerComponentViewModel),
typeof(CodeExplorerMemberViewModel)
};
private readonly RubberduckParserState _state;
private readonly FindAllReferencesAction _finder;
public CodeExplorerFindAllReferencesCommand(
RubberduckParserState state,
FindAllReferencesAction finder,
IVbeEvents vbeEvents)
: base(vbeEvents)
{
_state = state;
_finder = finder;
AddToCanExecuteEvaluation(EvaluateCanExecute, true);
}
private bool EvaluateCanExecute(object parameter)
{
switch (parameter)
{
case CodeExplorerReferenceViewModel refNode:
return refNode.IsDimmed;
case ICodeExplorerNode node:
return !(node is CodeExplorerCustomFolderViewModel)
&& !(node is CodeExplorerReferenceFolderViewModel);
case Declaration declaration:
return !(declaration is ProjectDeclaration);
default:
return false;
}
}
protected override void OnExecute(object parameter)
{
var node = parameter as ICodeExplorerNode;
var declaration = parameter as Declaration;
var reference = parameter as CodeExplorerReferenceViewModel;
if (_state.Status != ParserState.Ready || node == null && declaration == null)
{
return;
}
if (declaration != null)
{
// command could have been invoked from PeekReferences code explorer popup
_finder.FindAllReferences(declaration);
return;
}
if (reference != null)
{
if (!(node.Parent.Declaration is ProjectDeclaration))
{
Logger.Error(
$"The specified ICodeExplorerNode ({node.GetType()}) is expected to be a direct child of a node whose declaration is a ProjectDeclaration.");
return;
}
if(node.Parent?.Declaration is ProjectDeclaration projectDeclaration)
{
if (!(reference.Reference is ReferenceModel model))
{
Logger.Warn($"Project reference '{reference.Name}' does not have an explorable reference model ({nameof(CodeExplorerReferenceViewModel)}.{nameof(CodeExplorerReferenceViewModel.Reference)} is null.");
return;
}
_finder.FindAllReferences(projectDeclaration, model.ToReferenceInfo());
return;
}
}
_finder.FindAllReferences(node.Declaration);
}
public override IEnumerable<Type> ApplicableNodeTypes => ApplicableNodes;
}
}