I was asking myself recently if it is possible to add snippets to either one of the workspace, language-specific or global json snippet files through a VSCode extension.
// something like
Command.addSnippet('''
{
"prefix": "my_snippt",
.... etc.
}
''')
Reading through the API documentation I did not find anything. If there is no way, how would you go about creating kind of a snippet manager?
You can use SnippetString and a CompletionItemProvider to dynamically suggest snippets.
Here is an example.
Related
I am currently developing an extension for Visual Studio Code in which I provide some individual code snippets. I have also defined some settings for this extension. Now my question:
Is it possible to use values from my settings in a code snippet?
For example, I can also use the predefined variables ${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}
e.g..
`"myTicket": {
"prefix": ["ticket"
"body": ["My Ticket is ${TICKET}"]
},`
TICKET is defined in package.json under configuration/properties ...
I also want to display the currently logged in user in a code snippet (environment variable %USERNAME%).
I hope I have expressed myself clearly. Unfortunately my English is not so good :-)
Many thanks for your help
I tried it with {TICKET}
I'm currently working on an vscode extension. I'm trying to understand how I can create a "popup"/"tooltip" like this
.
I would like to know methods that I'm expected to use or an github link for an extension that is doing it.
Thanks
You could use the vscode.languages.registerHoverProvider function (Documentation) to do this.
I wrote a little demo extension that you can see here: https://github.com/ShPelles/vscode-hover-demo.
Can I get the list of the available scopes for snippets?
Is there any native functionality that would control what files to create and where?
Snippets are scoped to languages and introduced to VS Code as global or local to a workspace. Languages can be contributed to VS Code by exceptions. Typically by declaring a language in the package.json, adding a syntax and snippet files. Update:
Here is the guide how to add a snippet to any language via your extension. If you follow the guide and add a snippet to the current workspace, you will notice that the file with extension .code-snippets gets created under the .vscode folder in the workspace. Therefore you can enumerate all such files using the vscode.workspace.workspaceFolders and fs.readDir APIs.
vscode.workspace.workspaceFolders!.forEach(workspaceFolder => {
const snippetFiles = fs.readdirSync(path.join(workspaceFolder.uri.fsPath, '.vscode'))
.filter(el => /\.code-snippets$/.test(el));
});
Similarly, the global snippets get added as .code-snippets files into the user profile folder - on Windows it looks like this: C:\Users\currentUser\AppData\Roaming\Code\User\snippets\*.code-snippets. Both locations you can scan for *.code-snippets files as well as programmatically add such files to those folders.
Here is the documentation how to add snippets to a scope via a file. There does not seem to be any public API to enumerate all such snippet files in a programmatic way.
But if you want to retrieve list of the languages in the screenshot you posted, use this API:
vscode.languages.getLanguages()
It could help if you explained what are you trying to achieve. Do you want to find where a particular snippet is coming from to change it? Do you want to add a snippet to a language in this list? Or to a language that is not on this list?
I have been combing through VS Code API docs, and am trying to understand whether or not it is actually feasible to write an extension that can edit the global User Settings JSON file.
Am I correct in thinking that I can create my extension, add the metadata I'm after under the "contributes" section in the "configuration" child object, then based on those values when the plugin is activated, take action against the User's preferences JSON?
I've also looked at the Guides plugin's configuration to check other examples, I'm just having a hard time conceptualizing how all this works, so any pointers would be appreciated.
I'm definitely NOT asking for someone to write my extension, just provide an answer as to whether I'm understanding the mechanics of an extension as a developer.
Would this work?
import { ConfigurationTarget, workspace } from 'vscode';
const configuration = workspace.getConfiguration(<YOUR_SECTION>);
configuration.update(<SETTING_NAME>, <SETTING_VALUE>, ConfigurationTarget.Global).then(() => {
// take action here
});
More information about the WorkspaceConfiguration object located here.
I find the default intellisense suggestions for Javascript irrelevant. Too many native browser functions I will never use.
Is there any way to turn off/limit intellisense? e.g. hide browser methods like the one in the link above.
To disable the default set of globals, create a jsconfig.json file at the root of your project with the content:
{
"compilerOptions": {
"lib": ["es6"]
}
}
This prevents the TypeScript service that VSCode uses for JavaScript IntelliSense from automatically including information about the DOM api.
You can read about all the avalible lib configuration options in the TypeScript documentation.