I have a IWpfTextViewConnectionListener based extension. I need access to the EnvDTE.Document associated with the IWpfTextView I’m being given. I’ve tried storing _DTE.ActiveDocument, but this only works on files that were not already open previously (if the file was open already, it’s null).
How can I get the document object for the text view that is showing?
From an
IWpfTextView.TextBuffer, you can get theITextBuffer. From that, you can useITextDocumentFactoryService.TryGetTextDocument()to get the matchingITextDocument, if one exists. Then,ITextDocument.FilePathgives you the path to the file.You can then enumerate through the
DTE2.Documentscollection to find the open document with the same path.More on
ITextDocumentFactoryService: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.itextdocumentfactoryservice.aspx