How can I disable specific Visual Studio Commands from my Package?

59 views Asked by At

I am creating a package which requires the text white space be in a specific format. Without arguing about the reason why lets just assume this is an okay requirement. I must then prevent visual studio from auto-updating the code.

This is fairly easy from an open document where I can add a command filter and prevent the command from being executed with the following code.

[Export(typeof(IVsTextViewCreationListener))]

internal sealed class MyListener : IVsTextViewCreationListener
{
    public void VsTextViewCreated(IVsTextView textViewAdapter)
    {
        var filter = PackageManager.Kernel.Get<CommandFilter>();
        if (ErrorHandler.Succeeded(textViewAdapter.AddCommandFilter(filter,out var next)))
            filter.Next = next;
    }
 }

public class CommandFilter : IOleCommandTarget
{
    public IOleCommandTarget Next { get; set; }

    public const uint CmdEditFormat = 0x8F;
    public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
    {

        switch (prgCmds[0].cmdID)
        {
            case CmdEditFormat:
                return VSConstants.E_ABORT;

        return Next.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);

    }
}

The Edit.FormatDocument command id is blocked as I require. I could also add Edit.FormatSelection or any other commands that may affect the white-space. This is all well and good for open documents which I mark with this special need.

However, when it comes to add-ins like Resharper which updated files in a multitude of ways without actually opening the files themselves there becomes trouble. I need to also block some of these commands, once I find which ones are volatile to my implementation.

So the question is can I setup some sort of CommandFilter application-wide so I can catch them in the act? This would allow me cleanup command of Resharper and then restore the files that contain formatting as needed.

Another possibility is if I can find where the Resharper options file is and updated it somehow to exclude said files. I know this is manually possible.

0

There are 0 answers