Adding a Visual Studio toolbar button for a command that is only available as a keyboard shortcut
Asked Answered
M

1

5

This question relates to this ReSharper YouTrack issue.

In Visual Studio 2010 with ReSharper 7.1.1 installed, if I go to Tools > Options > Environment > Keyboard, there is a command called ReSharper_SilentCleanupCode.

I would like to bind this command to a toolbar button.

This seems to be impossible using Tools > Customize > Commands because the only commands available within this dialog are for actions that already have an associated menu item. The particular ReSharper command I'm interested in (Silent Code Cleanup) doesn't appear in any menu, so it cannot be assigned to a toolbar button using the "GUI".

Is there any other way to bind a keyboard-only command to a toolbar button? (One of ReSharper's programmers thought the "VS script editor" could be used, but I'm not having any luck finding info on this.)

Edit

I should have mentioned this in the first place. While azhrei's macro solution is great for Visual Studio 2010, it will break once I upgrade to VS 2012, because macros are no longer supported. If someone has a solution that will continue to work in VS 2012, that would be preferable. (Or perhaps VS 2012 toolbars don't have the same limitation in the first place?)

Murrhine answered 13/3, 2013 at 19:34 Comment(0)
S
5

Add a macro that executes the command, then add the macro to a toolbar.

This works because it makes the keyboard-only command appear in the Macros menu in the Customize Commands dialog.

Details

Add a macro which does this:

    Sub _ReSharper_SilentCleanupCode()
        DTE.ExecuteCommand("ReSharper_SilentCleanupCode")
    End Sub

Put this macro in a module which appears in Customize..Commands..AddCommand..Categories..Macros, such as Samples or MyMacros.RecordingModule, but not MyMacros.Module1 (the default when using the macro IDE).

Go to Tools..Customize..Command and select the Toolbar you want.

Now Add Command... and select the Macros category.

Select your Macros.Samples._ReSharper_SilentCleanupCode macro.

Click Modify Selection and change the name to #-) or whatever text makes you think ReSharper Silent Code Cleanup without being too long for your toolbar. :-)

I tried this with Visual Studio 2010 and ReSharper 7.1.2.

Edit

Visual Commander is a apparently way to get this going on VS2012 as well - see comments below for more.

Surrey answered 14/3, 2013 at 2:33 Comment(10)
+1, thanks. One question...why do you suggest not putting the macro in MyModule? First of all, did you mean MyModule (a module I don't seem to have) or MyMacros (the default macro project)? I tried putting my macro in MyMacros > Recording Module, and this seems to work, even if I switch between different .vssettings files.Murrhine
Thanks. I did mean MyMacros, the default project. Just tried using a recorded macro - works for me too. Updated answer.Surrey
As for VS2012, it does look like it has the same limitation. Try #12483120 - the accepted answer looks promising. Once you've added a command you could then customize.Surrey
Ahh, now that's what I was really looking for. I just didn't know the right search terms to use. Thanks for digging that up!Murrhine
You can re-use this macro in VS 2012 as a Visual Commander command and then add it to a toolbar: visualstudiogallery.msdn.microsoft.com/…Gonzalez
@SergeyVlasov, thanks for the tip, I will check it out! By the way, if you want people to see your comment, it's a good idea to put their name prefixed by "@" (like I did at the beginning of this comment), so they receive a notification. I just happened to notice your comment by chance because someone drew my attention back to this question.Murrhine
@SergeyVlasov, tried Visual Commander and it works perfectly. One question: where on my hard drive does the code get stored?Murrhine
@DanM, the code in Visual Commander is stored in "%APPDATA%\Sergey Vlasov\Visual Commander\1.0\snippets.vcmd"Gonzalez
@SergeyVlasov Thanks for the quick reply! I'm going to leave a followup comment on the Visual Studio Gallery Q/A for Visual Commander.Murrhine
I used the following equivalent in C#: using EnvDTE; using EnvDTE80; public class ResharperMacro : VisualCommanderExt.ICommand {public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package) {DTE.ExecuteCommand("ReSharper_ToggleSuspended");} }Amenity

© 2022 - 2024 — McMap. All rights reserved.