/
AnnotateDeclarationViewModel.cs
183 lines (151 loc) · 6.89 KB
/
AnnotateDeclarationViewModel.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
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using NLog;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Refactorings.AnnotateDeclaration;
using Rubberduck.UI.Command;
using Rubberduck.UI.WPF;
namespace Rubberduck.UI.Refactorings.AnnotateDeclaration
{
internal class AnnotateDeclarationViewModel : RefactoringViewModelBase<AnnotateDeclarationModel>
{
private readonly IAnnotationArgumentViewModelFactory _argumentFactory;
public AnnotateDeclarationViewModel(
AnnotateDeclarationModel model,
IEnumerable<IAnnotation> annotations,
IAnnotationArgumentViewModelFactory argumentFactory
)
: base(model)
{
_argumentFactory = argumentFactory;
ApplicableAnnotations = AnnotationsForDeclaration(model.Target, annotations);
AnnotationArguments = new ObservableViewModelCollection<IAnnotationArgumentViewModel>();
RefreshAnnotationArguments(Model.Annotation);
AddAnnotationArgument = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteAddArgument, parameter => CanAddArgument);
RemoveAnnotationArgument = new DelegateCommand(LogManager.GetCurrentClassLogger(), ExecuteRemoveArgument, parameter => CanRemoveArgument);
}
public IReadOnlyList<IAnnotation> ApplicableAnnotations { get; }
private static IReadOnlyList<IAnnotation> AnnotationsForDeclaration(Declaration declaration, IEnumerable<IAnnotation> annotations)
{
return AnnotationsForDeclarationType(declaration.DeclarationType, annotations)
.Where(annotation => (annotation.AllowMultiple
|| !declaration.Annotations.Any(pta => annotation.Equals(pta.Annotation)))
&& (declaration.DeclarationType.HasFlag(DeclarationType.Module)
|| declaration.AttributesPassContext != null
|| !(annotation is IAttributeAnnotation)))
.ToList();
}
private static IEnumerable<IAnnotation> AnnotationsForDeclarationType(DeclarationType declarationType, IEnumerable<IAnnotation> annotations)
{
if (declarationType.HasFlag(DeclarationType.Module))
{
return annotations.Where(annotation => annotation.Target.HasFlag(AnnotationTarget.Module));
}
if (declarationType.HasFlag(DeclarationType.Member)
&& declarationType != DeclarationType.LibraryProcedure
&& declarationType != DeclarationType.LibraryFunction)
{
return annotations.Where(annotation => annotation.Target.HasFlag(AnnotationTarget.Member));
}
if (declarationType.HasFlag(DeclarationType.Variable))
{
return annotations.Where(annotation => annotation.Target.HasFlag(AnnotationTarget.Variable));
}
return Enumerable.Empty<IAnnotation>();
}
public IAnnotation Annotation
{
get => Model.Annotation;
set
{
if (value == null && Model.Annotation == null || (value?.Equals(Model.Annotation) ?? false))
{
return;
}
Model.Annotation = value;
RefreshAnnotationArguments(value);
OnPropertyChanged();
OnPropertyChanged(nameof(IsValidAnnotation));
OnPropertyChanged(nameof(ShowAdjustAttributeOption));
}
}
public ObservableViewModelCollection<IAnnotationArgumentViewModel> AnnotationArguments { get; }
public bool AdjustAttribute
{
get => Model.AdjustAttribute;
set
{
if (value == Model.AdjustAttribute)
{
return;
}
Model.AdjustAttribute = value;
OnPropertyChanged();
}
}
public bool ShowAdjustAttributeOption => Model?.Annotation is IAttributeAnnotation;
private void RefreshAnnotationArguments(IAnnotation annotation)
{
AnnotationArguments.Clear();
var newArguments = InitialAnnotationArguments(annotation);
foreach (var argument in newArguments)
{
AnnotationArguments.Add(argument);
}
}
private IEnumerable<IAnnotationArgumentViewModel> InitialAnnotationArguments(IAnnotation annotation)
{
return annotation != null
? annotation.AllowedArgumentTypes
.Select(InitialArgumentViewModel)
.Take(annotation.RequiredArguments)
: Enumerable.Empty<AnnotationArgumentViewModel>();
}
private IAnnotationArgumentViewModel InitialArgumentViewModel(AnnotationArgumentType argumentType)
{
var argumentModel = _argumentFactory.Create(argumentType, string.Empty);
argumentModel.ErrorsChanged += ArgumentErrorStateChanged;
return argumentModel;
}
private void ArgumentErrorStateChanged(object requestor, DataErrorsChangedEventArgs e)
{
OnPropertyChanged(nameof(IsValidAnnotation));
}
public CommandBase AddAnnotationArgument { get; }
public CommandBase RemoveAnnotationArgument { get; }
private bool CanAddArgument => Annotation != null
&& (!Annotation.AllowedArguments.HasValue
|| AnnotationArguments.Count < Annotation.AllowedArguments.Value);
private bool CanRemoveArgument => Annotation != null
&& AnnotationArguments.Count > Model.Annotation.RequiredArguments;
private void ExecuteAddArgument(object parameter)
{
if (Annotation == null)
{
return;
}
var argumentType = Annotation.AllowedArgumentTypes.Last();
var newArgument = InitialArgumentViewModel(argumentType);
AnnotationArguments.Add(newArgument);
}
private void ExecuteRemoveArgument(object parameter)
{
if (!AnnotationArguments.Any())
{
return;
}
AnnotationArguments.RemoveAt(AnnotationArguments.Count - 1);
}
protected override void DialogOk()
{
Model.Arguments = AnnotationArguments
.Select(viewModel => viewModel.Model)
.ToList();
base.DialogOk();
}
public bool IsValidAnnotation => Annotation != null
&& AnnotationArguments.All(argument => !argument.HasErrors);
}
}