Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-go / lib / utils / tests.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.extractFunctionName = exports.toogleTests = exports.generateTestsFunction = exports.generateTestsExported = exports.generateTestsAll = void 0;
4 const coc_nvim_1 = require("coc.nvim");
5 const vscode_uri_1 = require("vscode-uri");
6 const tools_1 = require("./tools");
7 const binaries_1 = require("../binaries");
8 ////////////////////////////////////////////////////////////////////////////////
9 async function generateTestsAll(document) {
10     if (isTest(document)) {
11         coc_nvim_1.workspace.showMessage("Document is a test file", "error");
12         return;
13     }
14     await runGotests(document, ["-all"]) && await openTests(document);
15 }
16 exports.generateTestsAll = generateTestsAll;
17 async function generateTestsExported(document) {
18     if (isTest(document)) {
19         coc_nvim_1.workspace.showMessage("Document is a test file", "error");
20         return;
21     }
22     await runGotests(document, ["-exported"]) && await openTests(document);
23 }
24 exports.generateTestsExported = generateTestsExported;
25 async function generateTestsFunction(document) {
26     if (isTest(document)) {
27         coc_nvim_1.workspace.showMessage("Document is a test file", "error");
28         return;
29     }
30     const { line } = await coc_nvim_1.workspace.getCursorPosition();
31     const text = await document.getText({
32         start: { line, character: 0 },
33         end: { line, character: Infinity },
34     });
35     coc_nvim_1.workspace.showMessage(text);
36     const funcName = extractFunctionName(text);
37     if (!funcName) {
38         coc_nvim_1.workspace.showMessage("No function found", "error");
39         return;
40     }
41     await runGotests(document, ["-only", `^${funcName}$`]) && await openTests(document);
42 }
43 exports.generateTestsFunction = generateTestsFunction;
44 async function toogleTests(document) {
45     const targetURI = isTest(document)
46         ? sourceURI(document)
47         : testURI(document);
48     return coc_nvim_1.workspace.openResource(targetURI);
49 }
50 exports.toogleTests = toogleTests;
51 ////////////////////////////////////////////////////////////////////////////////
52 async function openTests(document) {
53     return coc_nvim_1.workspace.openResource(testURI(document));
54 }
55 function isTest(document) {
56     return document.uri.endsWith('_test.go');
57 }
58 function testURI(document) {
59     return document.uri.replace(/(_test)?\.go$/, '_test.go');
60 }
61 function sourceURI(document) {
62     return document.uri.replace(/(_test)?\.go$/, '.go');
63 }
64 async function runGotests(document, args) {
65     const config = coc_nvim_1.workspace.getConfiguration().get('go.tests', {});
66     args.push(...(config.generateFlags || []), '-w', vscode_uri_1.URI.parse(document.uri).fsPath);
67     try {
68         const stdout = await tools_1.execTool(binaries_1.GOTESTS, args);
69         coc_nvim_1.workspace.showMessage(stdout || "");
70         return true;
71     }
72     catch (err) {
73         coc_nvim_1.workspace.showMessage(`Error: ${err}`, "error");
74         return false;
75     }
76 }
77 ////////////////////////////////////////////////////////////////////////////////
78 function extractFunctionName(line) {
79     const m = /^func +(\([^)]+\) +)?([^\s(]+)/.exec(line);
80     if (m) {
81         return m[2];
82     }
83 }
84 exports.extractFunctionName = extractFunctionName;
85 //# sourceMappingURL=tests.js.map