locked
How do I add a context menu item in the VS2010 Text Editor from a MEF Component ? RRS feed

  • Question

  • Hi,
    I'm writing a MEF Component to allow rename refactoring of identifiers in F#. I'm trying to find a way to add a context menu item in the VS2010 Text editor from this component. All the samples I could find on the internet add menu items using VSPackages instead of MEF. Is it possible to add a menu item from a MEF Component. And if is not how do I communicate from a VSPackage to a MEF component ? What's the proper way to do this ?

    Cheers,
    Edmondo

    Wednesday, May 4, 2011 9:09 PM

Answers

  • You can't add commands via MEF. 

    As for communicating from your package to your MEF component, well, that is up to you :)  You can have some singleton instance of your MEF component or some way to find the 'active' one say from the active view, or you can import it in your package (you have to satisfy the import explicitly since packages don't undergo MEF composition), etc...

    Ryan

    Wednesday, May 4, 2011 9:59 PM

All replies

  • You can't add commands via MEF. 

    As for communicating from your package to your MEF component, well, that is up to you :)  You can have some singleton instance of your MEF component or some way to find the 'active' one say from the active view, or you can import it in your package (you have to satisfy the import explicitly since packages don't undergo MEF composition), etc...

    Ryan

    Wednesday, May 4, 2011 9:59 PM
  • Also, you can do something like this to 'inject' a command handler into the editor's command chain, said handler could even be your MEF editor extension if you wanted...

    http://msdn.microsoft.com/en-us/library/dd885474.aspx


    Then you just need some VSCT in a package to contribute the actual command to the editor context menu and you should be good to go.

    Ryan

    Wednesday, May 4, 2011 10:01 PM
  • Ok,

    Let's say I do the entire thing as a VsPackage. How do I get access to the following interfaces ? Or what are the equivalents in the VsPackage world ?

     ITextView
     ITextBuffer
     ITextSearchService
     ITextStructureNavigator
     ITextBufferUndoManagerProvider

     

    Cheers,

    Edmondo

    Wednesday, May 4, 2011 11:46 PM
  • Well there is no need to 'do the entire thing as a VSPackage', you can leave your MEF stuff as it is and simply use the package to contribute the command.  Handler definition can either be done at the package level or via the 'injection' technique I linked above.  All the interfaces you mention are new editor interfaces in 2010, you can use IVsEditorAdaptersFactoryService to map from the shim/older interfaces (like IVsTextView, IVsTextBufer) to the newer interfaces (ITextView, ITextBuffer, etc...)

    Ryan

    • Proposed as answer by Anonymous8293 Saturday, May 7, 2011 1:39 PM
    Thursday, May 5, 2011 3:00 PM