I\'m working on Visual Studio 2013 Extension using MEF while trying to read Active Document Content Type and Code. Presently it only reads at Opening Time of the Document/Pr
Fortunately, I got what I was trying to achieve. A helper solution is already posted here: I used the helper method in dte2.Events.WindowsEvents.WindowActived and getting IVsTextView object to retrieve the Text Buffer. Here is my Code snippet of WindowActivated event:
void WindowEvents_WindowActivated(EnvDTE.Window GotFocus, EnvDTE.Window LostFocus)
{
if (null != GotFocus.Document)
{
Document curDoc = GotFocus.Document;
Debug.Write("Activated : " + curDoc.FullName);
this.IvsTextView=GetIVsTextView(curDoc.FullName); //Calling the helper method to retrieve IVsTextView object.
if (IvsTextView != null)
{
IvsTextView.GetBuffer(out curDocTextLines); //Getting Current Text Lines
//Getting Buffer Adapter to get ITextBuffer which holds the current Snapshots as wel..
Microsoft.VisualStudio.Text.ITextBuffer curDocTextBuffer = AdaptersFactory.GetDocumentBuffer(curDocTextLines as IVsTextBuffer);
Debug.Write("\r\nContentType: "+curDocTextBuffer.ContentType.TypeName+"\nTest: " + curDocTextBuffer.CurrentSnapshot.GetText());
}
}
}
This is now working with all the code documents opened in VS Editor. Hope this would help others like me.