Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made 'create ols.json' and 'edit global ols.json' behave more consistently with each other #564

Merged
merged 2 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
"categories": [
"Programming Languages"
],
"activationEvents": [
"onLanguage:odin"
],
"icon": "images/emblem.png",
"main": "./out/extension.js",
"contributes": {
Expand All @@ -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"
}
],
Expand Down
97 changes: 59 additions & 38 deletions editors/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -109,32 +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") {
createOlsConfig(ctx);
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()) {
Expand All @@ -154,18 +166,18 @@ 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();

parseOlsFile(config, olsFile);
watchOlsConfigFile(ctx, olsFile);
parseOlsFile(config, projectConfigPath);
watchOlsConfigFile(ctx, projectConfigPath);
}

async function bootstrap(config: Config, state: PersistentState): Promise<string> {
Expand Down Expand Up @@ -230,21 +242,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) }
);
}
Expand All @@ -256,10 +272,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 {
Expand Down
Loading