Home > Services > DXCore Services – Selection

DXCore Services – Selection

August 30th, 2011

The Selection DXCore service provides methods for manipulating the selected text in the code editor.

Methods of this service:

Method name

Description

Collapse Collapses the current selection if automatic outlining is turned off. Call the CodeRush.Outline.ExpandCurrent() method to restore the collapsed region.
Delete Deletes the selected text.
GetPoints(SourcePoint, SourcePoint) Outputs the anchor and active points of the active selection via the anchorPoint and activePoint parameters. If no text is selected, activePoint will be the active caret position. If no document is open, activePoint and anchorPoint will be set to empty source points. This method returns true if a selection exists; otherwise, false.
SelectRange(Int32, Int32, Int32, Int32) Selects the specified range in the active text document.
SelectRange(SourcePoint, SourcePoint) Selects the specified range in the active text document.
SelectRange(SourceRange) Selects the specified range in the active text document.
SelectRange(TextPoint, TextPoint) Selects the range between the specified text points in the active document.
SelectToMarker Selects the text from the editor caret to the topmost marker.
SelectWord Selects the word at the editor caret.

Properties:

Property name

Description

Exists Returns true if a text is selected in the code editor.
Height Returns the number of lines in the current selection.
State Gets the current selection state. A state is one of the following values: no selection, line fragment, whole line or multiple lines.
Text Sets or gets the text selected in the code editor, as a string.
—–
Products: DXCore
Versions: 11.1 and up
VS IDEs: any
Updated: Aug/30/2011
ID: D111

Similar Posts:

  1. Robert Leahey
    February 20th, 2012 at 11:38 | #1

    Hi Alex. Is CodeRush.Selection.SelectWord() currently functional? I get a COMException (“The Command to execute is not enabled.”) when I call it from a plug-in with the following code:

    if (string.IsNullOrEmpty(CodeRush.Selection.Text))
    {
    CodeRush.Selection.SelectWord();
    }

    Is there perhaps a preferred or better way to increase the selection in a case like this?

    Thanks

  2. February 20th, 2012 at 11:51 | #2

    Hi Robert, I just tried your code and it works correctly in my plug-in. To perform this command, the code editor should be active and focused. Has this requirement been met in your case? Would you please also clarify how you want to extend the selection? Is it just a word or something else? What results you would like to achieve?

  3. Robert Leahey
    February 20th, 2012 at 12:20 | #3

    Well that was quick, Alex, thanks.
    I think I see the problem: this call is in response to a button click in a ToolWindow plug-in. Thus, the code editor will have lost focus due to the button click.

    This is an XML documentation helper plug-in. In the case of this command, I’m allowing the user to add a -tag to their comment. If text is selected, I’ll enclose the selection with the tag. If nothing is selected and we’re not in a word, I’ll just insert “”; if nothing is selected but the cursor is in the middle of a word, I’d like to expand the selection to include the word and then enclose that in the tag.

    Is there a way to force focus back to the code editor so I can call SelectWord() or is there some other course to take?

  4. Robert Leahey
    February 20th, 2012 at 12:25 | #4

    Whoops, the xml tags were stripped from my comment.
    Where I say, “…allowing the user to add a -tag…”, that’s a <c> tag.
    Likewise at ‘I’ll just insert””‘ – that should be “<c><\c>

  5. February 20th, 2012 at 23:27 | #5

    Robert,
    Before SelectWord(), you can call CodeRush.TextViews.Active.SetFocus(). It will activate the code editor, and SelectWord() will work as expected.
    Then, you can return focus back to your tool window by calling its Focus() method. However, changing focus will produce flickering of the IDE (when it gains focus and then loses it immediately).

    To avoid this issue (flickering), you can use APIs of the TextView object:

    if (string.IsNullOrEmpty(CodeRush.Selection.Text))
    {
    TextView activeView = CodeRush.TextViews.Active;
    if (activeView == null)
    return;

    Word word;
    if (activeView.GetWordAt(CodeRush.Caret.SourcePoint, out word) == GetWordResult.Success)
    {
    activeView.Selection.Set(word.Range);
    }
    }

    The GetWordAt() and Selection.Set() methods do not require the code editor to be either active or focused, so they will work from your tool window correctly.

  6. Robert Leahey
    February 21st, 2012 at 06:57 | #6

    Thanks a bunch, Alex, as always.

  7. February 22nd, 2012 at 01:44 | #7

    You are welcome, Robert!