diff --git a/package.json b/package.json index 424297e2..ea88ef13 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,10 @@ { "title": "Biome: Troubleshoot - Reset", "command": "biome.reset" + }, + { + "title": "Biome: Initialize", + "command": "biome.initialize" } ], "configuration": { @@ -191,7 +195,15 @@ "scopeName": "source.biome_syntax_tree", "path": "./resources/grammars/biome-syntax-tree.tmGrammar.json" } - ] + ], + "menus": { + "explorer/context": [ + { + "command": "biome.initialize", + "when": "resourceFilename =~ /(\\.biome|biome.json|biome.jsonc)$/ || resourceFilename =~ /settings.json/" + } + ] + } }, "engines": { "vscode": "^1.80.0" diff --git a/src/commands.ts b/src/commands.ts index 9ef44a2b..3166d4ee 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,9 +1,9 @@ +import { ConfigurationTarget, Uri, workspace } from "vscode"; import { downloadBiome } from "./downloader"; import { restart, start, stop } from "./lifecycle"; import { info } from "./logger"; import { state } from "./state"; import { clearTemporaryBinaries } from "./utils"; - /** * Starts the Biome extension * @@ -51,3 +51,51 @@ export const resetCommand = async () => { info("Biome extension was reset"); await start(); }; + +export const initializeCommand = async (args: Uri) => { + const subconfigs = [ + "[javascript]", + "[typescript]", + "[typescriptreact]", + "[javascriptreact]", + "[json]", + "[jsonc]", + "[vue]", + "[astro]", + "[svelte]", + "[css]", + "[graphql]", + ].join(""); + + // Scopes the config down to the current workspace folder + const config = workspace.getConfiguration(undefined, Uri.parse(args.path)); + try { + const configsToUpdate = [ + { name: "biome.enabled", value: true }, + { + name: "editor.codeActionsOnSave", + value: { + ...config.get( + "editor.codeActionsOnSave", + ), + "source.organizeImports.biome": "always", + "quickfix.biome": "always", + }, + }, + { name: "editor.defaultFormatter", value: "biomejs.biome" }, + { + name: `${subconfigs}`, + value: { "editor.defaultFormatter": "biomejs.biome" }, + }, + ]; + for (const { name, value } of configsToUpdate) { + await config.update( + name, + value, + ConfigurationTarget.WorkspaceFolder, + ); + } + + info("Workspace configuration updated"); + } catch (e) {} +}; diff --git a/src/extension.ts b/src/extension.ts index e6f5607c..ef59c58f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -6,6 +6,7 @@ import { } from "vscode"; import { downloadCommand, + initializeCommand, resetCommand, restartCommand, startCommand, @@ -52,6 +53,7 @@ const registerUserFacingCommands = () => { commands.registerCommand("biome.restart", restartCommand), commands.registerCommand("biome.download", downloadCommand), commands.registerCommand("biome.reset", resetCommand), + commands.registerCommand("biome.initialize", initializeCommand), ); info("User-facing commands registered");