This repository was archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathDefaultVisualStudioDocumentTracker.cs
345 lines (272 loc) · 11.8 KB
/
DefaultVisualStudioDocumentTracker.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.Editor;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
namespace Microsoft.VisualStudio.Editor.Razor
{
internal class DefaultVisualStudioDocumentTracker : VisualStudioDocumentTracker
{
private readonly ForegroundDispatcher _foregroundDispatcher;
private readonly string _filePath;
private readonly string _projectPath;
private readonly ProjectSnapshotManager _projectManager;
private readonly WorkspaceEditorSettings _workspaceEditorSettings;
private readonly ITextBuffer _textBuffer;
private readonly ImportDocumentManager _importDocumentManager;
private readonly List<ITextView> _textViews;
private readonly Workspace _workspace;
private bool _isSupportedProject;
private ProjectSnapshot _projectSnapshot;
private int _subscribeCount;
// Only allow a single tag helper computation task at a time.
private (ProjectSnapshot project, Task task) _computingTagHelpers;
// Stores the result from the last time we computed tag helpers.
private IReadOnlyList<TagHelperDescriptor> _tagHelpers;
public override event EventHandler<ContextChangeEventArgs> ContextChanged;
public DefaultVisualStudioDocumentTracker(
ForegroundDispatcher dispatcher,
string filePath,
string projectPath,
ProjectSnapshotManager projectManager,
WorkspaceEditorSettings workspaceEditorSettings,
Workspace workspace,
ITextBuffer textBuffer,
ImportDocumentManager importDocumentManager)
{
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(filePath));
}
if (projectPath == null)
{
throw new ArgumentNullException(nameof(projectPath));
}
if (projectManager == null)
{
throw new ArgumentNullException(nameof(projectManager));
}
if (workspaceEditorSettings == null)
{
throw new ArgumentNullException(nameof(workspaceEditorSettings));
}
if (workspace == null)
{
throw new ArgumentNullException(nameof(workspace));
}
if (textBuffer == null)
{
throw new ArgumentNullException(nameof(textBuffer));
}
if (importDocumentManager == null)
{
throw new ArgumentNullException(nameof(importDocumentManager));
}
_foregroundDispatcher = dispatcher;
_filePath = filePath;
_projectPath = projectPath;
_projectManager = projectManager;
_workspaceEditorSettings = workspaceEditorSettings;
_textBuffer = textBuffer;
_importDocumentManager = importDocumentManager;
_workspace = workspace; // For now we assume that the workspace is the always default VS workspace.
_textViews = new List<ITextView>();
_tagHelpers = Array.Empty<TagHelperDescriptor>();
}
public override RazorConfiguration Configuration => _projectSnapshot?.Configuration;
public override EditorSettings EditorSettings => _workspaceEditorSettings.Current;
public override IReadOnlyList<TagHelperDescriptor> TagHelpers => _tagHelpers;
public override bool IsSupportedProject => _isSupportedProject;
public override Project Project =>
_projectSnapshot.WorkspaceProject == null ?
null :
_workspace.CurrentSolution.GetProject(_projectSnapshot.WorkspaceProject.Id);
internal override ProjectSnapshot ProjectSnapshot => _projectSnapshot;
public override ITextBuffer TextBuffer => _textBuffer;
public override IReadOnlyList<ITextView> TextViews => _textViews;
public override Workspace Workspace => _workspace;
public override string FilePath => _filePath;
public override string ProjectPath => _projectPath;
public Task PendingTagHelperTask => _computingTagHelpers.task ?? Task.CompletedTask;
internal void AddTextView(ITextView textView)
{
if (textView == null)
{
throw new ArgumentNullException(nameof(textView));
}
_foregroundDispatcher.AssertForegroundThread();
if (!_textViews.Contains(textView))
{
_textViews.Add(textView);
}
}
internal void RemoveTextView(ITextView textView)
{
if (textView == null)
{
throw new ArgumentNullException(nameof(textView));
}
_foregroundDispatcher.AssertForegroundThread();
if (_textViews.Contains(textView))
{
_textViews.Remove(textView);
}
}
public override ITextView GetFocusedTextView()
{
_foregroundDispatcher.AssertForegroundThread();
for (var i = 0; i < TextViews.Count; i++)
{
if (TextViews[i].HasAggregateFocus)
{
return TextViews[i];
}
}
return null;
}
public void Subscribe()
{
_foregroundDispatcher.AssertForegroundThread();
if (_subscribeCount++ > 0)
{
return;
}
_projectSnapshot = _projectManager.GetOrCreateProject(_projectPath);
_isSupportedProject = true;
_projectManager.Changed += ProjectManager_Changed;
_workspaceEditorSettings.Changed += EditorSettingsManager_Changed;
_importDocumentManager.Changed += Import_Changed;
_importDocumentManager.OnSubscribed(this);
OnContextChanged(ContextChangeKind.ProjectChanged);
}
public void Unsubscribe()
{
_foregroundDispatcher.AssertForegroundThread();
if (_subscribeCount == 0 || _subscribeCount-- > 1)
{
return;
}
_importDocumentManager.OnUnsubscribed(this);
_projectManager.Changed -= ProjectManager_Changed;
_workspaceEditorSettings.Changed -= EditorSettingsManager_Changed;
_importDocumentManager.Changed -= Import_Changed;
// Detached from project.
_isSupportedProject = false;
_projectSnapshot = null;
OnContextChanged(kind: ContextChangeKind.ProjectChanged);
}
private void StartComputingTagHelpers()
{
_foregroundDispatcher.AssertForegroundThread();
Debug.Assert(_projectSnapshot != null);
Debug.Assert(_computingTagHelpers.project == null && _computingTagHelpers.task == null);
if (_projectSnapshot.TryGetTagHelpers(out var results))
{
_tagHelpers = results;
OnContextChanged(ContextChangeKind.TagHelpersChanged);
return;
}
// if we get here then we know the tag helpers aren't available, so force async for ease of testing
var task = _projectSnapshot
.GetTagHelpersAsync()
.ContinueWith(TagHelpersUpdated, CancellationToken.None, TaskContinuationOptions.RunContinuationsAsynchronously, _foregroundDispatcher.ForegroundScheduler);
_computingTagHelpers = (_projectSnapshot, task);
}
private void TagHelpersUpdated(Task<IReadOnlyList<TagHelperDescriptor>> task)
{
_foregroundDispatcher.AssertForegroundThread();
Debug.Assert(_computingTagHelpers.project != null && _computingTagHelpers.task != null);
if (!_isSupportedProject)
{
return;
}
_tagHelpers = task.Exception == null ? task.Result : Array.Empty<TagHelperDescriptor>();
OnContextChanged(ContextChangeKind.TagHelpersChanged);
var projectHasChanges = _projectSnapshot != null && _projectSnapshot != _computingTagHelpers.project;
_computingTagHelpers = (null, null);
if (projectHasChanges)
{
// More changes, keep going.
StartComputingTagHelpers();
}
}
private void OnContextChanged(ContextChangeKind kind)
{
_foregroundDispatcher.AssertForegroundThread();
var handler = ContextChanged;
if (handler != null)
{
handler(this, new ContextChangeEventArgs(kind));
}
if (kind == ContextChangeKind.ProjectChanged &&
_projectSnapshot != null &&
_computingTagHelpers.project == null)
{
StartComputingTagHelpers();
}
}
// Internal for testing
internal void ProjectManager_Changed(object sender, ProjectChangeEventArgs e)
{
_foregroundDispatcher.AssertForegroundThread();
if (_projectPath != null &&
string.Equals(_projectPath, e.ProjectFilePath, StringComparison.OrdinalIgnoreCase))
{
// This will be the new snapshot unless the project was removed.
_projectSnapshot = _projectManager.GetLoadedProject(e.ProjectFilePath);
switch (e.Kind)
{
case ProjectChangeKind.DocumentAdded:
case ProjectChangeKind.DocumentRemoved:
case ProjectChangeKind.DocumentChanged:
// Nothing to do.
break;
case ProjectChangeKind.ProjectAdded:
case ProjectChangeKind.ProjectChanged:
// Just an update
OnContextChanged(ContextChangeKind.ProjectChanged);
break;
case ProjectChangeKind.ProjectRemoved:
// Fall back to ephemeral project
_projectSnapshot = _projectManager.GetOrCreateProject(ProjectPath);
OnContextChanged(ContextChangeKind.ProjectChanged);
break;
default:
throw new InvalidOperationException($"Unknown ProjectChangeKind {e.Kind}");
}
}
}
// Internal for testing
internal void EditorSettingsManager_Changed(object sender, EditorSettingsChangedEventArgs args)
{
_foregroundDispatcher.AssertForegroundThread();
OnContextChanged(ContextChangeKind.EditorSettingsChanged);
}
// Internal for testing
internal void Import_Changed(object sender, ImportChangedEventArgs args)
{
_foregroundDispatcher.AssertForegroundThread();
foreach (var path in args.AssociatedDocuments)
{
if (string.Equals(_filePath, path, StringComparison.OrdinalIgnoreCase))
{
OnContextChanged(ContextChangeKind.ImportsChanged);
break;
}
}
}
}
}