【问题标题】:Is there a way to add a blank option with VS Code snippets placeholder choices?有没有办法使用 VS 代码片段占位符选项添加空白选项?
【发布时间】:2021-08-24 23:55:52
【问题描述】:

我正在尝试创建一个可以让我选择可选属性的 sn-p。我过去使用过这种方法,我只是在placeholder 中放置一个空格作为choice。这在 sn-p 的其他部分之间只有一个选项时有效,但就像在下面的示例中一样,如果我想跳过两个占位符(装饰上的可选属性),生成的代码中将有多个空格,我将不得不手动删除。

"Adorn": 
  "prefix": ["adorn"],
  "body": [
    "<%= adorn${1| , color: :blue, color: :white|}${2| , inline: true|} do |a| %>",
      "\t$0",
        "\t<%= a.adornment %>",
          "\t\t",
        "\t<% end %>",
    "<% end %>"
  ],
  "description": "An adorn"
},

从我所看到的in the documentation 看来,使用placeholderschoices 似乎无法做我想做的事情。我以为我可以使用一个变量并将其解析为空字符串,但语法似乎不允许这样做。

还有其他方法可以完成我想做的事情吗?

【问题讨论】:

    标签: visual-studio-code vscode-snippets


    【解决方案1】:

    你可以在 sn-ps 中使用一些 unicode 字符,所以我尝试了退格(没用),但 \u200B 这是一个 “零宽度空格”。所以你可以在你的选择中这样做:

    {1|\u200B, color: :blue, color: :white|}${2|\u200B, inline: true|}

    如果您选择空格(即\u200B's),将插入一个零宽度的空格。你得到你想要的 - 没有要删除的空格。

    但是我留给你看看在你的代码中使用该字符是否存在任何解析或其他问题。也许它是或不是你的语言的问题。请参阅未解决的问题(我在最初发布此答案后发现)https://github.com/microsoft/vscode/issues/34368(“允许在 sn-ps 中选择“空” ") 警告零宽度空间选项并可能导致问题 - 所以测试它。看起来除了您尝试过的常规空间之外没有任何其他选项。

    我尝试了“null”\u0000,但 vscode 无法识别。

    【讨论】:

    • 虽然这不是我所希望的,但它确实是一个我没有考虑过的创造性解决方法。感谢您提供指向未解决问题的链接,以确认该行为是不可能的。
    【解决方案2】:

    如果您只需要一个空格,那么只需要一个“$1”即占位符。

    但首先让我们看看你是否有错误

    首先

    在你的项目的根目录,你需要一个新的目录调用 .vscode

    在 yow dir 添加一个新文件 注意 名称非常重要 yow 新文件必须具有此模式 anycode-snippets

    在你的新文件中添加这个

    {
        // Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
        // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
        // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
        // used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
        // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
        // Placeholders with the same ids are connected.
        // Example:
         "Print to console": {
            "scope": "javascript,typescript",
            "prefix": "log",
            "body": [
                "console.log('$1');",
                "$2"
            ],
            "description": "Log output to console"
         }
    }
    

    最后,我建议您删除范围属性。并且不要将文件用于一种以上的语言,因为这样做可能根本不起作用。

    【讨论】:

      【解决方案3】:

      为带有选项的占位符获取空值的最佳方法是添加 1 个字符的选项,然后使用 Delete 或 Backspace 将其删除(取决于光标所在的位置,这 2 个占位符会有所不同),然后 TAB 到下一个占位符。

      要获取占位符 2 的选择列表,请在位于占位符 2 时按 TAB。

      注意分隔空格的位置,它们在选择的末尾。

        "Adorn": {
          "prefix": ["adorn"],
          "body": [
            "<%= adorn ${1|e,color: :blue ,color: :white |}${2|e,inline: true |}do |a| %>",
              "\t$0",
                "\t<%= a.adornment %>",
                  "\t\t",
                "\t<% end %>",
            "<% end %>"
          ],
          "description": "An adorn"
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 2022-10-08
        • 2013-06-15
        • 2013-10-27
        • 2022-01-05
        相关资源
        最近更新 更多