How can I open a new Visual Studio Code file with pre-set template code inside it? - visual-studio-code

I would like to know if it is possible to open a new file in Visual Studio Code with code already written inside it, like a template code.
This is to save time instead of writing the same line of code on every new file I create for a project.

I found that what I was asking for was essentially how to create a code snippet that holds a template code. In Visual Studio Code:
Use Ctrl+Shift+P (Windows) and search preferences:Configure User Snippits
Then search javascript.json
Then write in your code (if you don't know the format use: https://snippet-generator.app/ or search how to write it)
Then add that code and use your assigned prefix keyword to generate the template on your file.

Related

can I add custom default code in visual studio code

Can I create a default code on VSC, that whenever I open a new page to write a code my default code(my written code) is there already?
you can use the extension: File Templates
You can specify global or project templates, based on name and extension.

Can you file from where in a project a file is imported in visual studio code?

I'm trying to understand how a specific javascript app works reading the source code.
I know there is a file that, at some point, gets executed, but the file has no exports so I am thinking that, in some other point in the code, there is an import 'foo/bar' statement causing the file to be run, but I don't know how to find where from.
Visual Studio Code has a feature that lets me see where a function has been defined or where is it used, is there a similar feature for tracking imports of a file?

Is there a way to put a VS Code snippets file in a non standard folder and have VS Code use it?

I want to create a standard VS Code snippets file for my team. We use Perforce for version control and I'd like to create a snippet file in our project folder and point VS Code to it. I just want people to be able to sync up and have them just have updated snippets.
I think Project-level snippets will do what you want:
Project level snippets
Snippets can now be scoped to a project and
shared with your team. Simply use the Preferences: Configure User
Snippets command or create *.code-snippets file in the .vscode folder.
Project snippets work just like other snippets, they show up in
IntelliSense and in the Insert Snippet action where they now have
their own category
More info: https://code.visualstudio.com/updates/v1_28#_project-level-snippets

How to create a link to a file in VSCode workspace

I'm trying to improve my VSCode extension which shows you the different values of a Jinja Template in your workspace. For now it creates a Hover message with the list of values and the location of the definition. Here is what it looks like:
I'm now trying to add a link to this definition.
I tried a markdown link like [filename](vscode://file/filePath:line:column) which works pretty well except it opens a new vscode instance even if the file is in the current workspace.
Would you have an idea on how to open the file in the current workspace or another way to create this link?

How to edit existing VS Code Snippets

Is there a way to remove or edit some of the default code snippets in Visual Studio CODE ?
For example when i type req+TAB i need require not requestAnimationFrame
The extensions snippets can be found inside each snippet directory below:
(if there are snippets in the extension)
Mac/Linux: $HOME/.vscode/extensions/
Windows: %USERPROFILE%\.vscode\extensions/
Select the extension you want to modify and then dive into the javascript.json file in snippets/ directory in there, and change whatever you like.
Just remember that if/when you choose to download and update the extension someday, all your personal modifications will get overwritten/replaced out with the updated version of the file.
(unless of course you squirrel away your changes outside of the extension's directory...)
Edit/Aside:
Looking closely at all the copied editions already present in this directory, it appears that at least some of the extension updates keep the former version around. If this is the case, when you update an extension when a new version is released, you wouldn't need to worry about storing a copy of your modified file somewhere else; returning a file to active duty might just be as easy as a copy-paste from the old into the appropriate, newer, higher numbered directory.
Resources/citations/acknowledgements:
Thanks to here for helping initially pointing me towards the relevant directory.
The suggestion item requestAnimationFrame is coming from the JavaScript language service. It's not coming from the snippets.
However, you can define your own snippets and tell Visual Studio Code to show the snippets first. How to do it:
Go to File -> Preferences -> User Snippets and select JavaScript in order to edit snippets for that language
Add this entry to the opened file javascript.json and save it
"require": {
"prefix": "req",
"body": [
"require"
],
"description": "Add 'require'"
}
Add the following line to your preferred settings.json (user or workspace settings) and save it
"editor.snippetSuggestions": "top"
Now you get your self defined require suggestion in first place as soon as you type req in a .js file.
On my Windows10 machine the log and other default javascript snippets can be found in :
C:\Users\$USER\AppData\Local\Programs\Microsoft VS Code\resources\app\extensions\javascript\snippets\javascript.json
On my Windows installation the default/built-in JavaScript snippets are located in
C:\Program Files (x86)\Microsoft VS Code\resources\app\extensions\javascript\snippets\javascript.json
I renamed that snippet to "logx" (requires admin privileges to modify the file) and restarted vsCode and now have just my user "log" snippet.
There are some threads about this on the issue tracker -
https://github.com/Microsoft/vscode/issues/10565
https://github.com/Microsoft/vscode/issues/48315
Attention everyone!
This is now possible in the latest vscode. Solution here: https://github.com/microsoft/vscode/issues/10565#issuecomment-721832613
That solution tells you how to disable any snippet (including built-in or extension snippets). While this is technically not editing the snippet, disabling the snippet and then creating your own user snippet accomplishes the same exact goal. Yay!
I found mine at ~/.config/Code/User/snippets
If you want to create a global snippet, create a file named snippet_name.code-snippets
If you want a language specific snippet, create it like php.json
Hiding default VSCode snippets is easy:
you can hide specific snippets from showing in IntelliSense (completion list) by selecting the Hide from IntelliSense button to the right of snippet items in the Insert Snippet command dropdown.
https://code.visualstudio.com/docs/editor/userdefinedsnippets#_can-i-remove-snippets-from-intellisense