forked from codecadwallader/codemaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeModelManager.cs
More file actions
276 lines (230 loc) · 9.41 KB
/
CodeModelManager.cs
File metadata and controls
276 lines (230 loc) · 9.41 KB
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
using EnvDTE;
using SteveCadwallader.CodeMaid.Helpers;
using SteveCadwallader.CodeMaid.Model.CodeItems;
using SteveCadwallader.CodeMaid.Properties;
using System;
using System.Threading.Tasks;
namespace SteveCadwallader.CodeMaid.Model
{
/// <summary>
/// A manager class for centralizing code model creation and life cycles.
/// </summary>
internal class CodeModelManager
{
#region Fields
private readonly CodeMaidPackage _package;
private readonly CodeModelBuilder _codeModelBuilder;
private readonly CodeModelCache _codeModelCache;
#endregion Fields
#region Constructors
/// <summary>
/// The singleton instance of the <see cref="CodeModelManager" /> class.
/// </summary>
private static CodeModelManager _instance;
/// <summary>
/// Initializes a new instance of the <see cref="CodeModelManager" /> class.
/// </summary>
/// <param name="package">The hosting package.</param>
private CodeModelManager(CodeMaidPackage package)
{
_package = package;
_codeModelBuilder = CodeModelBuilder.GetInstance(_package);
_codeModelCache = new CodeModelCache();
}
/// <summary>
/// Gets an instance of the <see cref="CodeModelManager" /> class.
/// </summary>
/// <param name="package">The hosting package.</param>
/// <returns>An instance of the <see cref="CodeModelManager" /> class.</returns>
internal static CodeModelManager GetInstance(CodeMaidPackage package)
{
return _instance ?? (_instance = new CodeModelManager(package));
}
#endregion Constructors
#region Events
/// <summary>
/// An event raised when a <see cref="CodeModel" /> has been built.
/// </summary>
internal event Action<CodeModel> CodeModelBuilt;
#endregion Events
#region Internal Event Handlers
/// <summary>
/// An event callback that is raised when a document has changed.
/// </summary>
/// <param name="document">The document.</param>
internal void OnDocumentChanged(Document document)
{
if (document != null)
{
_codeModelCache.StaleCodeModel(document);
}
}
/// <summary>
/// An event callback that is raised when a document is closing.
/// </summary>
/// <param name="document">The document.</param>
internal void OnDocumentClosing(Document document)
{
if (document != null)
{
_codeModelCache.RemoveCodeModel(document);
}
}
#endregion Internal Event Handlers
#region Internal Methods
/// <summary>
/// Retrieves a <see cref="SetCodeItems"/> of CodeItems within the specified document.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="loadLazyInitializedValues">
/// A flag indicating if lazy initialized values should be immediately loaded.
/// </param>
/// <returns>The set of code items within the document.</returns>
internal SetCodeItems RetrieveAllCodeItems(Document document, bool loadLazyInitializedValues = false)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
OutputWindowHelper.DiagnosticWriteLine(
$"CodeModelManager.RetrieveAllCodeItems for '{document.FullName}'");
var codeModel = _codeModelCache.GetCodeModel(document);
if (codeModel.IsBuilding)
{
if (!codeModel.IsBuiltWaitHandle.WaitOne(TimeSpan.FromSeconds(3)))
{
OutputWindowHelper.WarningWriteLine(
$"Timed out waiting for code model to be built for '{codeModel.Document.FullName}'");
return null;
}
}
else if (codeModel.IsStale)
{
BuildCodeItems(codeModel);
if (loadLazyInitializedValues)
{
LoadLazyInitializedValues(codeModel);
}
}
return codeModel.CodeItems;
}
/// <summary>
/// Retrieves a <see cref="SetCodeItems"/> of CodeItems within the specified document. If
/// the code items are already available they will be returned, otherwise an event will be
/// raised once the code items have been asynchronously built.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="loadLazyInitializedValues">
/// A flag indicating if lazy initialized values should be immediately loaded.
/// </param>
/// <returns>
/// The set of code items within the document if already available, otherwise null.
/// </returns>
internal SetCodeItems RetrieveAllCodeItemsAsync(Document document, bool loadLazyInitializedValues = false)
{
if (document == null)
{
throw new ArgumentNullException(nameof(document));
}
// If asynchronous loading has been disabled, redirect to the synchronous version.
if (!Settings.Default.General_LoadModelsAsynchronously)
{
return RetrieveAllCodeItems(document, loadLazyInitializedValues);
}
OutputWindowHelper.DiagnosticWriteLine(
$"CodeModelManager.RetrieveAllCodeItemsAsync for '{document.FullName}'");
var codeModel = _codeModelCache.GetCodeModel(document);
if (codeModel.IsBuilding)
{
// Exit out and wait for the event to be raised.
return null;
}
if (codeModel.IsStale)
{
// Asynchronously build the code items then raise an event.
Task.Run(() =>
{
BuildCodeItems(codeModel);
if (loadLazyInitializedValues)
{
LoadLazyInitializedValues(codeModel);
}
RaiseCodeModelBuilt(codeModel);
});
return null;
}
return codeModel.CodeItems;
}
#endregion Internal Methods
#region Private Methods
/// <summary>
/// Builds a <see cref="SetCodeItems" /> of CodeItems based on the specified code model. If
/// the document gets marked as stale during execution this process will recursively call
/// itself to start over in order to guarantee a valid code model is returned.
/// </summary>
/// <param name="codeModel">The code model.</param>
private void BuildCodeItems(CodeModel codeModel)
{
try
{
OutputWindowHelper.DiagnosticWriteLine(
$"CodeModelManager.BuildCodeItems started for '{codeModel.Document.FullName}'");
codeModel.IsBuilding = true;
codeModel.IsStale = false;
var codeItems = _codeModelBuilder.RetrieveAllCodeItems(codeModel.Document);
if (codeModel.IsStale)
{
BuildCodeItems(codeModel);
return;
}
codeModel.CodeItems = codeItems;
codeModel.IsBuilding = false;
OutputWindowHelper.DiagnosticWriteLine(
$"CodeModelManager.BuildCodeItems completed for '{codeModel.Document.FullName}'");
}
catch (Exception ex)
{
OutputWindowHelper.DiagnosticWriteLine(
$"Unable to build code model for '{codeModel.Document.FullName}': {ex}");
codeModel.CodeItems = new SetCodeItems();
codeModel.IsBuilding = false;
}
}
/// <summary>
/// Loads all lazy initialized values for items within the code model.
/// </summary>
/// <param name="codeModel">The code model.</param>
private void LoadLazyInitializedValues(CodeModel codeModel)
{
try
{
OutputWindowHelper.DiagnosticWriteLine(
$"CodeModelManager.LoadLazyInitializedValues for '{codeModel.Document.FullName}'");
foreach (var codeItem in codeModel.CodeItems)
{
codeItem.LoadLazyInitializedValues();
}
}
catch (Exception ex)
{
OutputWindowHelper.ExceptionWriteLine(
$"Unable to load lazy initialized values for '{codeModel.Document.FullName}'", ex);
}
}
/// <summary>
/// Raises the <see cref="CodeModelBuilt" /> event.
/// </summary>
/// <param name="codeModel">The code model.</param>
private void RaiseCodeModelBuilt(CodeModel codeModel)
{
var codeModelBuilt = CodeModelBuilt;
if (codeModelBuilt != null)
{
OutputWindowHelper.DiagnosticWriteLine(
$"CodeModelManager.CodeModelBuilt raised for '{codeModel.Document.FullName}'");
codeModelBuilt(codeModel);
}
}
#endregion Private Methods
}
}