diff options
| author | Carlyle <hack366@pm.me> | 2025-01-03 17:44:03 -0800 |
|---|---|---|
| committer | Carlyle <hack366@pm.me> | 2025-01-03 17:44:03 -0800 |
| commit | c45b8c9382611a2fad6ef4f90c94e07798883aeb (patch) | |
| tree | 45592f9639b2372608c71c2b215628181d26fe6e /editors | |
| parent | 59138b1c815b800b783155489c64348fc909ed31 (diff) | |
made 'create ols.json' and 'edit global ols.json' behave more consistently with each other
Diffstat (limited to 'editors')
| -rw-r--r-- | editors/vscode/package.json | 11 | ||||
| -rw-r--r-- | editors/vscode/src/extension.ts | 66 |
2 files changed, 43 insertions, 34 deletions
diff --git a/editors/vscode/package.json b/editors/vscode/package.json index 6ecfd98..f431984 100644 --- a/editors/vscode/package.json +++ b/editors/vscode/package.json @@ -14,9 +14,6 @@ "categories": [ "Programming Languages" ], - "activationEvents": [ - "onLanguage:odin" - ], "icon": "images/emblem.png", "main": "./out/extension.js", "contributes": { @@ -37,13 +34,13 @@ "category": "Odin Language Server" }, { - "command": "ols.editGlobalOls", - "title": "Edit global ols.json file", + "command": "ols.editProjectConfig", + "title": "Edit per-project config file (ols.json), creating it if necessary", "category": "Odin Language Server" }, { - "command": "ols.createOls", - "title": "Create ols.json file in project", + "command": "ols.editUserConfig", + "title": "Edit per-user config file (ols.json), creating it if necessary", "category": "Odin Language Server" } ], diff --git a/editors/vscode/src/extension.ts b/editors/vscode/src/extension.ts index 3a6d7b9..8e675b2 100644 --- a/editors/vscode/src/extension.ts +++ b/editors/vscode/src/extension.ts @@ -27,12 +27,16 @@ import { watchOlsConfigFile } from './watch'; const onDidChange: vscode.EventEmitter<void> = new vscode.EventEmitter<void>(); -const defaultConfig = { - $schema: "https://raw.githubusercontent.com/DanielGavin/ols/master/misc/ols.schema.json", - enable_document_symbols: true, - enable_hover: true, - enable_snippets: true -}; +const defaultConfig = JSON.stringify( + { + $schema: "https://raw.githubusercontent.com/DanielGavin/ols/master/misc/ols.schema.json", + enable_document_symbols: true, + enable_hover: true, + enable_snippets: true + }, + null, + 4, +); let ctx: Ctx; @@ -126,14 +130,13 @@ export async function activate(context: vscode.ExtensionContext) { ); if (userResponse === "Yes") { - createOlsConfig(ctx); + createOrEditProjectConfig(); } else if (userResponse === "Don't ask again") { config.updateAskCreateOLS(false); return; } } - parseOlsFile(config, olsFile); }); @@ -154,12 +157,12 @@ export async function activate(context: vscode.ExtensionContext) { client.start(); }); - vscode.commands.registerCommand("ols.createOls", async() => { - createOlsConfig(ctx); + vscode.commands.registerCommand("ols.editProjectConfig", async() => { + createOrEditProjectConfig(); }); - vscode.commands.registerCommand("ols.editGlobalOls", async () => { - editGlobalOlsConfig(serverPath); + vscode.commands.registerCommand("ols.editUserConfig", async () => { + createOrEditUserConfig(serverPath); }); client.start(); @@ -230,21 +233,25 @@ async function removeOldServers(config: Config, state: PersistentState): Promise } } -export function createOlsConfig(_ctx: Ctx) { - const olsPath = vscode.workspace.workspaceFolders![0].uri.fsPath; - const content = JSON.stringify(defaultConfig, null, 4); - writeFileSync(path.join(olsPath, "ols.json"), content); +export function createOrEditProjectConfig() { + const projectConfigPath = vscode.workspace.workspaceFolders![0].uri.fsPath; + openFileAndCreateIfNotExists("ols.json", projectConfigPath, defaultConfig); } -export async function editGlobalOlsConfig(serverPath: string) { - const configPath = path.join(path.dirname(serverPath), "ols.json"); - - vscode.workspace.openTextDocument(configPath).then( +export function createOrEditUserConfig(serverPath: string) { + const userConfigPath = path.dirname(serverPath); + openFileAndCreateIfNotExists("ols.json", userConfigPath, defaultConfig); +} + +function openFileAndCreateIfNotExists(file: string, folder: string, defaultContents: string) { + const filePath = path.join(folder, file); + console.log(filePath); + + vscode.workspace.openTextDocument(filePath).then( (document) => { vscode.window.showTextDocument(document) }, () => { - const content = JSON.stringify(defaultConfig, null, 4); - writeFileSync(configPath, content); - vscode.workspace.openTextDocument(configPath).then( + writeFileSync(filePath, defaultContents); + vscode.workspace.openTextDocument(filePath).then( (document) => { vscode.window.showTextDocument(document) } ); } @@ -256,10 +263,15 @@ export async function parseOlsFile(config: Config, file: string) { /* We have to parse the collections that they have specificed through the json(This will be changed when odin gets it's own builder files) */ - fs.readFile(file).then((data) => { - const conf = JSON.parse(data.toString()); - config.collections = conf.collections; - }); + fs.readFile(file).then( + (data) => { + const conf = JSON.parse(data.toString()); + config.collections = conf.collections; + }, + (error) => { + console.info("no ols.json found in workspace"); + }, + ); } function serverPath(config: Config): string | null { |