0

我想实现一个visual studio 2015扩展,以在代码编辑器中获取用户选定的文本。比我想操纵选定的文字。如何获取visual studio 2015编辑器窗口的选定文本?

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

我觉得这个解决方案here已经过时了,或者我误解了解决方案。

回答

1

我假设你的代码已经在派生自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; 
} 
相关问题