Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the selected text of the editor window..visual studio extension

hi i'm making a extension for visual studio and the specific thing that i need is get the selected text of the editor windows for further processing. Someone know what interface or service has this? Previously i need to locate the path of the open solution and for that i ask for a service that implements IVsSolution, so for this other problem I thing that there must be some service that provides me this information.

like image 675
mjsr Avatar asked May 19 '10 18:05

mjsr


People also ask

How do I select specific text in Visual Studio?

Ctrl+D selects the word at the cursor, or the next occurrence of the current selection. Tip: You can also add more cursors with Ctrl+Shift+L, which will add a selection at each occurrence of the current selected text.

Where is the text editor in Visual Studio?

The Text Editor toolbar, which is the row of buttons under the menu bar in Visual Studio, helps make you more productive as you code.

How do I open the Visual Studio editor window?

On the Window menu, click New Window. A new tabbed instance of the editor is added.

What is extensions in Visual Studio?

What are extensions? Extensions are add-ons that allow you to customize and enhance your experience in Visual Studio by adding new features or integrating existing tools. An extension can range in all levels of complexity, but its main purpose is to increase your productivity and cater to your workflow.


2 Answers

To clarify "just get the viewhost" in Stacker's answer, here is the full code for how you can get the current editor view, and from there the ITextSelection, from anywhere else in a Visual Studio 2010 VSPackage. In particular, I used this to get the current selection from a menu command callback.

IWpfTextViewHost GetCurrentViewHost()
{
    // code to get access to the editor's currently selected text cribbed from
    // http://msdn.microsoft.com/en-us/library/dd884850.aspx
    IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
    IVsTextView vTextView = null;
    int mustHaveFocus = 1;
    txtMgr.GetActiveView(mustHaveFocus, null, out vTextView);
    IVsUserData userData = vTextView as IVsUserData;
    if (userData == null)
    {
        return null;
    }
    else
    {
        IWpfTextViewHost viewHost;
        object holder;
        Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
        userData.GetData(ref guidViewHost, out holder);
        viewHost = (IWpfTextViewHost)holder;
        return viewHost;
    }
}


/// Given an IWpfTextViewHost representing the currently selected editor pane,
/// return the ITextDocument for that view. That's useful for learning things 
/// like the filename of the document, its creation date, and so on.
ITextDocument GetTextDocumentForView( IWpfTextViewHost viewHost )
{
    ITextDocument document;
    viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out document);
    return document;
}

/// Get the current editor selection
ITextSelection GetSelection( IWpfTextViewHost viewHost )
{
    return viewHost.TextView.Selection;
}

Here's MSDN's docs for IWpfTextViewHost, ITextDocument, and ITextSelection.

like image 65
Crashworks Avatar answered Sep 21 '22 15:09

Crashworks


Inside of the OnlayoutChanged, the following code would pop up a message with the code selected:

if (_view.Selection.IsEmpty) return;
else
{
    string selectedText = _view.Selection.StreamSelectionSpan.GetText();
    MessageBox.Show(selectedText);
}

Anywhere else, just get the viewhost and its _view of typeIWpfTextView

like image 31
Stacker Avatar answered Sep 23 '22 15:09

Stacker