Pergunta

I am looking for a way to have my extension check if the currently opened window in visual studio 2012 is one, where the user can write code (or any kind of text, really).

To check if the currently opened window has changed, I use

_DTE.Events.WindowEvents.WindowActivated.

This gives me the EnvDTE.Window that received the focus.

When I look at the properties of that window while debugging, and I look at EnvDTE.Window.Document.Type and it's value is "Text".

However, if I stop debugging and try to access the Document.Type property, it does not exist.

If I look for this property in the documentation of EnvDTE.Window.Document, its description says

Infrastructure. Microsoft Internal Use Only.

So now I am looking for any advice on how I could check if the currently active window is one, where I can write code (or anything else), or some other kind of document (like the solution properties for example).

Edit:

I also tried checking Window.Type and Window.Kind of the active window, but they just tell me that it's a document, not making any differentiation between a resource file, an image file or an actual source file, which is what I'm trying to find out.

Edit²:

The reason why I want to check if the current document is one where I can write code in, is because I want my extension to store information about some of those documents and I want to modify the right-click context menu based on the information I have stored, if any.

Foi útil?

Solução

It is not a "real" answer, but you can follow status of VS GoTo command - it is available only for text editors:

bool isCodeWindow = IsCommandAvailable("Edit.GoTo");

private bool IsCommandAvailable(string commandName)
{
    EnvDTE80.Commands2 commands = dte.Commands as EnvDTE80.Commands2;
    if (commands == null)
        return false;

    EnvDTE.Command command = commands.Item(commandName, 0);
    if (command == null)
        return false;

    return command.IsAvailable;
}

Outras dicas

You can check to see if the document is a 'TextDocument'

bool isCodeWindow = dte.CurrentDocument.Object() is EnvDTE.TextDocument;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top