-
Notifications
You must be signed in to change notification settings - Fork 296
/
CodeExplorerViewModel.cs
461 lines (391 loc) · 16.2 KB
/
CodeExplorerViewModel.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using NLog;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.UI;
using Rubberduck.UI.CodeExplorer.Commands;
using Rubberduck.UI.Command;
using Rubberduck.VBEditor.SafeComWrappers;
using System.Windows;
using System.Windows.Input;
using Rubberduck.Parsing.UIContext;
using Rubberduck.Templates;
using Rubberduck.UI.UnitTesting.Commands;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
// ReSharper disable ExplicitCallerInfoArgument
namespace Rubberduck.Navigation.CodeExplorer
{
[Flags]
public enum CodeExplorerSortOrder
{
Undefined = 0,
Name = 1,
CodeLine = 1 << 2,
DeclarationType = 1 << 3,
DeclarationTypeThenName = DeclarationType | Name,
DeclarationTypeThenCodeLine = DeclarationType | CodeLine
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
public sealed class CodeExplorerViewModel : ViewModelBase
{
// ReSharper disable NotAccessedField.Local - The settings providers aren't used, but several enhancement requests will need them.
private readonly RubberduckParserState _state;
private readonly RemoveCommand _externalRemoveCommand;
private readonly IConfigProvider<GeneralSettings> _generalSettingsProvider;
private readonly IConfigProvider<WindowSettings> _windowSettingsProvider;
private readonly IUiDispatcher _uiDispatcher;
private readonly IVBE _vbe;
private readonly ITemplateProvider _templateProvider;
// ReSharper restore NotAccessedField.Local
public CodeExplorerViewModel(
RubberduckParserState state,
RemoveCommand removeCommand,
IConfigProvider<GeneralSettings> generalSettingsProvider,
IConfigProvider<WindowSettings> windowSettingsProvider,
IUiDispatcher uiDispatcher,
IVBE vbe,
ITemplateProvider templateProvider,
ICodeExplorerSyncProvider syncProvider)
{
_state = state;
_state.StateChanged += HandleStateChanged;
_state.ModuleStateChanged += ParserState_ModuleStateChanged;
_externalRemoveCommand = removeCommand;
_generalSettingsProvider = generalSettingsProvider;
_windowSettingsProvider = windowSettingsProvider;
_uiDispatcher = uiDispatcher;
_vbe = vbe;
_templateProvider = templateProvider;
CollapseAllSubnodesCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteCollapseNodes, EvaluateCanSwitchNodeState);
ExpandAllSubnodesCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteExpandNodes, EvaluateCanSwitchNodeState);
ClearSearchCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteClearSearchCommand);
if (_externalRemoveCommand != null)
{
RemoveCommand = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteRemoveCommand, _externalRemoveCommand.CanExecute);
}
OnPropertyChanged("Projects");
SyncCodePaneCommand = syncProvider.GetSyncCommand(this);
// Force a call to EvaluateCanExecute
OnPropertyChanged("SyncCodePaneCommand");
}
public ObservableCollection<ICodeExplorerNode> Projects { get; } = new ObservableCollection<ICodeExplorerNode>();
public ObservableCollection<Template> BuiltInTemplates =>
new ObservableCollection<Template>(_templateProvider.GetTemplates().Where(t => !t.IsUserDefined)
.OrderBy(t => t.Name));
public ObservableCollection<Template> UserDefinedTemplates =>
new ObservableCollection<Template>(_templateProvider.GetTemplates().Where(t => t.IsUserDefined)
.OrderBy(t => t.Name));
private ICodeExplorerNode _selectedItem;
public ICodeExplorerNode SelectedItem
{
get => _selectedItem;
set
{
if (_selectedItem == value)
{
return;
}
_selectedItem = value;
ExpandToNode(value);
OnPropertyChanged();
OnPropertyChanged("ExportVisibility");
OnPropertyChanged("ExportAllVisibility");
}
}
public bool AnyTemplatesCanExecute =>
BuiltInTemplates.Concat(UserDefinedTemplates)
.Any(template => AddTemplateCommand.CanExecuteForNode(SelectedItem));
private CodeExplorerSortOrder _sortOrder = CodeExplorerSortOrder.Name;
public CodeExplorerSortOrder SortOrder
{
get => _sortOrder;
set
{
if (_sortOrder == value)
{
return;
}
_sortOrder = value;
foreach (var project in Projects.OfType<CodeExplorerProjectViewModel>())
{
project.SortOrder = _sortOrder;
}
OnPropertyChanged();
}
}
private bool _canSearch;
public bool CanSearch
{
get => _canSearch;
set
{
_canSearch = value;
OnPropertyChanged();
}
}
private bool _isBusy;
public bool IsBusy
{
get => _isBusy;
set
{
_isBusy = value;
OnPropertyChanged();
}
}
private bool _unparsed = true;
public bool Unparsed
{
get => _unparsed;
set
{
if (_unparsed == value)
{
return;
}
_unparsed = value;
OnPropertyChanged();
}
}
private string _filterText = string.Empty;
public string Search
{
get => _filterText;
set
{
var input = value ?? string.Empty;
if (_filterText.Equals(input))
{
return;
}
_filterText = value;
foreach (var project in Projects)
{
project.Filter = _filterText;
}
OnPropertyChanged();
}
}
private double? _fontSize = 12;
public double? FontSize
{
get => _fontSize ?? 12;
set
{
if (!value.HasValue || value.Equals(_fontSize))
{
return;
}
_fontSize = value;
OnPropertyChanged();
}
}
private void HandleStateChanged(object sender, ParserStateEventArgs e)
{
Unparsed = false;
if (e.State == ParserState.Ready)
{
// Finished up resolving references, so we can now update the reference nodes.
_uiDispatcher.Invoke(() =>
{
var referenceFolders = Projects.SelectMany(node =>
node.Children.OfType<CodeExplorerReferenceFolderViewModel>());
foreach (var library in referenceFolders)
{
library.UpdateChildren();
}
IsBusy = false;
});
return;
}
IsBusy = _state.Status != ParserState.Pending && _state.Status <= ParserState.ResolvedDeclarations;
if (e.State != ParserState.ResolvedDeclarations)
{
return;
}
Synchronize(_state.DeclarationFinder.AllUserDeclarations.ToList());
}
private void Synchronize(List<Declaration> declarations)
{
_uiDispatcher.Invoke(() =>
{
var existing = Projects.OfType<CodeExplorerProjectViewModel>().ToList();
foreach (var project in existing)
{
project.Synchronize(declarations);
if (project.Declaration is null)
{
Projects.Remove(project);
}
}
var adding = declarations.OfType<ProjectDeclaration>().ToList();
foreach (var project in adding)
{
var model = new CodeExplorerProjectViewModel(project, declarations.Where(proj => proj.ProjectId.Equals(project.ProjectId)).ToList(), _state, _vbe);
Projects.Add(model);
}
CanSearch = Projects.Any();
});
}
private void ParserState_ModuleStateChanged(object sender, ParseProgressEventArgs e)
{
// if we are resolving references, we already have the declarations and don't need to display error
if (!(e.State == ParserState.Error ||
e.State == ParserState.ResolverError &&
e.OldState == ParserState.ResolvingDeclarations))
{
return;
}
var componentProject = _state.ProjectsProvider.Project(e.Module.ProjectId);
var module = Projects.OfType<CodeExplorerProjectViewModel>()
.FirstOrDefault(p => p.Declaration.Project?.Equals(componentProject) ?? false)?.Children
.OfType<CodeExplorerComponentViewModel>().FirstOrDefault(component =>
component.QualifiedSelection?.QualifiedName.Equals(e.Module) ?? false);
if (module == null)
{
return;
}
module.IsErrorState = true;
}
private void ExecuteClearSearchCommand(object parameter)
{
if (!string.IsNullOrEmpty(Search))
{
Search = string.Empty;
}
}
private bool EvaluateCanSwitchNodeState(object parameter)
{
return SelectedItem?.Children?.Any() ?? false;
}
private void ExecuteCollapseNodes(object parameter)
{
if (!(parameter is ICodeExplorerNode node))
{
return;
}
SwitchNodeState(node, false);
}
private void ExecuteExpandNodes(object parameter)
{
if (!(parameter is ICodeExplorerNode node))
{
return;
}
SwitchNodeState(node, true);
}
// this is a special case--we have to reset SelectedItem to prevent a crash
private void ExecuteRemoveCommand(object param)
{
var node = (CodeExplorerComponentViewModel)SelectedItem;
SelectedItem = Projects.FirstOrDefault(p => p.QualifiedSelection.HasValue
&& p.QualifiedSelection.Value.QualifiedName.ProjectId == node.Declaration.ProjectId);
_externalRemoveCommand.Execute(param);
}
public ReparseCommand RefreshCommand { get; set; }
public OpenCommand OpenCommand { get; set; }
public AddVBFormCommand AddVBFormCommand { get; set; }
public AddMDIFormCommand AddMDIFormCommand { get; set; }
public AddUserFormCommand AddUserFormCommand { get; set; }
public AddStdModuleCommand AddStdModuleCommand { get; set; }
public AddClassModuleCommand AddClassModuleCommand { get; set; }
public AddUserControlCommand AddUserControlCommand { get; set; }
public AddPropertyPageCommand AddPropertyPageCommand { get; set; }
public AddUserDocumentCommand AddUserDocumentCommand { get; set; }
public AddTestComponentCommand AddTestModuleCommand { get; set; }
public AddTestModuleWithStubsCommand AddTestModuleWithStubsCommand { get; set; }
public AddTemplateCommand AddTemplateCommand { get; set; }
public OpenDesignerCommand OpenDesignerCommand { get; set; }
public OpenProjectPropertiesCommand OpenProjectPropertiesCommand { get; set; }
public SetAsStartupProjectCommand SetAsStartupProjectCommand { get; set; }
public RenameCommand RenameCommand { get; set; }
public IndentCommand IndenterCommand { get; set; }
public CodeExplorerFindAllReferencesCommand FindAllReferencesCommand { get; set; }
public CodeExplorerFindAllImplementationsCommand FindAllImplementationsCommand { get; set; }
public CommandBase CollapseAllSubnodesCommand { get; }
public CopyResultsCommand CopyResultsCommand { get; set; }
public CommandBase ExpandAllSubnodesCommand { get; }
public ImportCommand ImportCommand { get; set; }
public ExportCommand ExportCommand { get; set; }
public ExportAllCommand ExportAllCommand { get; set; }
public CommandBase RemoveCommand { get; }
public PrintCommand PrintCommand { get; set; }
public AddRemoveReferencesCommand AddRemoveReferencesCommand { get; set; }
public ICommand ClearSearchCommand { get; }
public CommandBase SyncCodePaneCommand { get; }
public ICodeExplorerNode FindVisibleNodeForDeclaration(Declaration declaration)
{
if (declaration == null)
{
return null;
}
var project = Projects.OfType<CodeExplorerProjectViewModel>().FirstOrDefault(proj =>
(proj.Declaration?.ProjectId ?? string.Empty).Equals(declaration.ProjectId));
if (declaration.DeclarationType == DeclarationType.Project)
{
return project;
}
var child = FindChildNodeForDeclaration(project, declaration);
return child;
}
private ICodeExplorerNode FindChildNodeForDeclaration(ICodeExplorerNode node, Declaration declaration)
{
if (node is null || declaration is null)
{
return null;
}
if (node.Declaration.DeclarationType == declaration.DeclarationType &&
node.Declaration.QualifiedName.Equals(declaration.QualifiedName))
{
return node;
}
return node.Children.OfType<CodeExplorerItemViewModel>()
.Select(child => FindChildNodeForDeclaration(child, declaration))
.FirstOrDefault(child => child != null);
}
private void SwitchNodeState(ICodeExplorerNode node, bool expandedState)
{
node.IsExpanded = expandedState;
foreach (var item in node.Children)
{
item.IsExpanded = expandedState;
SwitchNodeState(item, expandedState);
}
}
/// <summary>
/// Works backward from the passed node and expands all parents to make it visible.
/// </summary>
/// <param name="node"></param>
private void ExpandToNode(ICodeExplorerNode node)
{
while (true)
{
if (node == null)
{
return;
}
node.IsExpanded = true;
node = node.Parent;
}
}
private bool CanExecuteExportAllCommand => ExportAllCommand?.CanExecute(SelectedItem) ?? false;
public Visibility ExportVisibility => _vbe.Kind == VBEKind.Standalone || CanExecuteExportAllCommand ? Visibility.Collapsed : Visibility.Visible;
public Visibility ExportAllVisibility => CanExecuteExportAllCommand ? Visibility.Visible : Visibility.Collapsed;
public Visibility VB6Visibility => _vbe.Kind == VBEKind.Standalone ? Visibility.Visible : Visibility.Collapsed;
public Visibility VBAVisibility => _vbe.Kind == VBEKind.Hosted ? Visibility.Visible : Visibility.Collapsed;
public void Dispose()
{
if (_state != null)
{
_state.StateChanged -= HandleStateChanged;
_state.ModuleStateChanged -= ParserState_ModuleStateChanged;
}
}
}
}