0

Problem: I'm looking for a way to create complex snippets. At our company we have larger functions which almost seem boilerplate-ish, and I feel can be made much easier.

Desired solution: I want to create something, similar to how snippets work, but suitable for more complex generation of code. For instance, see the following code, which is typical for what we generate:

private readonly DependencyOne dependencyOne;
private readonly DependencyTwo dependencyTwo;

public ClassName(DependencyOne dependencyOne, DependencyTwo dependencyTwo)
{
    this.dependencyOne = dependencyOne;
    this.dependencyTwo = dependencyTwo;
}

Basically I only want type the two classnames, and from that generate the constructor and the two associated fields. If possible I want to add these fields at the correct position in the code, pretty much like how IntelliSense's Quick Fix automatically finds the correct position in your code to place the fields.

The reason why I can't just generate it above the constructor, is because there are some methods which will be generated which aren't constructors and therefore don't reside on the top of the code.

How do I achieve this desired solution?

Jordi
  • 1
  • 1
  • For Visual Studio, see https://stackoverflow.com/questions/43742958/how-to-automate-the-creation-of-wpf-viewmodel-properties – Sergey Vlasov May 10 '18 at 11:47

2 Answers2

0

Solution with Visual Studio Code 1.24:

In visual studio code you can specify the snippets as you want by creating a snippet JSON file. Please refer to this doc to know how to create a new snippet in VS Code.

write the following in language.json, language would be whatever language for which you want to create the snippet :

"Constructor - A unique name" : {
    "prefix" : "constructor",
    "body": [
        "private readonly ${DependencyOne} ${dependencyOne};",
        "private readonly ${DependencyTwo} ${dependencyTwo};",
        "",
        "public ClassName(${DependencyOne} ${dependencyOne}, ${DependencyTwo} ${dependencyTwo})",
        "{",
        "    this.${dependencyOne} = ${dependencyOne};",
        "    this.${dependencyTwo} = ${dependencyTwo};",
        "}",

            ],
    "description": "description of what it does"
}

after following the steps in doc and writing the json, you would be able to use the snippet by typing constructor as mentioned as "prefix" of the snippet.

Jay Joshi
  • 1,402
  • 1
  • 13
  • 32
  • 1
    So it would be nice to do the requested variable transforms to reduce the number of variables here that have to be inputted. For example, dependencyOne is just a simple transform away from DependencyOne. I tried to get it to work but couldn't. There are a number of issues filed on this. I see this PR may have fixed things: https://github.com/Microsoft/vscode/pull/51621 . Hopefully it will make it into v.1.25 of vsCode in early July 2018. – Mark Jun 30 '18 at 17:50
  • Yeah. I am surely aware about the PR. I will update the answer as soon as there are any update on the mentioned feature. – Jay Joshi Jun 30 '18 at 20:19
0

And with release v1.25 the following works:

"Constructor and variables" : {
    "prefix" : "ctor",
    "body": [
        "private readonly ${1/(.*)/${1:/capitalize}/} ${1:var1};",
        "private readonly ${2/(.*)/${1:/capitalize}/} ${2:var2};",
        "",
        "public ClassName(${1/(.*)/${1:/capitalize}/} $1, ${2/(.*)/${1:/capitalize}/} $2)",
        "{",
        "    this.$1 = $1;",
        "    this.$2 = $2;",
        "}",
    ],
    "description": "your description"
},

For this you will only type two names - I have made it so you type the uncapitalized version and the snippet will automatically capitalize the classnames. It would be easy to reverse those but would be a lot more code. After you enter the second classname/var hit tab and your code will capitalize correctly.You can replace the "var1/var2" with whatever you want.

Mark
  • 143,421
  • 24
  • 428
  • 436