How to implement right click menu item\action

Is there a sample that demostrates how to refactor a method\parameter similar to the R# replace a IList<T> with IEnumerable<T>

action 1.png

I can understand how to do the highlighting of the actual code but I'm unsure how to add a right click method (and action) to do the actual replacement.

action 2.png



ta


Ollie

2 comments
Comment actions Permalink

Hi Ollie. The menu that gets displayed is from a class that implements IQuickFix and is marked with the [QuickFix] attribute (although the attribute might be optional). The class gets wired up due to what parameters its constructors take - there is one constructor per highlighting that it can fix. In other words, you have code that adds a highlighting instance for the IList<int> in the example screenshot, this would be ParameterTypeCanBeEnumerableGlobalWarning (which derives from ParameterTypeCanBeEnumerableWarningBase), and you have a your QuickFix class FixParameterTypeFix, which has a constructor that takes ParameterTypeCanBeEnumerableWarningBase. The system now knows that this QuickFix can handle this warning.

The QuickFix can then use the highlighting object to get the information it needs to work, such as the declaration and the suggested type. It can add one or more "bulb items", which are the items in the menu, and can then do whatever it needs to in response to one being selected. In this case, it creates a PSI transaction, and changes the type in the PSI, which updates the document. The code in FixParameterTypeFix shows how the file can be changed.

0
Comment actions Permalink

thanks


ta

Ollie

0

Please sign in to leave a comment.