summaryrefslogtreecommitdiff
path: root/.config/coc/extensions/node_modules/coc-go/lib/commands.js
blob: f77d866687cb465545dd36712622c1a10d14f77e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.installTools = exports.installImpl = exports.installGoplay = exports.installGotests = exports.installGomodifytags = exports.checkGopls = exports.installGopls = exports.version = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const coc_nvim_1 = require("coc.nvim");
const tools_1 = require("./utils/tools");
const checktag_1 = tslib_1.__importDefault(require("./utils/checktag"));
const ui_1 = require("./utils/ui");
const config_1 = require("./utils/config");
const binaries_1 = require("./binaries");
const versions_1 = require("./utils/versions");
async function version() {
    const v1 = await pkgVersion();
    const v2 = await goplsVersion() || 'unknown';
    coc_nvim_1.window.showMessage(`Version: coc-go ${v1}; gopls ${v2}`, 'more');
}
exports.version = version;
async function installGopls(client) {
    await (0, tools_1.installGoBin)(binaries_1.GOPLS, true, goplsVersion);
    if (client.needsStop()) {
        await client.stop();
        client.restart();
    }
}
exports.installGopls = installGopls;
const checkInterval = 24 * 60 * 60 * 1000;
async function shouldCheckGopls() {
    const now = new Date().getTime();
    const last = await (0, config_1.getState)('gopls:last-check');
    if (last - (now - checkInterval) < 0) {
        await (0, config_1.setState)('gopls:last-check', new Date().getTime());
        return true;
    }
    return false;
}
async function checkGopls(client, mode) {
    try {
        if (!(await shouldCheckGopls())) {
            return;
        }
        let install = false;
        await (0, ui_1.withProgress)('Checking for new gopls version', async () => {
            const [current, latest] = await Promise.all([
                goplsVersion(),
                (0, checktag_1.default)("golang/tools", /^gopls\//),
            ]);
            if (!(0, versions_1.isValidVersion)(current) || !(0, versions_1.isValidVersion)(latest)) {
                coc_nvim_1.window.showMessage('checking for a new gopls version failed', 'warning');
                return;
            }
            switch ((0, versions_1.compareVersions)(latest, current)) {
                case 0:
                    coc_nvim_1.window.showMessage(`[gopls] up-to-date: ${current}`, 'more');
                    break;
                case 1:
                    switch (mode) {
                        case 'install':
                            install = true;
                            break;
                        case 'ask':
                            install = await coc_nvim_1.window.showPrompt(`[gopls] Install update? ${current} => ${latest}`);
                            break;
                        case 'inform':
                            coc_nvim_1.window.showMessage(`[gopls] update available: ${current} => ${latest}`);
                            break;
                    }
                    break;
                case -1:
                    coc_nvim_1.window.showMessage(`[gopls] current: ${current} | latest: ${latest}`, 'more');
                    break;
            }
        });
        if (install) {
            await installGopls(client);
        }
    }
    catch (e) {
        coc_nvim_1.window.showMessage(e.toString(), 'error');
    }
}
exports.checkGopls = checkGopls;
async function pkgVersion() {
    try {
        const pkgPath = path_1.default.resolve(__dirname, '..', 'package.json');
        const pkgContent = await fs_1.default.promises.readFile(pkgPath, 'utf8');
        return JSON.parse(pkgContent).version;
    }
    catch (err) {
        console.error(err);
    }
    return '';
}
async function goplsVersion() {
    const [, versionOut] = await (0, tools_1.runGoTool)("gopls", ["version"]);
    const m = versionOut.trim().match(/\s{4}golang\.org\/x\/tools\/gopls@(v?\d+\.\d+\.\d+) .*/);
    if (m && (0, versions_1.isValidVersion)(m[1])) {
        return m[1].replace(/^v/, '');
    }
    return '';
}
async function installGomodifytags() {
    await (0, tools_1.installGoBin)(binaries_1.GOMODIFYTAGS, true);
}
exports.installGomodifytags = installGomodifytags;
async function installGotests() {
    await (0, tools_1.installGoBin)(binaries_1.GOTESTS, true);
}
exports.installGotests = installGotests;
async function installGoplay() {
    await (0, tools_1.installGoBin)(binaries_1.GOPLAY, true);
}
exports.installGoplay = installGoplay;
async function installImpl() {
    await (0, tools_1.installGoBin)(binaries_1.IMPL, true);
}
exports.installImpl = installImpl;
async function installTools() {
    for (const tool of binaries_1.TOOLS) {
        await (0, tools_1.installGoBin)(tool, true);
    }
}
exports.installTools = installTools;
//# sourceMappingURL=commands.js.map