85 lines
3.2 KiB
JavaScript
85 lines
3.2 KiB
JavaScript
|
"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 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
|