2

When creating snippets I would like to know if it is possible to define a default value for a placeholder if no value is assigned.

For example, having this php snippet:

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

Where placeholders from tabstops 5 to 8 have the following default values:

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

I tried with choices in the following way, but with no success:

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

Please take a look to the row_offset definition. When the snippet is rendered I get the following

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

In this scenario what I would like to happen is in case I omit a placeholder value 0 is assigned.

Thanks for any help.

Mark
  • 143,421
  • 24
  • 428
  • 436
Mario
  • 4,784
  • 3
  • 34
  • 50

1 Answers1

5

The closest you will get to what you want are "choices". See snippet choices.

Your terminology is a little off. In ${6:limit} for example - that entire thing is the placeholder and limit is the default value for it. So it already has a default value - limit - and now you want to have another. So try this syntax:

${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.

Mark
  • 143,421
  • 24
  • 428
  • 436
  • You are right `choices` looks like the the closest solution at the moment. `${6|limit,-1|}` do the trick, Thanks Mark for answering – Mario Feb 21 '18 at 19:14