Skip to content

Code action which selects text #237

@sharwell

Description

@sharwell

I want to create a code action in my CodeFixProvider which causes particular text to be selected in the editor. Is this possible?

Note: this is not a rename operation, and not on a symbol, so adding a RenameAnnotation to the node does not work.

The proposal would be to add a SelectionAnnotation, which is used similar to RenameAnnotation but instead of triggering a rename operation it just sets the text selection.

namespace Microsoft.CodeAnalysis.CodeActions
{
    /// <summary>
    /// Apply this annotation to an appropriate SyntaxNode to request that it should be selected in the IDE after
    /// the action.
    /// </summary>
    public static class SelectionAnnotation
    {
        public const string Kind = "CodeAction_Selection";
 
        public static SyntaxAnnotation Create();

        /// <summary>
        /// Creates a selection annotation which selects a subspan within a syntax node to which the annotation is
        /// applied. The index is calculated relative to the start of the <see cref="SyntaxNode.FullSpan"/> of the
        /// syntax node.
        /// </summary>
        public static SyntaxAnnotation Create(int index, int length);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEConcept-APIThis issue involves adding, removing, clarification, or modification of an API.Feature Request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions