Highlighted Code Action


  • Hello, I am trying to invoke Code Action when source code is highlighted by the user in the editor, such as for use with the extract method refactoring, IExtractMethodService. Can someone point me in the right direction?

    Is there something within Roslyn that helps? I've been looking at "Inside the Editor" on MSDN for extending the editor with adornments and such, am I headed the right way?

    Thanks much in advance!

    12 martie 2012 15:15

Toate mesajele

  • Hi,

    Take a look at the ICodeRefactoringProvider.  This handles contextual offering of features, and is how Extract Method, and Introduce Variable work in Roslyn.

    -- Kevin Pilch-Bisson

    13 martie 2012 15:51
  • Can you be a bit more detailed, please? Looking at ICodeRefactoringProvider within the definition and object browser doesn't tell me much.

    Also, I noticed there is an IHighlighter and IHighlighterService. Would these be what I should be using?

    For example, to invoke a refactoring by highlighting a piece of code and pressing a shortcut or from a context menu, would I use this method:

    IEnumerable<TextSpan> GetHighlights(IDocument document, int position, CancellationToken cancellationToken = null);

    Would int position be something like variable.DirectiveNameToken.Span.End if say I wanted to rename a variable?

    Thanks again.

    15 martie 2012 14:49
  • IHighlighter is used to show a highlight around "matching" spans in the editor.  It's what's used to highlight the "if" and "else" when the caret is touching them, or the { and } of a method for example.

    If you create an ICodeRefactoringProvider and export it with an ExportCodeRefactoringProvider attribute, Roslyn will load it and call "GetRefactoring" every time the caret position or selection changes.  The passed in "textSpan" will be the selection.

    In order to make changes as part of your refactoring, you should also import an ICodeActionEditFactory.  Take a look at the "ConvertToAutoProperty" sample for an example of what this looks like all put together.

    -- Kevin Pilch-Bisson

    15 martie 2012 16:43