如何在 Visual Studio/Visual Studio Code 中生成更智能/复杂的片段? [英] How to generate smarter/complex snippets in Visual Studio/Visual Studio Code?

查看:33
本文介绍了如何在 Visual Studio/Visual Studio Code 中生成更智能/复杂的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我正在寻找一种方法来创建复杂的片段.在我们公司,我们有更大的功能,几乎看起来像样板一样,我觉得可以变得更容易.

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;
}

基本上我只想输入两个类名,然后从中生成构造函数和两个关联的字段.如果可能的话,我想在代码中的正确位置添加这些字段,就像 IntelliSense 的快速修复如何自动在您的代码中找到正确位置来放置字段一样.

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?

推荐答案

在 v1.25 版本中,以下工作:

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"
},

为此,您将只输入两个名称 - 我已经这样做了,因此您输入非大写版本,代码段将自动将类名大写.反转这些很容易,但需要更多的代码.输入第二个类名/var 后,点击 tab 并且您的代码将正确大写.您可以将var1/var2"替换为您想要的任何内容想要.

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.

这篇关于如何在 Visual Studio/Visual Studio Code 中生成更智能/复杂的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆