Create a Context Action
What you should know beforehand:
The purpose of context actions is to quickly introduce a minor change to the code, like changing access modifiers, convert
for, and so on. When a context action is available for the current caret position, ReSharper displays the "hummer" action indicator. The list of possible context actions is displayed after a user presses Alt+Enter.
For example, let's create a very simple context action that will be applicable to variable declarations. If a variable name contains any character in uppercase, the context action will suggest to convert the name to lowercase (of course, from the point of functionality, such action is absolutely useless).
The easiest way to create a context action is to inherit from the
The context action class must be marked with the
Groupdefines the scope of the action (C# files in our case).
Disabledspecifies whether the action is disabled by default.
Prioritysets the position of the action in the actions list.
The context data provider
LanguageIndependentContextActionDataProviderobtained in the constructor provides a super-helpful method
GetSelectedElementthat returns the tree node under the caret. The provider has also methods that allow you to get current offset, selection range and so on.
Textproperty defines the text that will be shown in the actions list.
IsAvailableis used to check whether the action is available for the current caret position. If
false, the action is not shown in the list of possible context actions.
ExecutePsiTransactionreturns the action that is executed when the context action is selected.
Note that we use
RenameRefactoringServiceto change variable's name. The service performs seamless renaming of a variable everywhere in the code.