How to get Current ActiveDocument in Visual Studio Extension using MEF?

后端 未结 1 1417
后悔当初
后悔当初 2021-01-14 03:25

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

相关标签:
1条回答
  • 2021-01-14 04:23

    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.

    0 讨论(0)
提交回复
热议问题