How do I add a code snippet with a number as a tabstop - visual-studio-code

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?

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.

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

Is there a way to add a continuous placeholder in vs code snippets?

I created a simple js snippet for objects:
"object": {
"prefix": "ob",
"body": [
"const ${1} = {",
"",
"${2}: ${3},",
"",
"};",
],
"description": "object"
},
I want it to continue to add names and values until I stop writing, not just one name and value like my snippets.

POSTing a nested object - returning the nested object inside the parent

Suppose that we have a Post resource (this is just a dummy example):
GET /api/posts/1
{
"id": 1,
"header": null,
"content": null
}
And now we'd like to create a Header inside the Post#1.
POST /api/posts/1/header
{
"color": "blue",
"title": "Some title"
}
Now, is it okay for the POST request above to return the following response?
{
"id": 1,
"header": {
"id": 1,
"color": "blue",
"title": "Some title"
},
"content": null
}
So, basically, a Header was created inside the Post and returned as part of the Post.
Also, if we need to GET the header for Post#1:
GET api/posts/1/header
{
"id": 1,
"color": "blue",
"title": "Some title"
}
So here only the Header is returned.
Edit: Formatted JSON following #Mike Slinn response.
Nested JSON objects are not a problem.
The problems with the JSON that you show are:
Single quotes are used instead of double quotes. Perhaps you are writing in Python and serializing a dict into JSON? If so, no problem. Otherwise, you need to replace the single quotes with double quotes.
Similarly, the keys need to be double-quoted.
{
"id": 1,
"color": "blue",
"title": "Some title"
}

Define defaultSnippets for JSONSchema additionalProperties

As the title suggests, I would like to define a vscode defaultSnippet for the additionalProperites of a JSONObject. The JSONSchema looks like the following
"$schema ": "http://json-schema.org/draft-07/schema#",
"type":"object",
"additionalProperties":{
"type": "object",
"properties":{
"name":{"type":"string"}
}
}
So just adding defaultSnippet property does not seem to work:
"$schema ": "http://json-schema.org/draft-07/schema#",
"type":"object",
"additionalProperties":{
"defaultSnippets": [{
"label": "test",
"description": "Additional property template",
"body": {
"$1": {
"name": "$2"
}
}
}],
"type": "object",
"properties":{
"name":{"type":"string"}
}
}
Is there a workaround to obtain the same behavior?
Your code is correct, but vscode at this moment is only checking properties for snippets. I had the same problem, so I've created Pull Request with fix for this issue.
https://github.com/microsoft/vscode-json-languageservice/pull/62
You can replace default extension if you in rush or just wait for merge and update.

VSCode nested snippets (or include a snippet inside another snippet)

I'm wondering if I can refer to another snippet within a snippet in VSCode user defined snippet.
Say I have
"Test1": {
"prefix": "snippet_test1",
"body":
"something"
}
and is there a way to insert snippet_test1 in another snippet like
"Test2": {
"prefix": "snippet_test2",
"body":
"${1:snippet_test1}"
}
Now snippet_test2 just outputs snippet_test1 instead of the content of snippet_test1.
#Mark provides a good answer to use macros and I got another possible answer for who are interested.
"Test1": {
"prefix": "snippet_test1",
"body":
"something"
}
"Test2": {
"prefix": "snippet_test2",
"body":
"${1:snippet_test1}"
}
For Test2, it does only show snippet_test1 other than Test1 content but if you hit ctrl+space at snippet_test1, it will show a list of possible snippet available and you can expand text in snippet_test2 to the full content in snippet_test1.
I think the only way to include or nest snippets within each other is to use a macro or some other programmatic way. Here is a solution using the macro extension multi-command.
Lets say you have these three snippets (in some snippets file):
"Master Snippet": {
"prefix": "master_snippet",
"body": [
"body of master",
"snippet2 = $2",
"$1",
"some other stuff",
"$1",
],
"description": "build the multi-snippet"
},
"snippet1": {
"prefix": "sn1",
"body": [
"body of snippet1",
],
"description": "insert1"
},
"snippet2": {
"prefix": "sn2",
"body": [
"I am snippet2",
],
"description": "insert2"
},
Then your macro would print Master Snippet first and then wherever the cursor is - the cursor will be in both $1 tabstop positions initally - the macro will insert the snippet1.
Then with the "jumpToNextSnippetPlaceholder", command in the macro you will jump to the next tabstop $2 which could be anywhere - I put it before $1 (where snippet1 got inserted) and snippet2 will be inserted at tabstop $2.
You can see that the Master Snippet is where you build the structure for inserting the other snippets - according to tabstops.
The macro would look like this (in your settings.json):
"multiCommand.commands": [
{
"command": "multiCommand.insertMultipleSnippets",
"sequence": [
{
"command": "editor.action.insertSnippet",
"args": {
"name": "Master Snippet",
}
},
{
"command": "editor.action.insertSnippet",
"args": {
"name": "snippet1",
}
},
"jumpToNextSnippetPlaceholder",
{
"command": "editor.action.insertSnippet",
"args": {
"name": "snippet2",
}
},
]
}
],
and then trigger the macro with some keybinding (keybindings.json):
{
"key": "alt+m", // or whichever keybinding you choose
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.insertMultipleSnippets" },
"when": "editorTextFocus"
},
You cannot use any snippet prefix to trigger the whole macro, but you can still use the individual snippet prefixes to trigger each snippet individually if you wish.
With the above Master Snippet, snippet1 and snippet2 the result of running the macro would be:
body of master snippet
snippet2 = I am snippet2
body of snippet1
some other stuff
body of snippet1
You do lose some functionality, like the inserted snippet cannot be pre-selected like placeholder text would be - if used like ${1:howdy}, the placeholder text howdy is just overwritten by the first snippet inserted.

Task.Json triggers validation on invisible only fields

I am trying to create a Azure DevOps Pipelines Custom extension. I have a task.json where fields are visible on certain conditions.
For example:
{
"name": "actions",
"type": "picklist",
"label": "Actions",
"defaultValue": "Select",
"required": true,
"helpMarkDown": "Select an Action from the dropdown as per your requirement.",
"options": {
"New": "Add",
"Delete": "Delete"
}
},
{
"name": "backEndIPAddress",
"type": "string",
"label": "IP Address",
"required": true,
"defaultValue": "",
"helpMarkDown": "",
"visibleRule": "actions = New",
"validation": {
"expression": "isMatch(value,'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?){0,15}$','IgnoreCase')",
"message": "Invalid IP Address. Please try again!"
}
}
The issue when the IPAddress field is hidden, the validation is still tried and it fails. How to ensure that the fields are validated only if they are visible?
A few options:
Set a default value for the input
Update the regex to include ^$| at the start to allow empty values ('require':true will take care of the requiredness)
Remember that there should be a default isIpV4Address(value: string) function so you don't have to specify the regex.
From the docs it looks like there is an upcoming when clause which will dictate when the set of rules should trigger, I suppose this may be causing the currently unwanted behavior.
See also:
https://github.com/Microsoft/vsts-tasks/blob/1d75fa8f66aa1cf7a9cb62946939f30f087b2969/docs/taskinputvalidation.md