2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.activate = void 0;
4 const tslib_1 = require("tslib");
5 const coc_nvim_1 = require("coc.nvim");
6 const child_process_1 = require("child_process");
7 const os_1 = (0, tslib_1.__importDefault)(require("os"));
8 const tools_1 = require("./utils/tools");
9 const commands_1 = require("./commands");
10 const modify_tags_1 = require("./utils/modify-tags");
11 const config_1 = require("./utils/config");
12 const editor_1 = require("./editor");
13 const binaries_1 = require("./binaries");
14 const tests_1 = require("./utils/tests");
15 const playground_1 = require("./utils/playground");
16 const impl_1 = require("./utils/impl");
17 const restartConfigs = [
23 async function activate(context) {
24 (0, config_1.setStoragePath)(context.storagePath);
25 if ((0, config_1.getConfig)().enable === false) {
28 registerGeneral(context);
29 registerGopls(context);
30 registerTest(context);
31 registerTags(context);
32 registerPlaygroud(context);
33 registerGoImpl(context);
34 registerTools(context);
36 exports.activate = activate;
37 async function registerGeneral(context) {
38 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.version", () => (0, commands_1.version)()));
40 async function registerGopls(context) {
41 const config = (0, config_1.getConfig)();
42 const command = await goplsPath(config.goplsPath);
46 const args = config.goplsArgs ? [...config.goplsArgs] : [];
47 if (config.goplsUseDaemon !== false && !args.find(arg => arg.startsWith('-remote'))) {
48 // Use daemon by default
49 args.push('-remote=auto');
51 // TMPDIR needs to be resetted, because its altered by coc.nvim, which breaks
52 // the automatic deamon launching of gopls.
53 // See: https://github.com/neoclide/coc.nvim/commit/bdd9a9e1401fe6fdd57a9bd078e3651ecf1e0202
54 const tmpdir = await coc_nvim_1.workspace.nvim.eval('$TMPDIR');
55 const server = () => {
56 return new Promise(resolve => {
57 resolve((0, child_process_1.spawn)(command, args, {
58 cwd: coc_nvim_1.workspace.cwd,
59 env: Object.assign(Object.assign(Object.assign({}, process.env), { TMPDIR: tmpdir }), config.goplsEnv),
63 // https://github.com/neoclide/coc.nvim/blob/master/src/language-client/client.ts#L684
64 const clientOptions = {
65 documentSelector: ['go', 'gomod'],
66 initializationOptions: () => (0, config_1.getConfig)().goplsOptions,
67 disableWorkspaceFolders: config.disable.workspaceFolders,
68 disableDiagnostics: config.disable.diagnostics,
69 disableCompletion: config.disable.completion,
70 // TODO disableSnippetCompletion: config.disable.snippetCompletion,
72 const client = new coc_nvim_1.LanguageClient('go', 'gopls', server, clientOptions);
73 if (config.checkForUpdates !== 'disabled' && !config.goplsPath) {
74 await (0, commands_1.checkGopls)(client, config.checkForUpdates);
76 context.subscriptions.push(coc_nvim_1.services.registLanguageClient(client),
77 // restart gopls if options changed
78 coc_nvim_1.workspace.onDidChangeConfiguration(async (e) => {
79 if (restartConfigs.find(k => e.affectsConfiguration(k))) {
83 }), coc_nvim_1.commands.registerCommand("go.install.gopls", () => (0, commands_1.installGopls)(client)));
85 async function goplsPath(goplsPath) {
87 if (goplsPath.startsWith('~')) {
88 goplsPath = os_1.default.homedir() + goplsPath.slice(1);
90 if (!await (0, tools_1.commandExists)(goplsPath)) {
91 coc_nvim_1.window.showMessage(`goplsPath is configured ("${goplsPath}"), but does not exist!`, 'error');
96 if (!await (0, tools_1.installGoBin)(binaries_1.GOPLS)) {
99 return (0, tools_1.goBinPath)(binaries_1.GOPLS);
101 async function registerGoImpl(context) {
102 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.install.impl", () => (0, commands_1.installImpl)()), coc_nvim_1.commands.registerCommand("go.impl.cursor", async () => (0, impl_1.generateImplStubs)(await (0, editor_1.activeTextDocument)())));
104 async function registerTest(context) {
105 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.install.gotests", () => (0, commands_1.installGotests)()), coc_nvim_1.commands.registerCommand("go.test.generate.file", async () => (0, tests_1.generateTestsAll)(await (0, editor_1.activeTextDocument)())), coc_nvim_1.commands.registerCommand("go.test.generate.exported", async () => (0, tests_1.generateTestsExported)(await (0, editor_1.activeTextDocument)())), coc_nvim_1.commands.registerCommand("go.test.generate.function", async () => (0, tests_1.generateTestsFunction)(await (0, editor_1.activeTextDocument)())), coc_nvim_1.commands.registerCommand("go.test.toggle", async () => (0, tests_1.toogleTests)(await (0, editor_1.activeTextDocument)())));
107 async function registerTags(context) {
108 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.install.gomodifytags", () => (0, commands_1.installGomodifytags)()), coc_nvim_1.commands.registerCommand("go.tags.add", async (...tags) => (0, modify_tags_1.addTags)(await (0, editor_1.activeTextDocument)(), { tags })), coc_nvim_1.commands.registerCommand("go.tags.add.line", async (...tags) => (0, modify_tags_1.addTags)(await (0, editor_1.activeTextDocument)(), { tags, selection: "line" })), coc_nvim_1.commands.registerCommand("go.tags.add.prompt", async () => (0, modify_tags_1.addTags)(await (0, editor_1.activeTextDocument)(), { prompt: true })), coc_nvim_1.commands.registerCommand("go.tags.remove", async (...tags) => (0, modify_tags_1.removeTags)(await (0, editor_1.activeTextDocument)(), { tags })), coc_nvim_1.commands.registerCommand("go.tags.remove.line", async (...tags) => (0, modify_tags_1.removeTags)(await (0, editor_1.activeTextDocument)(), { tags, selection: "line" })), coc_nvim_1.commands.registerCommand("go.tags.remove.prompt", async () => (0, modify_tags_1.removeTags)(await (0, editor_1.activeTextDocument)(), { prompt: true })), coc_nvim_1.commands.registerCommand("go.tags.clear", async () => (0, modify_tags_1.clearTags)(await (0, editor_1.activeTextDocument)())), coc_nvim_1.commands.registerCommand("go.tags.clear.line", async () => (0, modify_tags_1.clearTags)(await (0, editor_1.activeTextDocument)(), { selection: "line" })));
110 async function registerPlaygroud(context) {
111 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.install.goplay", () => (0, commands_1.installGoplay)()), coc_nvim_1.commands.registerCommand("go.playground", async () => (0, playground_1.openPlayground)(await (0, editor_1.activeTextDocument)())));
113 async function registerTools(context) {
114 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.install.tools", () => (0, commands_1.installTools)()));
116 //# sourceMappingURL=extension.js.map