Gregory Jensen
Gregory Jensen

Reputation: 13

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

I would like to be able to highlight a section of text and remove any ';' in the highlighted section.

I've written a "surround with snippet" to encapsulate the highlighted text, but would also like to pragmatically modify that text and am unsure how.

I use the "Snippet Designer" extension in visual studios for snippet creation.

I would expect the highlighted text " RunMethod1(var1); " to be converted to ".Then(() => RunMethod1(var1) ) " ie without the semi-colon.

Upvotes: 0

Views: 220

Answers (2)

Gregory Jensen
Gregory Jensen

Reputation: 13

AutoHotKey was able to do what I needed and proved to be a pretty robust tool after getting a hang of the syntax. Thanks for the suggestion Caius Jard.

This was the code I used to solve the problem.

#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%
}

Upvotes: 0

Sergey Vlasov
Sergey Vlasov

Reputation: 27940

You can use the following command for Visual Commander to remove any ';' in the selection and surround it with 'Then' (Language: 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(";", "") + ") ";
    }
}

Upvotes: 1

Related Questions