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 pathDefaultVisualStudioRazorParser.cs
450 lines (363 loc) · 15.4 KB
/
DefaultVisualStudioRazorParser.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
// 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.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.CodeAnalysis.Razor.Editor;
using Microsoft.Extensions.Internal;
using Microsoft.VisualStudio.Text;
using static Microsoft.VisualStudio.Editor.Razor.BackgroundParser;
using ITextBuffer = Microsoft.VisualStudio.Text.ITextBuffer;
using Timer = System.Threading.Timer;
namespace Microsoft.VisualStudio.Editor.Razor
{
internal class DefaultVisualStudioRazorParser : VisualStudioRazorParser, IDisposable
{
public override event EventHandler<DocumentStructureChangedEventArgs> DocumentStructureChanged;
// Internal for testing.
internal TimeSpan IdleDelay = TimeSpan.FromSeconds(3);
internal Timer _idleTimer;
internal BackgroundParser _parser;
internal ChangeReference _latestChangeReference;
internal RazorSyntaxTreePartialParser _partialParser;
private readonly object IdleLock = new object();
private readonly VisualStudioCompletionBroker _completionBroker;
private readonly VisualStudioDocumentTracker _documentTracker;
private readonly ForegroundDispatcher _dispatcher;
private readonly ProjectSnapshotProjectEngineFactory _projectEngineFactory;
private readonly ErrorReporter _errorReporter;
private RazorProjectEngine _projectEngine;
private RazorCodeDocument _codeDocument;
private ITextSnapshot _snapshot;
private bool _disposed;
// For testing only
internal DefaultVisualStudioRazorParser(RazorCodeDocument codeDocument)
{
_codeDocument = codeDocument;
}
public DefaultVisualStudioRazorParser(
ForegroundDispatcher dispatcher,
VisualStudioDocumentTracker documentTracker,
ProjectSnapshotProjectEngineFactory projectEngineFactory,
ErrorReporter errorReporter,
VisualStudioCompletionBroker completionBroker)
{
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
if (documentTracker == null)
{
throw new ArgumentNullException(nameof(documentTracker));
}
if (projectEngineFactory == null)
{
throw new ArgumentNullException(nameof(projectEngineFactory));
}
if (errorReporter == null)
{
throw new ArgumentNullException(nameof(errorReporter));
}
if (completionBroker == null)
{
throw new ArgumentNullException(nameof(completionBroker));
}
_dispatcher = dispatcher;
_projectEngineFactory = projectEngineFactory;
_errorReporter = errorReporter;
_completionBroker = completionBroker;
_documentTracker = documentTracker;
_documentTracker.ContextChanged += DocumentTracker_ContextChanged;
}
public override string FilePath => _documentTracker.FilePath;
public override RazorCodeDocument CodeDocument => _codeDocument;
public override ITextSnapshot Snapshot => _snapshot;
public override ITextBuffer TextBuffer => _documentTracker.TextBuffer;
public override bool HasPendingChanges => _latestChangeReference != null;
// Used in unit tests to ensure we can be notified when idle starts.
internal ManualResetEventSlim NotifyForegroundIdleStart { get; set; }
// Used in unit tests to ensure we can block background idle work.
internal ManualResetEventSlim BlockBackgroundIdleWork { get; set; }
public override void QueueReparse()
{
// Can be called from any thread
if (_dispatcher.IsForegroundThread)
{
ReparseOnForeground(null);
}
else
{
Task.Factory.StartNew(ReparseOnForeground, null, CancellationToken.None, TaskCreationOptions.None, _dispatcher.ForegroundScheduler);
}
}
public void Dispose()
{
_dispatcher.AssertForegroundThread();
StopParser();
_documentTracker.ContextChanged -= DocumentTracker_ContextChanged;
StopIdleTimer();
_disposed = true;
}
// Internal for testing
internal void DocumentTracker_ContextChanged(object sender, ContextChangeEventArgs args)
{
_dispatcher.AssertForegroundThread();
if (!TryReinitializeParser())
{
return;
}
// We have a new parser, force a reparse to generate new document information. Note that this
// only blocks until the reparse change has been queued.
QueueReparse();
}
// Internal for testing
internal bool TryReinitializeParser()
{
_dispatcher.AssertForegroundThread();
StopParser();
if (!_documentTracker.IsSupportedProject)
{
// Tracker is either starting up, tearing down or wrongfully instantiated.
// Either way, the tracker can't act on its associated project, neither can we.
return false;
}
StartParser();
return true;
}
// Internal for testing
internal void StartParser()
{
_dispatcher.AssertForegroundThread();
// Make sure any tests use the real thing or a good mock. These tests can cause failures
// that are hard to understand when this throws.
Debug.Assert(_documentTracker.IsSupportedProject);
Debug.Assert(_documentTracker.ProjectSnapshot != null);
_projectEngine = _projectEngineFactory.Create(_documentTracker.ProjectSnapshot, ConfigureProjectEngine);
Debug.Assert(_projectEngine != null);
Debug.Assert(_projectEngine.Engine != null);
Debug.Assert(_projectEngine.FileSystem != null);
var projectDirectory = Path.GetDirectoryName(_documentTracker.ProjectPath);
_parser = new BackgroundParser(_projectEngine, FilePath, projectDirectory);
_parser.ResultsReady += OnResultsReady;
_parser.Start();
TextBuffer.Changed += TextBuffer_OnChanged;
}
// Internal for testing
internal void StopParser()
{
_dispatcher.AssertForegroundThread();
if (_parser != null)
{
// Detatch from the text buffer until we have a new parser to handle changes.
TextBuffer.Changed -= TextBuffer_OnChanged;
_parser.ResultsReady -= OnResultsReady;
_parser.Dispose();
_parser = null;
}
}
// Internal for testing
internal void StartIdleTimer()
{
_dispatcher.AssertForegroundThread();
lock (IdleLock)
{
if (_idleTimer == null)
{
// Timer will fire after a fixed delay, but only once.
_idleTimer = NonCapturingTimer.Create(state => ((DefaultVisualStudioRazorParser)state).Timer_Tick(), this, IdleDelay, Timeout.InfiniteTimeSpan);
}
}
}
// Internal for testing
internal void StopIdleTimer()
{
// Can be called from any thread.
lock (IdleLock)
{
if (_idleTimer != null)
{
_idleTimer.Dispose();
_idleTimer = null;
}
}
}
private void TextBuffer_OnChanged(object sender, TextContentChangedEventArgs args)
{
_dispatcher.AssertForegroundThread();
if (args.Changes.Count > 0)
{
// Idle timers are used to track provisional changes. Provisional changes only last for a single text change. After that normal
// partial parsing rules apply (stop the timer).
StopIdleTimer();
}
if (!args.TextChangeOccurred(out var changeInformation))
{
return;
}
var change = new SourceChange(changeInformation.firstChange.OldPosition, changeInformation.oldText.Length, changeInformation.newText);
var snapshot = args.After;
var result = PartialParseResultInternal.Rejected;
using (_parser.SynchronizeMainThreadState())
{
// Check if we can partial-parse
if (_partialParser != null && _parser.IsIdle)
{
result = _partialParser.Parse(change);
}
}
// If partial parsing failed or there were outstanding parser tasks, start a full reparse
if ((result & PartialParseResultInternal.Rejected) == PartialParseResultInternal.Rejected)
{
QueueChange(change, snapshot);
}
if ((result & PartialParseResultInternal.Provisional) == PartialParseResultInternal.Provisional)
{
StartIdleTimer();
}
}
// Internal for testing
internal void OnIdle(object state)
{
_dispatcher.AssertForegroundThread();
if (_disposed)
{
return;
}
OnNotifyForegroundIdle();
foreach (var textView in _documentTracker.TextViews)
{
if (_completionBroker.IsCompletionActive(textView))
{
// Completion list is still active, need to re-start timer.
StartIdleTimer();
return;
}
}
QueueReparse();
}
// Internal for testing
internal void ReparseOnForeground(object state)
{
_dispatcher.AssertForegroundThread();
if (_disposed)
{
return;
}
var snapshot = TextBuffer.CurrentSnapshot;
QueueChange(null, snapshot);
}
private void QueueChange(SourceChange change, ITextSnapshot snapshot)
{
_dispatcher.AssertForegroundThread();
_latestChangeReference = _parser.QueueChange(change, snapshot);
}
private void OnNotifyForegroundIdle()
{
if (NotifyForegroundIdleStart != null)
{
NotifyForegroundIdleStart.Set();
}
}
private void OnStartingBackgroundIdleWork()
{
if (BlockBackgroundIdleWork != null)
{
BlockBackgroundIdleWork.Wait();
}
}
private void Timer_Tick()
{
try
{
_dispatcher.AssertBackgroundThread();
OnStartingBackgroundIdleWork();
StopIdleTimer();
// We need to get back to the UI thread to properly check if a completion is active.
Task.Factory.StartNew(OnIdle, null, CancellationToken.None, TaskCreationOptions.None, _dispatcher.ForegroundScheduler);
}
catch (Exception ex)
{
// This is something totally unexpected, let's just send it over to the workspace.
Task.Factory.StartNew(() => _errorReporter.ReportError(ex), CancellationToken.None, TaskCreationOptions.None, _dispatcher.ForegroundScheduler);
}
}
private void OnResultsReady(object sender, BackgroundParserResultsReadyEventArgs args)
{
_dispatcher.AssertBackgroundThread();
// Jump back to UI thread to notify structure changes.
Task.Factory.StartNew(OnDocumentStructureChanged, args, CancellationToken.None, TaskCreationOptions.None, _dispatcher.ForegroundScheduler);
}
// Internal for testing
internal void OnDocumentStructureChanged(object state)
{
_dispatcher.AssertForegroundThread();
if (_disposed)
{
return;
}
var backgroundParserArgs = (BackgroundParserResultsReadyEventArgs)state;
if (_latestChangeReference == null || // extra hardening
_latestChangeReference != backgroundParserArgs.ChangeReference)
{
// In the middle of parsing a newer change or about to parse a newer change.
return;
}
if (backgroundParserArgs.ChangeReference.Snapshot != TextBuffer.CurrentSnapshot)
{
// Changes have impacted the snapshot after our we recorded our last change reference.
// This can happen for a multitude of reasons, usually because of a user auto-completing
// C# statements (causes multiple edits in quick succession). This ensures that our latest
// parse corresponds to the current snapshot.
QueueReparse();
return;
}
_latestChangeReference = null;
_codeDocument = backgroundParserArgs.CodeDocument;
_snapshot = backgroundParserArgs.ChangeReference.Snapshot;
_partialParser = new RazorSyntaxTreePartialParser(CodeDocument.GetSyntaxTree());
var documentStructureChangedArgs = new DocumentStructureChangedEventArgs(
backgroundParserArgs.ChangeReference.Change,
backgroundParserArgs.ChangeReference.Snapshot,
backgroundParserArgs.CodeDocument);
DocumentStructureChanged?.Invoke(this, documentStructureChangedArgs);
}
private void ConfigureProjectEngine(RazorProjectEngineBuilder builder)
{
builder.Features.Add(new VisualStudioParserOptionsFeature(_documentTracker.EditorSettings));
builder.Features.Add(new VisualStudioTagHelperFeature(_documentTracker.TagHelpers));
}
private class VisualStudioParserOptionsFeature : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
{
private readonly EditorSettings _settings;
public VisualStudioParserOptionsFeature(EditorSettings settings)
{
_settings = settings;
}
public int Order { get; set; }
public void Configure(RazorCodeGenerationOptionsBuilder options)
{
options.IndentSize = _settings.IndentSize;
options.IndentWithTabs = _settings.IndentWithTabs;
}
}
private class VisualStudioTagHelperFeature : ITagHelperFeature
{
private readonly IReadOnlyList<TagHelperDescriptor> _tagHelpers;
public VisualStudioTagHelperFeature(IReadOnlyList<TagHelperDescriptor> tagHelpers)
{
_tagHelpers = tagHelpers;
}
public RazorEngine Engine { get; set; }
public IReadOnlyList<TagHelperDescriptor> GetDescriptors()
{
return _tagHelpers;
}
}
}
}