forked from rubberduck-vba/Rubberduck
/
FindAllReferencesCommand.cs
224 lines (192 loc) · 8.1 KB
/
FindAllReferencesCommand.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using NLog;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.Command.MenuItems;
using Rubberduck.UI.Controls;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
namespace Rubberduck.UI.Command
{
/// <summary>
/// A command that locates all references to a specified identifier, or of the active code module.
/// </summary>
[ComVisible(false)]
public class FindAllReferencesCommand : CommandBase, IDisposable
{
private readonly INavigateCommand _navigateCommand;
private readonly IMessageBox _messageBox;
private readonly RubberduckParserState _state;
private readonly ISearchResultsWindowViewModel _viewModel;
private readonly SearchResultPresenterInstanceManager _presenterService;
private readonly IVBE _vbe;
public FindAllReferencesCommand(INavigateCommand navigateCommand, IMessageBox messageBox,
RubberduckParserState state, IVBE vbe, ISearchResultsWindowViewModel viewModel,
SearchResultPresenterInstanceManager presenterService)
: base(LogManager.GetCurrentClassLogger())
{
_navigateCommand = navigateCommand;
_messageBox = messageBox;
_state = state;
_vbe = vbe;
_viewModel = viewModel;
_presenterService = presenterService;
_state.StateChanged += _state_StateChanged;
}
private Declaration FindNewDeclaration(Declaration declaration)
{
return _state.DeclarationFinder
.MatchName(declaration.IdentifierName)
.SingleOrDefault(d => d.ProjectId == declaration.ProjectId
&& d.ComponentName == declaration.ComponentName
&& d.ParentScope == declaration.ParentScope
&& d.DeclarationType == declaration.DeclarationType);
}
private void _state_StateChanged(object sender, ParserStateEventArgs e)
{
if (e.State != ParserState.Ready) { return; }
if (_viewModel == null) { return; }
UiDispatcher.InvokeAsync(UpdateTab);
}
private void UpdateTab()
{
try
{
var findReferenceTabs = _viewModel.Tabs.Where(
t => t.Header.StartsWith(RubberduckUI.AllReferences_Caption.Replace("'{0}'", ""))).ToList();
foreach (var tab in findReferenceTabs)
{
var newTarget = FindNewDeclaration(tab.Target);
if (newTarget == null)
{
tab.CloseCommand.Execute(null);
return;
}
var vm = CreateViewModel(newTarget);
if (vm.SearchResults.Any())
{
tab.SearchResults = vm.SearchResults;
tab.Target = vm.Target;
}
else
{
tab.CloseCommand.Execute(null);
}
}
}
catch (Exception exception)
{
Logger.Error(exception, "Exception thrown while trying to update the find all references tab.");
}
}
protected override bool EvaluateCanExecute(object parameter)
{
if (_state.Status != ParserState.Ready ||
(_vbe.ActiveCodePane == null && !(_vbe.SelectedVBComponent?.HasDesigner ?? false)))
{
return false;
}
var target = FindTarget(parameter);
var canExecute = target != null;
return canExecute;
}
protected override void OnExecute(object parameter)
{
if (_state.Status != ParserState.Ready)
{
return;
}
var declaration = FindTarget(parameter);
if (declaration == null)
{
return;
}
var viewModel = CreateViewModel(declaration);
if (!viewModel.SearchResults.Any())
{
_messageBox.Show(string.Format(RubberduckUI.AllReferences_NoneFound, declaration.IdentifierName), RubberduckUI.Rubberduck, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (viewModel.SearchResults.Count == 1)
{
_navigateCommand.Execute(viewModel.SearchResults.Single().GetNavigationArgs());
return;
}
_viewModel.AddTab(viewModel);
_viewModel.SelectedTab = viewModel;
try
{
var presenter = _presenterService.Presenter(_viewModel);
presenter.Show();
}
catch (Exception e)
{
Logger.Error(e);
}
}
private SearchResultsViewModel CreateViewModel(Declaration declaration)
{
var results = declaration.References.Distinct().Select(reference =>
new SearchResultItem(
reference.ParentNonScoping,
new NavigateCodeEventArgs(reference.QualifiedModuleName, reference.Selection),
reference.QualifiedModuleName.Component.CodeModule.GetLines(reference.Selection.StartLine, 1).Trim()));
var viewModel = new SearchResultsViewModel(_navigateCommand,
string.Format(RubberduckUI.SearchResults_AllReferencesTabFormat, declaration.IdentifierName), declaration, results);
return viewModel;
}
private Declaration FindTarget(object parameter)
{
if (parameter is Declaration declaration)
{
return declaration;
}
return _vbe.ActiveCodePane != null && (_vbe.SelectedVBComponent?.HasDesigner ?? false)
? FindFormDesignerTarget()
: FindCodePaneTarget();
}
private Declaration FindCodePaneTarget()
{
return _state.FindSelectedDeclaration(_vbe.ActiveCodePane);
}
private Declaration FindFormDesignerTarget(QualifiedModuleName? qualifiedModuleName = null)
{
(var projectId, var component) = qualifiedModuleName.HasValue
? (qualifiedModuleName.Value.ProjectId, qualifiedModuleName.Value.Component)
: (_vbe.ActiveVBProject.ProjectId, _vbe.SelectedVBComponent);
if (component?.HasDesigner ?? false)
{
if (qualifiedModuleName.HasValue)
{
return _state.DeclarationFinder
.MatchName(qualifiedModuleName.Value.Name)
.SingleOrDefault(m => m.ProjectId == projectId
&& m.DeclarationType.HasFlag(qualifiedModuleName.Value.ComponentType)
&& m.ComponentName == component.Name);
}
var selectedCount = component.SelectedControls.Count;
if (selectedCount > 1) { return null; }
// Cannot use DeclarationType.UserForm, parser only assigns UserForms the ClassModule flag
(var selectedType, var selectedName) = selectedCount == 0
? (DeclarationType.ClassModule, component.Name)
: (DeclarationType.Control, component.SelectedControls[0].Name);
return _state.DeclarationFinder
.MatchName(selectedName)
.SingleOrDefault(m => m.ProjectId == projectId
&& m.DeclarationType.HasFlag(selectedType)
&& m.ComponentName == component.Name);
}
return null;
}
public void Dispose()
{
if (_state != null)
{
_state.StateChanged -= _state_StateChanged;
}
}
}
}