How can I add “Toggle On/Off Code Lens” as a quick command button on Visual Studio?

975 Views Asked by At

I would like to have a quick command button displayed on my Menu bar to turn on/off the Code Lens. I understand there is a keyboard shortcut available for it:

The EditorContextMenus.CodeLens.CodeLensOptions command for keyboard shortcut, but I cannot make it as a quick command button.

It will be very awesome I can see a quick button on my VS Menu to quickly toggle it. (instead of remember it as keyboard shortcut)

How can I achieve this?

1

There are 1 best solutions below

2
On

Apparently this is as hard as it looks - even though the command definitely exists, it is nowhere to be found in any of the menus when clicking "Add Command...". But this is a fixable oversight.

Here's what I did:

  • Made a copy %LOCALAPPDATA%\Microsoft\VisualStudio\<version>\Settings\CurrentSettings.vssettings
  • Added a random command to my menu bar
  • Removed "CodeLens Options..." from the respective menu using the command editor:
    enter image description here
  • Closed all instances of Visual Studio for changes to be flushed to disk
  • Ran an XML formatter on both files and then a diff on them
    (note: some diff tools like Beyond Compare have a built-in XML formatter)
  • The diff revealed the command's GUID and CmdPri:
    <remove Cmd="{7660636A-8C08-4430-A56F-146E42A8B2C5}:00000001" CmdPri="01000000" Group="{7660636A-8C08-4430-A56F-146E42A8B2C5}:00002000" GroupPri="01000000" Menu="{7660636A-8C08-4430-A56F-146E42A8B2C5}:00003000" IsPartialRemoval="false"/>
    
  • Modified my new command's Cmd and CmdPri to be that of the removed CodeLens command:
    <add Cmd="{7660636A-8C08-4430-A56F-146E42A8B2C5}:00000001" CmdPri="01000000" Group="{D309F791-903F-11D0-9EFC-00A0C911004F}:00000174" GroupPri="09000000" Menu="{D309F791-903F-11D0-9EFC-00A0C911004F}:00000001"/>
    

And voila - the command is now residing in the appropriate toolbar and you can move/modify it as you please:
enter image description here
You can now also go and reset the original CodeLens menu that you have removed it from.