如何获取visual studio 2015编辑器窗口的选定文本?

cSt*_*off 0 c# visual-studio visual-studio-extensions visual-studio-2015

我想实现一个 Visual Studio 2015 扩展来获取用户在代码编辑器中选择的文本。比我想操纵选定的文本。

在代码编辑器中通过上下文菜单有一个按钮/命令。但我不知道如何获取选定的文本。

我认为,这个解决方案在这里已经过时或者我missunderstand解决方案。

Mat*_*son 6

我假设您的代码已经在一个派生自Package.

您可以像这样获取和修改选择文本:

DTE dte = (DTE)GetService(typeof(DTE));

if (dte.ActiveDocument != null)
{
    var selection = (TextSelection)dte.ActiveDocument.Selection;
    string text = selection.Text;

    // Modify the text, for example:
    text = ">>" + text + "<<";

    // Replace the selection with the modified text.
    selection.Text = text;
}
Run Code Online (Sandbox Code Playgroud)