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