Home > OS >  How do I add a code snippet with a number as a tabstop
How do I add a code snippet with a number as a tabstop

Time:10-14

Consider the following JSON schema snippet:

{
    "label": "New spell",
    "description": "Creates a new spell",
    "body": {
        "name": "$1",
        "source": "$2",
        "page": "${3}",
        "tradition": "$4",
        "type": "${5|U,A|}",
        "level": "${6}",
        "description": ["$7"]
    }
}

Both page and level need to be an integer (can default to 0), but also be a tabstop. I have tried a few things, but the only value that acts as a tabstop is enclosed in quotes, so it ends up being a string. Is there a way to add a tabstop where the default value is a number?

CodePudding user response:

This would work to set a tabstop 3 and 6 with defaults of 0. Note that the quotes must be escaped.

"\"page\": \"${3:0}\""

Also the general form of a snippet is as follows:

"new spell": {
    "description": "Creates a new spell",
    // "scope": "javascript,typescript",

    "prefix": "new spell",       // whatever prefix you want

    "body": [                      // array
        "\"page\": \"${3:0}\"",
        "\"level\": \"${6:0}\"",
    ]
}

There is no label property and there should be a prefix property. And body, if multiple statements is an array.

CodePudding user response:

As it says in the documentation:

If a string starts with ^, the string content will be inserted as-is, not stringified. You can use this to specify snippets for numbers and booleans.

The value string needs to be specified as "^${3:0}".

So, the snippet fixed:

{
    "label": "New spell",
    "description": "Creates a new spell",
    "body": {
        "name": "$1",
        "source": "$2",
        "page": "^${3:0}",
        "tradition": "$4",
        "type": "${5|U,A|}",
        "level": "^${6:0}",
        "description": ["$7"]
    }
}
  • Related