summaryrefslogtreecommitdiff
path: root/.config/coc/extensions/node_modules/coc-go/lib/utils/tests.js
blob: 4aa14b2140788b9a8dd97c66ea69e8fa933331b6 (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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractFunctionName = exports.toogleTests = exports.generateTestsFunction = exports.generateTestsExported = exports.generateTestsAll = void 0;
const coc_nvim_1 = require("coc.nvim");
const vscode_uri_1 = require("vscode-uri");
const tools_1 = require("./tools");
const binaries_1 = require("../binaries");
////////////////////////////////////////////////////////////////////////////////
async function generateTestsAll(document) {
    if (isTest(document)) {
        coc_nvim_1.window.showMessage("Document is a test file", "error");
        return;
    }
    await runGotests(document, ["-all"]) && await openTests(document);
}
exports.generateTestsAll = generateTestsAll;
async function generateTestsExported(document) {
    if (isTest(document)) {
        coc_nvim_1.window.showMessage("Document is a test file", "error");
        return;
    }
    await runGotests(document, ["-exported"]) && await openTests(document);
}
exports.generateTestsExported = generateTestsExported;
async function generateTestsFunction(document) {
    if (isTest(document)) {
        coc_nvim_1.window.showMessage("Document is a test file", "error");
        return;
    }
    const { line } = await coc_nvim_1.window.getCursorPosition();
    const text = await document.getText({
        start: { line, character: 0 },
        end: { line, character: Infinity },
    });
    coc_nvim_1.window.showMessage(text);
    const funcName = extractFunctionName(text);
    if (!funcName) {
        coc_nvim_1.window.showMessage("No function found", "error");
        return;
    }
    await runGotests(document, ["-only", `^${funcName}$`]) && await openTests(document);
}
exports.generateTestsFunction = generateTestsFunction;
async function toogleTests(document) {
    const targetURI = isTest(document)
        ? sourceURI(document)
        : testURI(document);
    return coc_nvim_1.workspace.openResource(targetURI);
}
exports.toogleTests = toogleTests;
////////////////////////////////////////////////////////////////////////////////
async function openTests(document) {
    return coc_nvim_1.workspace.openResource(testURI(document));
}
function isTest(document) {
    return document.uri.endsWith('_test.go');
}
function testURI(document) {
    return document.uri.replace(/(_test)?\.go$/, '_test.go');
}
function sourceURI(document) {
    return document.uri.replace(/(_test)?\.go$/, '.go');
}
async function runGotests(document, args) {
    const config = coc_nvim_1.workspace.getConfiguration().get('go.tests', {});
    args.push(...(config.generateFlags || []), '-w', vscode_uri_1.URI.parse(document.uri).fsPath);
    try {
        const stdout = await (0, tools_1.execTool)(binaries_1.GOTESTS, args);
        coc_nvim_1.window.showMessage(stdout || "");
        return true;
    }
    catch (err) {
        coc_nvim_1.window.showMessage(`Error: ${err}`, "error");
        return false;
    }
}
////////////////////////////////////////////////////////////////////////////////
function extractFunctionName(line) {
    const m = /^func +(\([^)]+\) +)?([^\s(]+)/.exec(line);
    if (m) {
        return m[2];
    }
}
exports.extractFunctionName = extractFunctionName;
//# sourceMappingURL=tests.js.map