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

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

Related

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

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.

Where do code snippets reside in Visual Studio Code?

The only C/C++ extension I have is the official one from Microsoft. I do not have any other snippet/intellisense/autocompletion extension.
See below all the extensions loaded:
In trying to create shortcut keys/prefixes for my snippets, I would like to make sure that it does not clash with any pre-existing shortcuts/prefixes for other snippets. Is there a way to know / look through all currently available code snippets in VSCode and their shortcut keys/prefixes?
I tried to Insert Snippet via the command pallette in the hope that it would reveal all available snippets. Unfortunately, it does not list all snippets. See, for instance, below image, where this command pallette does not show the existence of a for snippet and yet inside the editor, when I type for, there is an option to add such a snippet.
I obtained confirmation officially from vscode-cpptools folks that C/C++ snippets are only available via Ctrl-Space autocomplete and not via Ctrl-Shift-P Command Pallette.
See link to their answer here.

Visual Studio Code extension for saving and reusing my own custom code snippets / boilerplate?

I am looking for VSC extensions that will let me save my own code snippets and reuse them in any future projects with shortcuts like when I type html I am able to pick an option that generates whole document example that I can then continue modifying.
Ideally it should also have an option to export all custom settings so I can move them to another machine or back them up if I need to format the system.
I realized this was one of my major efficiency issues when I kept copying code from old projects to be reused in my latest work. I did check the extension search in VSC but so far only found such that already include existing snippet shortcuts.
I found 1 viable method so far with native VSC but post if you know of an even better way.
Press Ctrl + P and > then type Preferences: Configure user snippets and then selecting a language. To generate json fast I used: https://snippet-generator.app/
It saves snippets in AppData\Roaming\Code\User\snippets folder so that is good for backuping or exporting.
To reorder snippets to have custom ones at the top of the suggestions box for faster use go to: Workspace Settings > Text Editor > Suggestions > Snippet Suggestions and set it to top.

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

Any way to disable syntax checking for a project?

I created a project "Sample Code"... here I just paste sample code... much of it is snippets that won't compile.
Is there some project-specific setting I can make so that Eclipse doesn't try to compile it?
I would prefer not to have the source code littered with red error markers.
Put your code in a non-java project, ie a general project.
Downside: you will have to create package directory structure (unless you can copy and paste from somewhere else).
Upside: it won't try to compile.
MY SOLUTION
ok, this is not an exact solution to my problem... but it is another way to do it and I kinda like it now...
I simply forget about using Eclipse to store the sample java files!
I found a good program CodeBox for Mac to store code snippets and I'm sure there exist such things for Windows, Linux too...
there interesting thing is that when I choose from this program to open the java snippet file (.java) in an external editor (Eclipse), it will open in Eclipse without any Syntax checking... wohoo! no squiggly lines
Because of this, it is not full blown code highlighting... classes and variables same color... but that's ok.. still quite readable. Much more than if it was in Eclipse with syntax highlighting running on it...
So basically, if you want to get rid of these red squiggles... one way to do it is don't keep sample .java (or other language) files in a project in Eclipse... simply keep them in the filesystem or code storage app and open them with Eclipse when you want to view them.
Depending on how you prefer to structure your project:
you could put your java files into a separate folder that is not configured as a source folder. There is an entry in the eclipse help on how to configure your build path.
or you can set exclusion-patterns in the build configuration, so that specific packages or files that follow a pattern you define don't get compiled.
Yet another way to handle your snippets could be to use a Scrapbook page.
Eclipse won't highlight anything in a scrapbook page but you can select code parts inside the page and execute them isolated. That's nice if you're experimenting and don't want to set up a whole class with imports and methods just to see if a specific snippet works as expected.