VsCode - Triggering a snippet upon file creation - visual-studio-code

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

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"
// }
}

Go to definition, go to implementation, autogenerate import for Ember

Im using Ember with VS Code.
What I need is to generate import string on a fly when I encounter dependency. For example I write someting like:
#tracked isLarge = false;
But I don’t have “#tracked” imported yet. So the otion could be to set the coursor on #tracked, press something like “Action + .” and pick “generate import”. It should generate import string:
import { tracked } from '#ember/tracking';
But it doesn’t work out of the box. How can I do that?
UPDATE: the same question about:
go to definition
go to implementation
cmd+click to navigate to implementation/component
You can use the extension My Code Actions
You can create actions that just insert the text independent of an error.
"my-code-actions.actions": {
"[javascript]": {
"import tracked": {
"where": "insertAfter",
"insertFind": "^import",
"text": "import { tracked } from '#ember/tracking';\n"
}
}
}
The key combo to use is the Code Action combo: Ctrl+.
If you get a diagnostic (PROBLEM panel, and squiggle) you can use that to further customize the action and you can use text from the diagnostics message.
I'm current adding the possibility to make multiple edits in an action and to use further customization and generalization.
"Ember Language Server" brings some solution. But it works mostly with library code that has .d.ts typings.
In case of custom JS code it still doesn't work.
So there is no straight solution. Only 2 ways:
Write .d.ts typing for custom code JS files
Move project to typescript

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.

Where are the docs on how to add symbol support for a language to Visual Studio Code?

I would like to add symbol support for PowerShell to VS Code but I'm not finding any docs on the code.visualstudio.com/docs site.
Also, is it possible to do this for a language like PowerShell that, for the moment, will only work on Windows? Is there a way to light up symbol support on Windows only?
BTW I've added a bunch of PowerShell snippets that I'm in the process of trying to get integrated into VS Code. Any help on how to get these snippets into the product would be appreciated as well? I did submit an issue on the snippets, suggesting that the team put these into VS Code.
There is currently no documentation for the plugin API. It's too early for this as the API is still changing with every minor release. The VSCode team is focused on providing a stable plugin API. There will be a documentation about it when it's done.
Nevertheless it is already possible to add a new language plugin or extending an exisiting one. Take a look on this short description on how to add declaration support for a new language: Create Custom Language in Visual Studio Code
You could add symbol support in a similar way. What you need is something like an abstract syntax tree builder for powershell scripts and an application or a javascript module that is able to process a JSON request in order to provide the correct symbols. An example request for outline support is this:
{
"seq":442,
"type":"request",
"command":"navbar",
"arguments":
{
"file":"c:/Users/C/Documents/projects/MyProject/MyFile.xxx"
}
}
A response could look like that:
{
"seq":442,
"type":"response",
"command":"navbar",
"request_seq":442,
"success":true,
"body":[
{
"text":"TObjA",
"kind":"class",
"kindModifiers":"",
"spans":[
{
"start":{
"line":10,
"offset":3
},
"end":{
"line":16,
"offset":4
}
}
],
"childItems":[
]
},
{
"text":"DoSomething",
"kind":"method",
"kindModifiers":"",
"spans":[
{
"start":{
"line":20,
"offset":1
},
"end":{
"line":27,
"offset":4
}
}
],
"childItems":[
]
},
]
}
I'm not sure what do you mean with "symbol support". Is it something like "jump to symbol inside the current file" using CTRL+Shift+O? Then you are looking for outlineSupport.
Is it something like "find a symbol in any file" using CTRL+P, #? Then you are looking for navigateTypesSupport.
Copy the needed .js file from the vs.langauage.csharp.o folder to the vs.langauage.powershell folder and register the support in powershellMain.js as it is done in omnisharpMain.js.
If you want to register the new support only on Windows then you can do it like this:
var isWin = /^win/.test(process.platform);
if(isWin)
monaco.Modes.NavigateTypesSupport.register('powershell', new navigateTypesSupport_1.default(ModelService, server));
I hope this helps for the moment. Don't forget to save your changed plugins in a different folder. VSCode often deletes changes in the plugin folders on update.