Saving user preferences/settings for vscode extension - visual-studio-code

What are the best practices to store user settings for the extension? I can think of saving them in some specific format in the text file. Is there any integrated way of doing that using vscode API?

VSCode extensions typically contribute settings to be used in user and workspace settings.json files.
Settings are registered via contributes.configuration in an extension's package.json, and the allowed values can flexibly be described with a JSON schema (which also allows for code completion in the JSON file). The value of settings can then be checked by using the workspace.getConfiguration() API.
There is also a sample extension that showcases the Configuration API in vscode-extension-samples.
Having a separate file just for a specific extension's settings would be a bit unusual.

Related

What is the best way to access the VS Code intellisense settings via the API?

I'm building an extension that needs to harvest some of the intellisense settings from the c_cpp_properties.json file, specifically the values for "includePath" and "defines", and I have some questions.
Does VS Code support file specific intellisense settings? For example, if I want to set -DBOZO for foo.cpp and -DBINGO for bar.cpp.
Does the API have a way to access the intellisense settings, or should I just parse the json file myself (which is simple)?
If I need to parse the json file, then for a multi-root workspace, what is the best way to find the .vscode folder for a particular file? I know I can use the variable ${fileWorkspaceFolder} in tasks.json for example. Is there programmatic access to variables like this from the API?
Thanks!

vscode setting for specific filename

I want to apply a setting for a specific file, the only answers I've seen for this topic have been language-specific settings. How can I achieve this?
At this time, you cannot achieve this natively. According to their documentation they state:
VS Code provides two different scopes for settings:
User Settings - Settings that apply globally to any instance of VS Code you open.
Workspace Settings - Settings stored inside your workspace and only apply when the workspace is opened.
You would have to file a feature request on their github for support

Associate file type to my vscode extension like a project file

I'd like to do something like a project file. When the user opens it, the webview of my extension would welcome the user instead of the default VSCode editor.
I know I can workaround it by adding extra context menu items or buttons, but in this case I'm curious if it's possible to override that it by default tries to open in the editor.
Is this possible either manually or automatically?
What you're after sounds exactly like what the upcoming webview / custom editor API hopes to accomplish.
The custom editor API aims to allow extensions to create fully customizable read/write editors that are used in place of VS Code's standard text editor for specific resources. These editors will be based on webviews. We will support editors for both binary and text resources.
See the following issue for more info and further links to the proposed API, example extensions etc:
Custom webview editor API (#77131)
Note that it usually takes a while for new APIs to make it into stable releases after being made available as "proposed APIs".

Copy enable/disable extensions settings to another workspace

Idea:
I wanted to clean up my vscode extensions, because I'm working with a lot of different languages/file types and having all these extensions installed and enabled at once is just too much.
Problem:
I disabled some extensions for a specific workspace, and wanted to copy these settings to another workspace, but vscode is not storing the information about enabled/disabled extensions in .vscode/settings.json.
Questions:
Is there any way to copy these settings from one workspace to another?
Is there a better way of dealing with lots of extensions?
Can you recommend tools/extensions for managing extensions per workspace or language(s)
I assume vscode is not loading all extensions at once, but rather when needed. But some extensions display icons on the left or bottom of the window and overcrowd the "Show All Commands" list/search.
VS Code stores this info in its internals instead of the .vscode folder, so you can't copy this info between workspaces. There is an open issue asking exactly what you want.
But, you have an alternative. Use the Profile Switcher extension.
Its description:
This extension allows you to define a number of settings profiles that you can easily switch between. The original idea for this extension came from my desire to have an easy way for me to switch my VS Code to a setup that was better optimised for presenting (changed themes, increase font size, etc).
And this is how it handles extensions:
A profile isn't just the settings you have enabled, but also the extensions that were installed. This allows you to create different profiles for different styles of development (e.g. a React profile and a Vue profile, loading their respective extensions only).
Hope this helps
There is a github issue for this problem: Feature Request: Enable/disable extensions from config file #40239.
I posted there a workaround using multiple vscode instances: link
Here is a copy-paste:
I use some kind of workaround to be able to use the extensions I want.
According to the vscode-cli your can specify the folders for extensions and user-data:
Options Description
--extensions-dir <dir> Set the root path for extensions.
--user-data-dir <dir> Specifies the directory that user data is kept in. Can be used to open multiple distinct instances of Code.
Basically, I create a specific folder for my specific tasks (one of front, one for back, ..) and set basic extensions to my default vscode.
To launch my custom config:
code --extensions-dir "$HOME/.vscode/profiles/my-super-profile/extensions" --user-data-dir "$HOME/.vscode/profiles/my-super-profile/data"
The problem are that:
It's not REALLY a project config file but a global preference file
I had to install manually the extensions. I believe there is a hackish way to do this
It use more size than necessary (multiple vscode data / duplicate extensions)
It doesn't solve in a clean way the team-sharing problem

How are extension developers allowing users to customize features like linting?

I'm working on a VSCode extension that has a linting feature. The linter can be customized quite a bit. How are folks allowing users to customize a feature like this? Completely in user preference settings? Specially named file? User preference setting that specifies path to a settings file?
It looks like you can have a .vscode\settings.json file that allows global user preferences to be overridden on a per-workspace basis.