VSIX: Adding a Menu Item to the Visual Studio Editor Context Menu
Asked Answered
K

3

11

I have an internal extension I'd like to add to Visual Studio that should hook up to the Editor Context menu - regardless of what type of file is open. I can handle enabling/visibility dynamically but essentially I'd like it to be accessible on any type of editor file.

I've not been able to find the right parent command/group ids to manage to get a custom button to display on the editor context menu. I suspect there's not a single Id but several but any guidance on what I should be looking at. Having a hard time figuring out what the proper parent command Id is to hook up to the editor context menu.

Specifically I need to be able to add View in Browser option to files that Visual Studio doesn't recognize as HTML/Web files (even though they are mapped to the appropriate editors).

Related: Is there anyway to reasonable way to discover the menu command and group names? Poking around in the SharedCommandPlace.vsct is as close as I've come but even that is proving to be very difficult to match to actual menu items.

K2 answered 1/8, 2015 at 7:24 Comment(4)
Not sure if this still works in VS2013/2015 and in your specific use case, but might be worth a try: blogs.msdn.com/b/dr._ex/archive/2007/04/17/… .. just make sure to use the proper registry key for your corresponding VS versionTore
Looks like this isn't working VS2015, but it does work in VS2013. Once I have GUID and Cmd ID how do I map that to a Command Group definition required for a package?K2
Rick, it's been a while for me since I last fiddled with that part of VS extensibility, but basically there typically is a generic 'parent' guid for the editor context menu.. afaik there IS a common one for all file types, I just don't know it from the top of my head for sure.. it might be this one though: msdn.microsoft.com/en-US/library/…Tore
Then again, this SO post mentions things are different for js/html/css files in vs #3673621 . Le sigh. Once you got the(se) parent id(s), inserting your own entry and hooking up your own command is explained i.e. here: msdn.microsoft.com/en-us/library/bb165739.aspxTore
K
18

I was able to figure out the right command groups for the context menu. It turns out the various editors all use separate context ids and so have to be managed as separate menus so this gets messy quick.

Steps

  1. I used the HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\14.0\General key and EnableVSIPLogging value of 1 to enable logging.
  2. I then navigated into the editor and with the mouse on an empty area press CTRL-SHIFT and then right click the mouse

This gives the info a menu group like and it looks like this:

---------------------------
VSDebug Message
---------------------------
Menu data:
    Guid = {D7E8C5E1-BDB8-11D0-9C88-0000F8040A53}
    GuidID = 358
    CmdID = 53
    Type = 0x00000400
    Flags = 0x00000000
    NameLoc = ASPX Context
---------------------------
OK   
---------------------------

The important values are the GUID and the CommandID.

Add the Guid and Command ID under Symbols like this to register the command set mapping the Guid to the CommandSet and the CommandId to the context menu values:

<GuidSymbol name="aspxContextCommandSet" value="{D7E8C5E1-BDB8-11D0-9C88-0000F8040A53}">
  <IDSymbol name="aspxContextMenu" value="0x0035"/>
</GuidSymbol>

Note that the value maps to the CommandID represented as a hex value.

Then reference this group as a parent for your command group (MyMenuGroup) in the Groups section:

<Group guid="guidViewInBrowserPackageCmdSet" id="MyMenuGroup" priority="0x0000">
  <Parent guid="aspxContextCommandSet" id="aspxContextMenu"/>
</Group>

You reference the menu group you create for you command buttons and point at the context menu created in the previous step.

If you want to do this for multiple editors (ie. the ASPX, HTML, and Code editors for example as I do) you repeat this process for each of your editors by adding both the GuidSymbol and the Group.You'll end up with multiple Group entries for the same MenuGroup point at a different parent and all will activate appropriately.

Works great, but you'll probably have to make sure you filter your OleMenuCommand objects with a BeforeQueryStatus event handler to ensure the menu shows only when you actually can handle.

K2 answered 2/8, 2015 at 6:24 Comment(1)
Excellent idea to use EnableVSIPLoggingMicah
H
8

I needed the same thing and I used:

<Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_CODEWIN"/>

So, I just changed the id. See: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.shell.vsmenus.idm_vs_ctxt_codewin.aspx

Hygrograph answered 1/9, 2015 at 10:42 Comment(1)
Exactly what I was looking for. Thanks!Leatherwood
O
3

The EnableVSIPLogging registry value still works for VS 2015. You just need to add an EnableVSIPLogging DWORD set to 1, under HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\14.0\General.

If the CTRL+SHIFT + menu popup or menu select, doesn't result in that dialog, chances are the menu item in question is not implemented as a VSCT resource.

That being said, you may need to experiment a little, as editor and designers are not required to use the same context menu that the code editor uses.

Also, you might want to try out Mads "Extensibility Tools" extension at https://visualstudiogallery.msdn.microsoft.com/ab39a092-1343-46e2-b0f1-6a3f91155aa6 (2017) or https://marketplace.visualstudio.com/items?itemName=MadsKristensen.ExtensibilityEssentials2019 (2019).

He's added a nice autocomplete for VSCT files that's pretty useful.

Objectify answered 2/8, 2015 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.