Skip to content

Commit 048c996

Browse files
Use VSCode-EmmyLua config to provide global config
In e356b99 ('Set up language server configuration automatically') logic for setting up EmmyLuaLs automatically has been added. It used a configuration file at the home directory. It's not really very cool to spam with extension-specific files at the user's home dir. A patch [^1] has been introduced allowing one to specify a path to a global language server configuration file within the VS Code options. Let's set up this instead. [^1] EmmyLua/VSCode-EmmyLua#227
1 parent b490488 commit 048c996

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/extension.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as tt from './tt';
33
import * as fs from 'fs';
4-
import * as os from 'os';
54
import * as _ from 'lodash';
65
import * as utils from './utils';
76

@@ -18,28 +17,41 @@ const emmyrc = {
1817
}
1918
};
2019
const emmyrcFile = '.emmyrc.json';
20+
const globalEmmyrcKey = 'emmylua.misc.globalConfigPath';
21+
const globalEmmyrcPath = __dirname + `/${emmyrcFile}`;
2122

2223
async function initGlobalEmmyrc() {
23-
const globalEmmyrcPath = `${os.homedir()}/${emmyrcFile}`;
24-
25-
if (!fs.existsSync(globalEmmyrcPath)) {
26-
fs.writeFileSync(globalEmmyrcPath, JSON.stringify(emmyrc, undefined, 2));
27-
vscode.window.showInformationMessage(`Initialized ${globalEmmyrcPath} with Tarantool-specific settings`);
28-
return;
24+
const config = vscode.workspace.getConfiguration(undefined, null);
25+
const configuredGlobalEmmyrcPath = config.get(globalEmmyrcKey);
26+
27+
let existingEmmyrc = {};
28+
try {
29+
const f = fs.readFileSync(globalEmmyrcPath, 'utf8');
30+
existingEmmyrc = JSON.parse(f);
31+
} catch {
32+
existingEmmyrc = {};
2933
}
30-
31-
const f = fs.readFileSync(globalEmmyrcPath, 'utf8');
32-
const existingEmmyrc = JSON.parse(f);
3334
const upToDate = _.isMatch(existingEmmyrc, emmyrc);
34-
if (upToDate) {
35-
return;
35+
if (!upToDate) {
36+
fs.writeFileSync(globalEmmyrcPath, JSON.stringify(emmyrc, undefined, 2));
3637
}
3738

38-
// TODO: Don't miss user-defined libraries.
39-
const mergedEmmyrc = _.merge(existingEmmyrc, emmyrc);
40-
41-
fs.writeFileSync(globalEmmyrcPath, JSON.stringify(mergedEmmyrc, undefined, 2));
42-
vscode.window.showInformationMessage(`Updated existing ${globalEmmyrcPath} with actual Tarantool-specific configuration`);
39+
const desiredGlobalEmmyrcPath = globalEmmyrcPath;
40+
if (configuredGlobalEmmyrcPath !== desiredGlobalEmmyrcPath) {
41+
try {
42+
await config.update(globalEmmyrcKey, desiredGlobalEmmyrcPath, vscode.ConfigurationTarget.Global);
43+
} catch {
44+
vscode.window.showWarningMessage(`Tarantool extension has been unable to update the global configuration of the EmmyLua extension with its specific annotations. Run 'Tarantool: Initialize VS Code extension...' to initialize the annotations per-project`);
45+
return;
46+
}
47+
try {
48+
await vscode.commands.executeCommand('emmy.restartServer');
49+
} catch {
50+
vscode.window.showWarningMessage(`Tarantool extension has updated the configuration but wasn't able to restart the EmmyLua extension. Please, restart it manually`);
51+
return;
52+
}
53+
vscode.window.showInformationMessage(`The EmmyLua extension has been updated with Tarantool-specific settings`);
54+
}
4355
}
4456

4557
async function initVs() {

0 commit comments

Comments
 (0)