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 = [
21 async function activate(context) {
22 config_1.setStoragePath(context.storagePath);
23 if (config_1.getConfig().enable === false) {
26 registerGeneral(context);
27 registerGopls(context);
28 registerTest(context);
29 registerTags(context);
30 registerPlaygroud(context);
31 registerGoImpl(context);
32 registerTools(context);
34 exports.activate = activate;
35 async function registerGeneral(context) {
36 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.version", () => commands_1.version()));
38 async function registerGopls(context) {
39 const config = config_1.getConfig();
40 const command = await goplsPath(config.goplsPath);
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');
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),
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,
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);
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))) {
80 }), coc_nvim_1.commands.registerCommand("go.install.gopls", () => commands_1.installGopls(client)));
82 async function goplsPath(goplsPath) {
84 if (!await tools_1.commandExists(goplsPath)) {
85 coc_nvim_1.workspace.showMessage(`goplsPath is configured ("${goplsPath}"), but does not exist!`, 'error');
90 if (!await tools_1.installGoBin(binaries_1.GOPLS)) {
93 return tools_1.goBinPath(binaries_1.GOPLS);
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())));
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())));
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" })));
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())));
107 async function registerTools(context) {
108 context.subscriptions.push(coc_nvim_1.commands.registerCommand("go.install.tools", () => commands_1.installTools()));
110 //# sourceMappingURL=extension.js.map