Search code examples
c#visual-studiomacroscode-snippets

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.


Solution

  • 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(";", "") + ") ";
        }
    }