Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active June 1, 2024 21:58
Show Gist options
  • Save ninmonkey/434317e07a1245e426a975416b36ad9b to your computer and use it in GitHub Desktop.
Save ninmonkey/434317e07a1245e426a975416b36ad9b to your computer and use it in GitHub Desktop.
VS Code Snippets - Fancy Templates Test - 2024-05.md
{
"SnippetTemplate_SelectionWithVariableFallback": { // 2024-06-01
"scope": "snippets",
"description": "Wrap selected text, else fallback to another variable when there is in selection",
"prefix": "SnipTemplate SelectVarFallback",
"body": [
"<p>",
"${TM_SELECTED_TEXT:$CURRENT_YEAR}", // possible variables listed here: <https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables>
"</p>",
"$0"
]
},
"SnippetTemplateForClippy8": {
"scope": "snippets",
"description": "(this might work if I get the substitution grammar right) Use selected text, else falls back to clipboard",
"prefix": "test8",
"body": [
"<p>",
// "${1:${TM_SELECTED_TEXT:${CLIPBOARD}}}", // not quite
// "${1:${TM_SELECTED_TEXT:$CLIPBOARD}}", // not quite
"${1:${TM_SELECTED_TEXT:stuff}}", // base working case, showing fallback when no selection
"</p>",
"$0"
]
},
"SnippetTemplateForClippy3": { // created: 2024-06-01 . template snippet for clipboard else selected text
// "scope": "markdown,html,snippets",
"scope": "snippets",
"description": "(WIP: Does not work) If selected text use that, else fallback to clipboard rather than static string",
"prefix": "test3",
"body": [
"<p>${1:${TM_SELECTED_TEXT:$CLIPBOARD}}</p>",
"$0"
]
},
"SnippetTemplateForClippy2": {
"scope": "markdown,html,snippets",
"description": "template using selected text else hardcoded text",
"prefix": "test1",
"body": [
// "<p>${1:${TM_SELECTED_TEXT:$CLIPBOARD}}</p>",
"<p>${1:${TM_SELECTED_TEXT:a cat in the hat}}</p>",
"$0"
]
},
"SnippetTemplateForClippy": {
"scope": "markdown,html,snippets",
"description": "template using selected text else fallback to clipboard",
"prefix": "test2",
"body": [
"<p>${1:${TM_SELECTED_TEXT:$CLIPBOARD}}</p>",
"$0"
]
},
"SnippetTemplateForClippy4": {
// "scope": "markdown,html,snippets",
"scope": "snippets",
"description": "select from a list of options, default body text and close the element",
"prefix": "test4",
"body": [
"<${1|p,h1,h2,h3,h4,h5,h6,a,div|}>",
"${2:stuff here}",
"</${1}>",
"$0"
]
},
}

About

If text is selected, use it in the completion. If nothing is selected, fallback to default values

How to use

You can enable snippets for all your workspaces if you save it to: Code\User\snippets\foo.code-snippets

  • Tweak scope to match whichever languages you want. I use these so that it's easier to test changes to snippets.
  • This lets you test the snippet's completions within the snippets json file itself
  • set the editor language to snippets and not json or jsonc. This parser better visualizes your snippets -- even though it's technically json
"scope": "markdown,html,snippets"

Mine Is saved at:

> Join-Path $Env:AppData 'Code\User\snippets\templates-for-snippets.code-snippets'

Refs

Test 2

Inputs

selection: 'Selection 🦎'
clipboard: 'Clipboard 🐈'

Snippet Definition

"debug_ShowBothInOneGo": { // 2024-06-01
    "scope": "snippets",
    "description": "debug: try both in one go",
    "prefix": "debug_tryBoth",
    "body": [
        // possible variables listed here: <https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables>
        "πŸ“Œ Clip:",
        "$CLIPBOARD",
        "πŸ“Œ Selection: ",
        "$TM_SELECTED_TEXT",
        "πŸ“Œ Selection or Variable: ",
        "${TM_SELECTED_TEXT:$CURRENT_YEAR}",
        "πŸ“Œ Selection or Clip: ",
        "${TM_SELECTED_TEXT:$CLIPBOARD}",
        "$0"
    ]
},

Using Selected Text

πŸ“Œ Clip:
Clipboard 🐈
πŸ“Œ Selection:
Selection 🦎
πŸ“Œ Selection or Variable:
Selection 🦎
πŸ“Œ Selection or Clip:
Selection 🦎

No Selection

πŸ“Œ Clip:
Clipboard 🐈
πŸ“Œ Selection:

πŸ“Œ Selection or Variable:
2024
πŸ“Œ Selection or Clip:
Clipboard 🐈

Test 1

Snippet Definition

"SnippetTemplateForClippyTest": {
    "scope": "snippets",
    "description": "(wip) Use selected text, else falls back to clipboard",
    "prefix": "test",
    "body": [
        "<p>",
        "${$TM_SELECTED_TEXT:$CLIPBOARD}", // sort of works but also does not
        "</p>",
        "$0"

    ]
}

Using Selected Text

<p>
${Some Text:My Clip}
</p>

No Selection

<p>
${:My Clip}
</p>

other failed attempts

"${1:${TM_SELECTED_TEXT:${CLIPBOARD}}}", // not quite
"${1:${TM_SELECTED_TEXT:$CLIPBOARD}}", // not quite
"${1:${TM_SELECTED_TEXT:stuff}}", // works
"${1:${$TM_SELECTED_TEXT:$CLIPBOARD}}", // works , sort of. both at once with extra bars
"$TM_SELECTED_TEXT:$CLIPBOARD", // another works , sort of. both at once with extra bars
"${$TM_SELECTED_TEXT:$CLIPBOARD}", // sort of works but also does not
"${TM_SELECTED_TEXT}:${CLIPBOARD}", // another works , sort of. both at once with extra bars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment