public void DocumentOpened(uint cookie) { JoinableTaskContext.AssertUIThread(); lock (Lock) { // Casts avoid dynamic if ((object)_runningDocumentTable.GetDocumentData(cookie) is IVsTextBuffer vsTextBuffer) { var filePath = _runningDocumentTable.GetDocumentMoniker(cookie); if (!TryGetMatchingDocuments(filePath, out var documents)) { // This isn't a document that we're interesting in. return; } var textBuffer = _editorAdaptersFactory.GetDataBuffer(vsTextBuffer); if (textBuffer == null) { // The text buffer has not been created yet, register to be notified when it is. VsTextBufferDataEventsSink.Subscribe(vsTextBuffer, () => BufferLoaded(vsTextBuffer, filePath)); return; } // It's possible that events could be fired out of order and that this is a rename. if (_documentsByCookie.ContainsKey(cookie)) { DocumentClosed(cookie, exceptFilePath: filePath); } BufferLoaded(textBuffer, filePath, documents); } } }
public static void Subscribe(IVsTextBuffer vsTextBuffer, Action action) { if (vsTextBuffer is null) { throw new ArgumentNullException(nameof(vsTextBuffer)); } if (action is null) { throw new ArgumentNullException(nameof(action)); } var connectionPointContainer = (IConnectionPointContainer)vsTextBuffer; var guid = typeof(IVsTextBufferDataEvents).GUID; connectionPointContainer.FindConnectionPoint(ref guid, out var connectionPoint); var sink = new VsTextBufferDataEventsSink(connectionPoint, action); connectionPoint.Advise(sink, out sink._cookie); }