Snippet with function name - visual-studio-code

I want to add user code snippet and I want to have the function name in it, something like this:
{
"Print to console": {
"prefix": "clog",
"body": [
"console.log('CONSOLE', '$FUNC_NAME$', $1);"
],
"description": "Log to console"
}
}
I can't find how to get the function name in VSCode snippets.
Any help?

You'll need to write an extension to do this. VS Code's user snippets cannot access program structure information, such as class or function names.
Check out the completions extension sample to get started.

Related

How to manually add snippets to VSCode

There are a slew of questions/answers on this, and I went through them without success - here's my setup...
Extensions:
snippets.json file location -- C:\Users\USERNAME.vscode\snippets.json
Suggestion Settings:
Added Emmet Path (redacted):
Snippets.json
{
"Less": {
"scope": "php html",
"prefix": "!less",
"body": [
"<script src='https://cdn.jsdelivr.net/npm/less'></script>"
],
"description":"Adds the Less.CSS CDN"
},
"MooTools": {
"prefix": "!cdnmt",
"body": [
"<script src='https: //cdnjs.cloudflare.com/ajax/libs/mootools/1.6.0/mootools-core.min.js' integrity='sha512-P6QjhxYLbTLOHQpilU3lsmhwLfgVh/zN5rVzcGbmiTWhMcpkHVk8eqWk2vqvM0Z6OhJfrdbYoTzJtGQYUj8ibw==' crossorigin='anonymous' referrerpolicy='no-referrer'></script>"
]
}
}
Right now, neither quicktype works. I've tried !less - there's no conflicting shortcut and hitting tab or enter after it doesn't do anything other than tab or enter.
Same goes for MooTools...
Some reviewed links:
here-are-the-default-emmet-settings-in-visual-studio-code
visual-studio-code-user-snippets-not-working
I've gone through the myriad of answers and tried just about everything. Nothing seems to work. I also am unable to locate/edit the defaultSettings.json (opens as read only in vs).
Additional Information (1)
Tried updating included files:
Still not working (FYI - I did restart VSCode to make sure that wasn't blocking it from taking over).
The simplest way to add snippets is to use the interface, either the "File/ Preferences/ Configure User Snippets" menu entry or the similarly named element in the gear icon you can find on the side bar:
You can then choose to add global snippets (to be used in any file type) or per-language snippets:
The program takes care of creating a file with the appropriate name and extension in the correct location. It also adds some boilerplate with documentation. This way you don't need to do prior research about your environment.
{
// Place your snippets for sql here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
}

How to display snippets using Regex matching code in VS Code

I tried to read the documentation to write a extensions, but the information I got was very limited. I wanted to match the code dynamically and output a snippet of code via RE, but there seems to be no way
{
"quick show": {
"prefix": "/.*[\\/](.*[\\/].*)/",
"body": "$1",
"description": "Code snippet"
}
}
Is there any good way to do that

VsCode - Triggering a snippet upon file creation

I wanted to know if it is possible to trigger a user defined custom snippet when I create a file in vscode.
I started learning Golang recently and noticed that there is some boilerplate code that goes into creating the main.go file.
So I created my custom snippet for it and now now I can trigger the snippet manually and save me some keystrokes of typing.
I wanted to go one step further, so that whenever I create a new file named main.go from within VsCode, it should automatically fire that snippet and insert the biolerplate code for me, without me having to manually trigger the snippet.
Is this even possible ?
Check out this extension: Auto Snippet:
With this in your settings.json:
"autoSnippet.snippets": [
{ "pattern": "**/main.go", "snippet": "main-go-template" },
],
[Note that the example settings in the extension docs has a few syntax errors - use my example above - I have filed an issue with the creator.]
And then your snippet
"main-go-template": {
"prefix": "zz",
"body": [
... // you can use tabstops and transforms just like regular snippets
]
"description": "javascript template"
}
Then creating a new main.go file or opening an empty one should trigger the snippet.
It is a good idea to reload vscode whenever you make a change to this extension's settings.
Here is a demo of a gulpfile with common boilerplate:
Also see this extension https://marketplace.visualstudio.com/items?itemName=bam.vscode-file-templates

Making vscode snippets only trigger after specific characters

Let's say I have a bunch of functions below:
library.object1.function1()
library.object1.function2()
library.object2.function1()
library.object2.function2()
library.object3.function1()
library.object3.function2()
With what they provide in current custom snippet, when I type lib, it will show all those above functions, which will be a mess if there are too many functions.
I want to make my snippets work like what they did in default code completion:
When I type lib, it only shows:
library
When I type library., it shows:
object1
object2
object3
When I type library.object1., it shows
function1()
function2()
Also, if I type lib, and leave it there, then comeback and add rary, the snippet doesn't work at all, I want it to continue the completion.
Is there a way to achieve it?
I think the closest you can get is something like this (using javascript.json snippet file as an example):
"my library": {
"prefix": "lib",
"body": [
"library.${1|object1,object2,object3|}.${2|function1,function2,function3,function4|}()",
],
"description": "my library functions"
}
With that, when you type lib you get only the library suggested completion. Tab and you will get all the object choices you included in the snippet in the suggestion panel. Tab again and will get the function options that you listed in the snippet.
See snippet choices.

How do I add a snippet programmatically?

We have some custom snippets we provide as part of our VS Code extension via key bindings and a snippets json file:
{
"key": "ctrl+shift+i",
"mac": "cmd+shift+i",
"command": "editor.action.insertSnippet"
},
...
"snippets": [
{
"language": "xml",
"path": "./snippets/xml.json"
}
]
We would like a button to add one particular snippet to the editor at the current cursor position.
How do I programmatically I invoke the part of "editor.action.insertSnippet" after the user has selected the snippet?
I posted this issue on the vscode repo.
jrieken responded with the following reply:
The insertSnippet-command accepts an argument which is either the name of a snippet or a snippet itself. So, either { snippet: "console.log($1)$0"} for an inline snippet or { langId: "csharp", name: "myFavSnippet" } referencing an existing snippet.
You can run any registered command via vscode.commands.executeCommand. See also the vscode namespace API.