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/ProjectItem in the Editor. Once these are opened, it doesn't read these again whenever we switch between opened Document Tabs.
Requirement: I want this extension to read the Content Type and Code Text of current Active Document.
Updated:
Problem: I know, using EnvDTE80.DTE2.ActiveWindow, I can get currently focused document, but I'm confused here that how to call this code to read the Currently Active Document/Window things? Let's say if we have 10 Documents, the active document (which got current focus) needs to be read by the this extension. And here VsTextViewCreated is only called whenever we open a new document or the one closed before i.e Text View is Created. It won't be called upon already open documents (i.e. Text View already created) and so we won't be able to get updated wpfTextView object upon moving the focus on other already open documents. And I'm confused here how to call this using DTE2.ActiveDocument or DTE2.ActiveWindow event handlers.
Question:
Is this possible in MEF, without using DTE?
Is there any Interface dealing with TextViews already present in VS editor?
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Text.Editor;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Utilities;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using System.Diagnostics;
namespace VSIXProject_Test
{
[Export(typeof(IVsTextViewCreationListener))]
[ContentType("code")]
[TextViewRole(PredefinedTextViewRoles.Editable)]
class VsTextViewCreationListener : IVsTextViewCreationListener
{
[Import]
IVsEditorAdaptersFactoryService AdaptersFactory = null;
public void VsTextViewCreated(IVsTextView textViewAdapter)
{
var wpfTextView = AdaptersFactory.GetWpfTextView(textViewAdapter);
if (wpfTextView == null)
{
Debug.Fail("Unable to get IWpfTextView from text view adapter");
return;
}
Debug.Write(wpfTextView.TextBuffer.ContentType.TypeName);
}
}
}