VS Code Extension to Update Workspace Settings - visual-studio-code

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.

Related

Dynamically add VSCode snippets from an extension

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.

Opening the VSC settings window from code

I'm working on a Visual Studio Code extension, where some settings are required for it to work properly. Right now, in case the user has forgotten something, I'm showing a warning message indicating this, and telling them to fix this property in the settings. I would, however, like to add a button that opens the settings page of that property.
However, after looking around for over an hour, I can't seem to find any support for this in the Api, or anyone else asking this question (which I find a bit weird?). If it really doesn't exist, I'd still expect at least 1 other person asking this somewhere, but as far as I can see there's absolutely nothing.
Is it possible to open the settings window (preferably even filtering them down to only my extension's properties/the one property that has to be filled in) from code?
Thanks in advance
I found it out myself after digging through the keybinds window. You can use the following to open the settings:
vscode.commands.executeCommand("workbench.action.openSettings2");
I did not, however, find how to pass a search query into it to open a specific place in the settings.
EDIT: You can use openSettings & add the search query as an extra argument to executeCommand. For example - if your property is defined as my.extension.property in package.json, you can use this:
vscode.commands.executeCommand("workbench.action.openSettings", "my.extension.property");

Get Visual Studio Code snippets scope programmatically using API

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?

How to access per project / per preset configuration data in vue-cli 3.x?

I'm looking to create a vue-cli 3.x plugin that generates extra files in the project when invoked and templates these based on configuration info entered by the user.
It's rather easy to collect this information from user prompts but it'd be much nicer if users could put this info into some sort of configuration file and have the plugin read it from there.
I understand vue-cli now uses a vue.config.js file on the project level and ~/.vuerc on a more global (preset) level. However, it doesn't look like my generator function would have access to either one of those files.
"The entire preset" should be passed as the third argument to the function when invoking the plugin with module.exports = (api, options, rootOptions) => {} in ./generator/index.js, rootOptions is undefined.
Sililarly, the use of vue.config.js is discussed in the documentation when talking about service plugins, but there's no mention how to use it in a generator function.
Am I missing something obvious here or is there really no officially sanctioned way to do this? Nothing stops me from reading vue.config.js in my function, but this feels hacky. And I wouldn't even know how to find out which preset was used to create the project.
So your problem is that you can't find ~/.vuerc?
I had the same issue and I found the solution here.
The solution is to use the command line
vue config
to see .vuerc, and then use
vue config --delete presets.yourPresetName
to delete your preset.
As well, if you can't type in your preset name in the terminal because it contains a space or an apostrophe, you can use
vue config --edit
to open .vuerc with a text editor and just edit it there

Can not import and use DirectorySearch in atom.io

I'm trying to build a package for atom editor, I need to search for all local files in the project.
From https://atom.io/docs/api/v1.0.2/DirectorySearch I see DirectorySearch is an interesting class to search for specific text in local files.
There is little documentation on the page. I tried {DirectorySearch} = require 'atom' and new atom.DirectorySearch(). But they are not working, said "DirectorySearch is undefined".
I searched in atom's repository, but it seems that they only defined it. There is no usage of DirectorySearch. I also searched on Google and Stack Overflow but with no luck.
I'm using Version 1.0.2 on Mac OSX 10.
Can someone tell me how to import and use this class?
According to https://discuss.atom.io/t/how-to-import-and-use-directorysearch-in-atom/19205
Looking at the source it doesn't seem you can require it, but there's an instance of DefaultDirectorySearcher created in the workspace and available at atom.workspace.defaultDirectorySearcher that is used in the scan method as a fallback when a searcher for a directory haven't been specified.
If you want to search text in files, atom.workspace.scan should be enough.
You can also register a custom directory searcher using the atom.directory-searcher service, as far as I can tell, the object needs to implement the following methods to comply to the searcher interface:
-canSearchDirectory (directory:Directory) -> Boolean
-search(directories:Array, regex:RegExp, searchOptions:Object) -> CancellablePromise
The DirectorySearch class that appears in the docs is actually the CancellablePromise returned by the directory searcher.