Keith Padgett
Keith Padgett

Reputation: 3

Does VSCode snippet syntax allow optional parameters to a command

I recently started using a 3rd party extension for language supporting robot programming (RoboDK). The extension is pretty good but doesn't include many of the commands I would like to use and doesn't correctly (at least to me) handle optional arguments to commands. I'd like to 'extend' the extension to support additional commands (and potentially provide this feedback to the developer).

I'm specifically using ABB's RAPID robot language.

Example:

MoveL robtarget, speeddata, zonedata, tooldata \WObj:=WObj0;

For MoveL, var1 thru var4 are required but \WObj:=WObj0 is optional. The '\WObj0:=' part of the string is a constant (if supplying the option) and WObj0 would be a variable of the expected type for MoveL (a work object, in this case).

Is there a way to edit the snippets json to not enter the optional part unless I start typing a backslash followed by text? I also would like to keep the optional part in the snippet as a reference so I don't have to remember every optional argument as some commands have many options.

Here's an excerpt from the snippet.abb.json file:

"movel": {
    "prefix": "movel",
    "body": [
        "MoveL ${1:var_robtarget}, ${2:var_speeddata}, ${3:var_zonedata}, ${4:pers_tooldata} \\WObj:=WObj0;"
    ],
    "description": "Linear motion"
},

Which results in the code with var_robtarget, var_speeddata, var_zonedata & pers_tooldata all highlighted and the remainder is plain text:

MoveL var_robtarget, var_speeddata, var_zonedata, pers_tooldata \WObj:=WObj0;

Image from VS code editor

Upvotes: 0

Views: 1014

Answers (1)

Mark
Mark

Reputation: 182541

Is there a way to edit the snippets json to not enter the optional part unless I start typing a backslash followed by text?

No, I don't think there is any way to do that within the same snippet. You could make another snippet with the backslash prefix but that is cumbersome.

Here is a simple way to do what you want but you will have to delete any optional variables you don't want:

"body" [
   "MoveL ${1:var_robtarget}, ${2:var_speeddata}, ${3:var_zonedata}, ${4:pers_tooldata}${5: \\WObj:=WObj0}${6: anotherVar};"
]

That last variables are also selected when you get to them. If you don't want any just hit Delete.

how to handle optional args in a snippet

In the above demo I am just tabbing and deleting.

Upvotes: 1

Related Questions