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 the ITextBuffer
. From that, you can use ITextDocumentFactoryService.TryGetTextDocument()
to get the matching ITextDocument
, if one exists. Then, ITextDocument.FilePath
gives you the path to the file.
You can then enumerate through the DTE2.Documents
collection to find the open document with the same path.
More on ITextDocumentFactoryService
: http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.text.itextdocumentfactoryservice.aspx