如何定义片段占位符的默认值?

How to define default values for snippet placeholders?

创建代码片段时,我想知道如果没有分配值,是否可以为占位符定义默认值。

例如,有这个 php 片段:

{
  "get_list": {
        "prefix": "get_list",
        "body": "$${1:beanList} = $${2:bean}->get_list('${3:order_by}', \"${4:where}\", ${5:row_offset}, ${6:limit}, ${7:max}, ${8:show_deleted});",
        "description": "Get a paginate bean list"
    },
}

制表位 5 到 8 的占位符具有以下默认值:

$row_offset = 0
$limit= -1
$max= -1
$show_deleted = 0

我尝试通过以下方式进行选择,但没有成功:

{
  "get_list": {
        "prefix": "get_list",
        "body": "$${1:beanList} = $${2:bean}->get_list('${3:order_by}', \"${4:where}\", ${5:row_offset|0|}, ${6:limit}, ${7:max}, ${8:show_deleted});",
        "description": "Get a paginate bean list"
    },
}

请查看 row_offset 定义。呈现代码段后,我得到以下内容

$beanList = $bean->get_list('order_by', "where", row_offset|0|, limit, max, show_deleted);

在这种情况下,我希望发生的情况是万一我省略了分配的占位符值 0

感谢您的帮助。

最接近您想要的是 "choices"。参见 snippet choices

你的术语有点不对劲。例如,在 ${6:limit} 中 - 整个内容都是占位符,而 limit 是它的默认值。所以它已经有一个默认值 - limit - 现在你想要另一个。所以试试这个语法:

${6:|limit,-1|}

Placeholders can have choices as values. The syntax is a comma separated enumeration of values, enclosed with the pipe-character, for example ${1|one,two,three|}. When the snippet is inserted and the placeholder selected, choices will prompt the user to pick one of the values.