"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateTestsAll = generateTestsAll;
exports.generateTestsExported = generateTestsExported;
exports.generateTestsFunction = generateTestsFunction;
exports.toogleTests = toogleTests;
exports.extractFunctionName = extractFunctionName;
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);
}
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);
}
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);
}
async function toogleTests(document) {
    const targetURI = isTest(document)
        ? sourceURI(document)
        : testURI(document);
    return coc_nvim_1.workspace.openResource(targetURI);
}
////////////////////////////////////////////////////////////////////////////////
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];
    }
}
//# sourceMappingURL=tests.js.map