Call AdviseRunningDocTableEvents on UI thread take 2.

- Originally when this work was done I attempted to call the advise in the ctor; however, that in practice didn't work out so well. Therefore, given that the editor document manager is always "started" / requested elsewhere we can lazily advise the running document table when already on the UI thread.

Fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1441597
This commit is contained in:
N. Taylor Mullen 2021-12-06 17:47:24 -08:00
Родитель 51d34f58a8
Коммит 27574343c5
1 изменённых файлов: 29 добавлений и 3 удалений

Просмотреть файл

@ -25,6 +25,7 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
private readonly Dictionary<uint, List<DocumentKey>> _documentsByCookie;
private readonly Dictionary<DocumentKey, uint> _cookiesByDocument;
private bool _advised;
public VisualStudioEditorDocumentManager(
ProjectSnapshotManagerDispatcher projectSnapshotManagerDispatcher,
@ -52,9 +53,6 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
_runningDocumentTable = (IVsRunningDocumentTable4)runningDocumentTable;
_editorAdaptersFactory = editorAdaptersFactory;
var hr = runningDocumentTable.AdviseRunningDocTableEvents(new RunningDocumentTableEventSink(this), out _);
Marshal.ThrowExceptionForHR(hr);
_documentsByCookie = new Dictionary<uint, List<DocumentKey>>();
_cookiesByDocument = new Dictionary<DocumentKey, uint>();
}
@ -68,6 +66,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
// Check if the document is already open and initialized, and associate a buffer if possible.
uint cookie;
if (_runningDocumentTable.IsMonikerValid(filePath) &&
@ -90,6 +90,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
{
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
var cookie = _runningDocumentTable.GetDocumentCookie(document.DocumentFilePath);
if (cookie != VSConstants.VSCOOKIE_NIL)
{
@ -101,6 +103,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
{
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
var key = new DocumentKey(document.ProjectFilePath, document.DocumentFilePath);
if (_cookiesByDocument.TryGetValue(key, out var cookie))
{
@ -112,6 +116,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
{
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
lock (Lock)
{
// Casts avoid dynamic
@ -148,6 +154,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
{
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
var textBuffer = _editorAdaptersFactory.GetDocumentBuffer(vsTextBuffer);
if (textBuffer != null)
{
@ -164,6 +172,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
{
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
lock (Lock)
{
for (var i = 0; i < documents.Length; i++)
@ -177,6 +187,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
{
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
lock (Lock)
{
if (!_documentsByCookie.TryGetValue(cookie, out var documents))
@ -201,6 +213,8 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
{
JoinableTaskContext.AssertUIThread();
EnsureDocumentTableAdvised();
// Ignore changes is casing
if (FilePathComparer.Instance.Equals(fromFilePath, toFilePath))
{
@ -223,6 +237,18 @@ namespace Microsoft.VisualStudio.Editor.Razor.Documents
}
}
private void EnsureDocumentTableAdvised()
{
JoinableTaskContext.AssertUIThread();
if (!_advised)
{
_advised = true;
var hr = ((IVsRunningDocumentTable)_runningDocumentTable).AdviseRunningDocTableEvents(new RunningDocumentTableEventSink(this), out _);
Marshal.ThrowExceptionForHR(hr);
}
}
private void TrackOpenDocument(uint cookie, DocumentKey key)
{
if (!_documentsByCookie.TryGetValue(cookie, out var documents))