From c45b8c9382611a2fad6ef4f90c94e07798883aeb Mon Sep 17 00:00:00 2001 From: Carlyle Date: Fri, 3 Jan 2025 17:44:03 -0800 Subject: [PATCH 1/2] made 'create ols.json' and 'edit global ols.json' behave more consistently with each other --- editors/vscode/package.json | 11 ++---- 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 = new vscode.EventEmitter(); -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 { From 9c43bf48981c5079db80d0372176ecf49b3b87a4 Mon Sep 17 00:00:00 2001 From: Carlyle Date: Fri, 3 Jan 2025 18:26:50 -0800 Subject: [PATCH 2/2] improved the starting configuration dialog --- editors/vscode/src/extension.ts | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/editors/vscode/src/extension.ts b/editors/vscode/src/extension.ts index 8e675b2..ae776f8 100644 --- a/editors/vscode/src/extension.ts +++ b/editors/vscode/src/extension.ts @@ -113,31 +113,40 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand("runDebugTest", runDebugTest); ctx.registerCommand("runTest", runTest); - const olsFile = path.join(workspaceFolder.uri.fsPath, "ols.json"); - - fs.access(olsFile, constants.F_OK).catch(async err => { - if (err) { + const projectConfigPath = path.join(workspaceFolder.uri.fsPath, "ols.json"); + const userConfigPath = path.join(path.dirname(serverPath), "ols.json"); + fs.access(projectConfigPath, constants.F_OK).catch(async (_e1) => { + fs.access(userConfigPath, constants.F_OK).catch( async (_e2) => { if (!config.askCreateOLS) { return; } const userResponse = await vscode.window.showInformationMessage( - "No ols config file in the workspace root folder. Do you wish to create one?", + "No ols config file found. Do you wish to create one?", "Yes", "No", - "Don't ask again" + "Don't ask again", ); if (userResponse === "Yes") { - createOrEditProjectConfig(); + const clarification = await vscode.window.showInformationMessage( + "should it be specific to this project or to all your odin projects?", + "This project", + "All projects", + ); + if (clarification == "This project") { + createOrEditProjectConfig(); + parseOlsFile(config, projectConfigPath); + } else { + createOrEditUserConfig(serverPath); + parseOlsFile(config, userConfigPath); + } } else if (userResponse === "Don't ask again") { config.updateAskCreateOLS(false); return; } - - } - parseOlsFile(config, olsFile); + }) }); if(!isOdinInstalled()) { @@ -167,8 +176,8 @@ export async function activate(context: vscode.ExtensionContext) { client.start(); - parseOlsFile(config, olsFile); - watchOlsConfigFile(ctx, olsFile); + parseOlsFile(config, projectConfigPath); + watchOlsConfigFile(ctx, projectConfigPath); } async function bootstrap(config: Config, state: PersistentState): Promise {