Alternative to macros in Visual Studio 2012

2020-01-24 02:25发布

I use macros extensively for ViewModel properties in XAML development. I use them even more in WCF to generate Message and DataContract properties.

To my disappointment, the macros I've built aren't going to be usable in Visual Studio 2012.

An example of what I'm talking about, for a VM, I would enter something like this.

int id;
string name;

Select both lines, run a macro and end up with

private int _id;
private string _name;

public int Id
{
   get {return _id;}
   set
   {
      if(_id != value)
      {
        _id = value;
        RaisePropertyChanged("Id");
      }
}

public string Name
{
   if(_name != value)
   {
      _name = value;
      RaisePropertyChanged("Name");
   }
}

I'm looking for ideas of other solutions deal with losing macros.

10条回答
唯我独甜
2楼-- · 2020-01-24 02:37

Here's what I did to keep my macro functionality...

  1. Download and install the Visual Studio 2012 SDK here (it contains the "Visual Studio Package" template)
  2. New project -> Installed.Templates.Visual C#.Extensibility.Visual Studio Package

    Wizard page 1 of 7

    language = C#
    gen new key is fine, or use another if you have one
    

    wizard page 3 of 7

    check "menu command"
    

    Wizard page 7 of 7

    uncheck both integration and unit test project options
    

    Click finish

  3. In the .cs file:

    using EnvDTE;
    using EnvDTE80;
    
    ...
    private void MenuItemCallback(object sender, EventArgs e)
    {
        MenuCommand cmd = sender as MenuCommand;
    
        // This should start to look like familiar macro code...
        EnvDTE80.DTE2 dte2 = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2;
        TextSelection selection = (TextSelection)dte2.ActiveDocument.Selection;
    
        dte2.UndoContext.Open("macro command replacement");
        selection.Text = "inserted from macro replacement";
        selection.NewLine(1);
        dte2.UndoContext.Close();
        ...
    
  4. Run the project. A new Visual Studio instance will start with the package loaded.
  5. Find your command as the first entry at the top of the Tools menu. Click it to see if it works.
  6. To install for real, go to your bin\debug(/release) directory and double-click on the .vsix file
  7. It should be installed for the next time you run
  8. Go to menu Tools -> Options... -> environment.keyboard and map a keystroke to your tool

    Mapping theme : Visual C# 2005
    Command       : Tool.yourFunctionName (functions defined in the .vsct file)
    

If you want more than one command, you will need to add menu id's in the PkgCmdID.cs, Guids in the Guids.cs, layouts in the .vsct and a function in the *package.cs (and button(MenuCommand) in the Initialize function) for each one. It can all be done in the same project.

I used this project to create several new 'tools' with my old macro code, then mapped my keys to them. It's a lot more work (and headaches) up front, but doesn't have the lag time that macros had.

There is probably a way to do this without having it take up tool menus. I started looking at this last night and finally got it to work, so I'm done with it for now (at least until Microsoft decides to drop this too).

查看更多
混吃等死
3楼-- · 2020-01-24 02:38

I use Notepad++ with regular expressions like this:

Find:

public (.\*) (.)(.*) \\{ get; set; \\}

Replace:

private \1 \l(\2)\3; \r\n public \1 \2\3 \\{ get \\{ return \l(\2)\3; \\} \r\n set \\{ \l(\2)\3 = value; OnPropertyChanged\(para => this\.\2\3\); \\}\\}
查看更多
爷、活的狠高调
4楼-- · 2020-01-24 02:38

Check out http://devexpress.com/coderush

The templates feature does pretty much what you want.

There is a free "Express" version too.

查看更多
Explosion°爆炸
5楼-- · 2020-01-24 02:48

Visual Studio 2012's lack of macros was getting me down, as I have a few that I use literally all the time to insert standard little bits of text with a single keypress. So I wrote a very simple scripts extensibility package, VSScripts, which allows manipulation of the current selection from a command-line program.

This doesn't claim to be some all-encompassing full replacement for the old macro system, and it doesn't provide keyboard macros, but it does make it possible to recreate many types of text manipulation macro.

查看更多
趁早两清
6楼-- · 2020-01-24 02:56

The Visual Commander extension (developed by me) is an alternative to macros in Visual Studio 2012/2013/2015. You can even reuse your existing Visual Studio macros code in new VB commands.

查看更多
我命由我不由天
7楼-- · 2020-01-24 02:57

I'll stick to cutting the text into Notepad++ and using macros there, then pasting back. It is a shame the feature isn't in Visual Studio 2012 any more...

查看更多
登录 后发表回答