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 pathDefaultRazorDocumentManager.cs
111 lines (94 loc) · 3.9 KB
/
DefaultRazorDocumentManager.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
// 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.ComponentModel.Composition;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Razor;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
namespace Microsoft.VisualStudio.Editor.Razor
{
[System.Composition.Shared]
[Export(typeof(RazorDocumentManager))]
internal class DefaultRazorDocumentManager : RazorDocumentManager
{
private readonly ForegroundDispatcher _foregroundDispatcher;
private readonly RazorEditorFactoryService _editorFactoryService;
[ImportingConstructor]
public DefaultRazorDocumentManager(
ForegroundDispatcher dispatcher,
RazorEditorFactoryService editorFactoryService)
{
if (dispatcher == null)
{
throw new ArgumentNullException(nameof(dispatcher));
}
if (editorFactoryService == null)
{
throw new ArgumentNullException(nameof(editorFactoryService));
}
_foregroundDispatcher = dispatcher;
_editorFactoryService = editorFactoryService;
}
public override void OnTextViewOpened(ITextView textView, IEnumerable<ITextBuffer> subjectBuffers)
{
if (textView == null)
{
throw new ArgumentNullException(nameof(textView));
}
if (subjectBuffers == null)
{
throw new ArgumentNullException(nameof(subjectBuffers));
}
_foregroundDispatcher.AssertForegroundThread();
foreach (var textBuffer in subjectBuffers)
{
if (!textBuffer.IsRazorBuffer())
{
continue;
}
if (!_editorFactoryService.TryGetDocumentTracker(textBuffer, out var documentTracker) ||
!(documentTracker is DefaultVisualStudioDocumentTracker tracker))
{
Debug.Fail("Tracker should always be available given our expectations of the VS workflow.");
return;
}
tracker.AddTextView(textView);
if (documentTracker.TextViews.Count == 1)
{
tracker.Subscribe();
}
}
}
public override void OnTextViewClosed(ITextView textView, IEnumerable<ITextBuffer> subjectBuffers)
{
if (textView == null)
{
throw new ArgumentNullException(nameof(textView));
}
if (subjectBuffers == null)
{
throw new ArgumentNullException(nameof(subjectBuffers));
}
_foregroundDispatcher.AssertForegroundThread();
// This means a Razor buffer has be detached from this ITextView or the ITextView is closing. Since we keep a
// list of all of the open text views for each text buffer, we need to update the tracker.
//
// Notice that this method is called *after* changes are applied to the text buffer(s). We need to check every
// one of them for a tracker because the content type could have changed.
foreach (var textBuffer in subjectBuffers)
{
DefaultVisualStudioDocumentTracker documentTracker;
if (textBuffer.Properties.TryGetProperty(typeof(VisualStudioDocumentTracker), out documentTracker))
{
documentTracker.RemoveTextView(textView);
if (documentTracker.TextViews.Count == 0)
{
documentTracker.Unsubscribe();
}
}
}
}
}
}