visual studio 中有任何方法可以在编辑器中以编程方式修改选定的编辑器文本吗?代码片段?宏?

Any way in visual studio to programmatically modify selected editor text while in editor? Code snippets? Macros?

我希望能够突出显示一段文本并删除任何“;”在突出显示的部分。

我写了一个 "surround with snippet" 来封装突出显示的文本,但也想务实地修改该文本,但不确定如何修改。

我在 visual studio 中使用 "Snippet Designer" 扩展来创建片段。

我希望突出显示的文本 " RunMethod1(var1); " 转换为 ".Then(() => RunMethod1(var1) ) " 即没有分号。

您可以对 Visual Commander 使用以下命令删除任何“;”在选择中并用 'Then' (语言: C#):

包围它
public class C : VisualCommanderExt.ICommand
{
    public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
    {
        EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
        ts.Text = ".Then(() =>" + ts.Text.Replace(";", "") + ") ";
    }
}

AutoHotKey 能够做我需要的事情,并且在掌握语法后被证明是一个非常强大的工具。感谢 Caius Jard 的建议。

这是我用来解决问题的代码。

#p:: ;Hold 'WindowsKey' and press 'P' ConvertToPromise()

return

;------------------------------------------------ ConvertToPromise()
{

oCB := clipboard ; local var to save original clipboard content clipboard :=
send, ^c
ClipWait,1
nCB := clipboard ; get currently Selected text.
mnCB := EraseSemiColon(nCB)

clipboard := mnCB
send, .Then(()=> ^v )
sleep, 100
clipboard := oCB ; restores original Clipboard value.

}

;------------------------------------------------- EraseSemiColon(stringText)
{
strVar := stringText
modifiedSt := StrReplace(strVar, "`;", "")

return, %modifiedSt%
}