【问题标题】:Visual studio code user snippets capitalize not working properlyVisual Studio 代码用户片段大写不能正常工作
【发布时间】:2019-11-18 11:16:00
【问题描述】:

我写了这样的代码

"State": {
    "prefix": "state",
    "body": [
        "const [$1, set${1:/capitalize}] = useState($2);"
    ],
    "description": "Adds state"
},

我希望结果是这样的(如果我在 $1 中输入 test):

const [test, setTest] = useState($2);

但我得到这样的结果:

const [/capitalize, set/capitalize] = useState();

在官方文档中我发现了这样的规则:'${' int ':' '/upcase' | '/downcase' | '/capitalize' '}'

你能告诉我我做错了什么吗?

【问题讨论】:

    标签: visual-studio-code code-snippets vscode-snippets


    【解决方案1】:

    您可以使用下面的 sn-p 来获取请求的输出:

    const [$1, set${1/(.*)/${1:/capitalize}/}] = useState($2);
    

    输出将是(如果我输入 $1 作为测试):

    const [test, setTest] = useState();
    

    【讨论】:

      【解决方案2】:

      让我们看看为什么你的版本${1:/capitalize} 不起作用:

      这是你从https://code.visualstudio.com/docs/editor/userdefinedsnippets引用的部分sn-p语法

      tabstop     ::= '$' int
                      | '${' int '}'
                      | '${' int  transform '}'
      
       -snip-
      
      transform   ::= '/' regex '/' (format | text)+ '/' options
      
      format      ::= '$' int | '${' int '}'
                      | '${' int ':' '/upcase' | '/downcase' | '/capitalize' '}'
      

      所以最初看起来${1:/capitalize}是正确的,只看上面语法的最后一行似乎

      ${' int ':' '/capitalize'

      是一个有效的选项。但是您必须跟踪语法才能正确使用它。 format 语法只能在 transform 中使用。我们在以下方面看到了这一点:

      transform ::= '/' regex '/' (format | text)+ '/' options

      所以你的版本不包括转换。您没有必要的 regex 前缀。因此,那些'/upcase' | '/downcase' | '/capitalize' 选项只能用作带有正则表达式的转换的一部分(尽管您可以有一个空的正则表达式,但这对您没有帮助,您仍然需要有正则表达式入口点)。

      这是变换的一般形式:

      ${someInt/regex captures here/do something with the captures here, like ${1:/capitalize} /}

      请注意,第一个 $someInt 是一个制表位 - 例如,它可能是 $1,但第二个 $1(大写)不是制表位,而是对第一个捕获组来自前面的正则表达式。因此,转换只能转换正则表达式已捕获的内容。

      语法要求format 选项是transform 的一部分,而转换要求format 部分中的regex$n 指的是捕获组而不是制表符变量。

      我希望这一切都有意义。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        • 1970-01-01
        • 2018-06-02
        相关资源
        最近更新 更多