Extending the Visual Studio 2010 editor by adding a context menu item to manipulate selected text
Asked Answered
W

2

15

I'd like to create an extension to Visual Studio that will allow someone to select any text in an editor, right-click to get a context menu, and then perform some action on the text by clicking on my custom menu item.

Let's say for example that my custom menu item is called "Email...". The new context menu for the editor might look something like this:

Breakpoint
Run to Cursor
Cut
Copy
Paste
Email...
Outlining

I'd also like to add a popup (adornment?) with options. In this case it could be things like To, Subject, and CC fields.

I think that some of this can be done with MEF using editor extensions, like the popup and the actual code for firing off an email. But it looks like I need a VSPackage to add the context menu item. From what I've read, I can bundle the MEF piece and the VSPackage together.

This is where I get stuck. I need an in-depth reference with details to implement both parts, but I also need information on how to integrate them. MSDN has me going around in circles.

Is there any good, solid example code or documentation (even books) that I can learn from?

Winchester answered 5/6, 2011 at 1:53 Comment(1)
Though not a direct answer, I still wanted to share a useful link to resource that delves into VS package development and extensions.Antilepton
W
8

I totally scrapped my idea of dealing with MEF, VSIX files, etc. Instead I went with a Visual Studio add-in which was much easier.

I combined code from various resources like Adding Menu Items to context menus inside the Visual Studio .NET IDE and Visual Studio Add-in : Creating Context Menus which are somewhat old and needed cleaning up, but they worked fine.

Winchester answered 10/7, 2011 at 20:31 Comment(2)
Good deal, but just so you're aware, in VS 2013 add-ins are deprecated. So it looks like they're going to force us to use extensions.Bosley
This answer is more or less deprecated now, to suffice with the FAQ it shouldn't be the selected answer.Pasco
M
19

1) You can create always get a reference to EnvDTE in an extension.

m_dte = (EnvDTE.DTE)this.GetService(typeof(EnvDTE.DTE));

2) To directly add, menu item in the extension paradigm. You just need to modify the ".VSCT" file. First, the menu command is defined in a menu group. In the VSCT file, the group definition looks like this as generated:

<Group guid="guidCodeBlogCmdSet" id="MyMenuGroup" priority="0x0600">
<Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
</Group>

To move the “Email” menu entry to the context menu, all we need to do is specify the code window context editor as the parent for the menu group, using the identifier IDM_VS_CTX_CODEWIN:

<Group guid="guidCodeBlogCmdSet" id="MyMenuGroup" priority="0x0600">
<Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_CODEWIN"/>
</Group>

I based this example from a neat tutorial on how to auto-tweet or blog an code snippet from visual studio: http://solutions.developer.com/ms/web-dev/visual-studio/codeblog-writing-a-blogging-extension-for-visual-studio-2010.html

Massimiliano answered 18/3, 2012 at 20:41 Comment(1)
Thanks, gameweld. That ID should be "IDM_VS_CTXT_CODEWIN" though, not "IDM_VS_CTX_CODEWIN".Shiner
W
8

I totally scrapped my idea of dealing with MEF, VSIX files, etc. Instead I went with a Visual Studio add-in which was much easier.

I combined code from various resources like Adding Menu Items to context menus inside the Visual Studio .NET IDE and Visual Studio Add-in : Creating Context Menus which are somewhat old and needed cleaning up, but they worked fine.

Winchester answered 10/7, 2011 at 20:31 Comment(2)
Good deal, but just so you're aware, in VS 2013 add-ins are deprecated. So it looks like they're going to force us to use extensions.Bosley
This answer is more or less deprecated now, to suffice with the FAQ it shouldn't be the selected answer.Pasco

© 2022 - 2024 — McMap. All rights reserved.