From d9f44a9320aad1678e83a13804e7c0a240b10c41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Brelih?= Date: Mon, 25 Oct 2021 01:05:02 +0200 Subject: [PATCH] feat: default build tags (#38) * feat: default build tags Added option to specify default build tags for test and debugger. * Update dap.lua check if _GO_NVIM_CFG.build_tags ~= "" then Co-authored-by: Ales Brelih Co-authored-by: rayx --- lua/go.lua | 3 ++- lua/go/dap.lua | 17 +++++++++++++++-- lua/go/gotest.lua | 31 ++++++++++++++++++++----------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/lua/go.lua b/lua/go.lua index 21aee4c..ddc06d0 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -22,7 +22,8 @@ _GO_NVIM_CFG = { dap_debug = true, dap_debug_gui = true, dap_vt = true, -- false, true and 'all frames' - gopls_cmd = nil --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" } + gopls_cmd = nil, --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" } + build_tags = "", --- you can provide extra build tags for tests or debugger } local dap_config = function() diff --git a/lua/go/dap.lua b/lua/go/dap.lua index b8ebe05..b93518a 100644 --- a/lua/go/dap.lua +++ b/lua/go/dap.lua @@ -41,6 +41,14 @@ local function keybind() end +local function get_build_flags() + if _GO_NVIM_CFG.build_tags ~= "" then + return "-tags " .. _GO_NVIM_CFG.build_tags + else + return "" + end +end + local M = {} M.prepare = function() @@ -109,9 +117,12 @@ M.run = function(...) type = "go", name = "Debug", request = "launch", - dlvToolPath = vim.fn.exepath("dlv") + dlvToolPath = vim.fn.exepath("dlv"), + buildFlags = get_build_flags() + } + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) row, col = row, col + 1 @@ -174,6 +185,7 @@ function M.ultest_post() end args[#args + 1] = arg end + return { dap = { type = "go", @@ -181,7 +193,8 @@ function M.ultest_post() mode = "test", program = "./${relativeFileDirname}", dlvToolPath = vim.fn.exepath("dlv"), - args = args + args = args, + buildFlags = get_build_flags() }, parse_result = function(lines) return lines[#lines] == "FAIL" and 1 or 0 diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index d16331e..d284755 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -1,6 +1,24 @@ local M = {} local utils = require("go.utils") +local function get_build_tags(args) + local tags = {} + + if args ~= nil then + table.insert(tags, args) + end + + if _GO_NVIM_CFG.build_tags ~= "" then + table.insert(tags, _GO_NVIM_CFG.build_tags) + end + + if #tags == 0 then + return "" + end + + return [[-tags\ ]] .. table.concat(tags, ",") .. [[\ ]] +end + M.test_fun = function(args) local fpath = vim.fn.expand('%:p:h') @@ -11,14 +29,8 @@ M.test_fun = function(args) return end - local tag = '' - utils.log(args) - if args ~= nil then - tag = [[-tags\ ]] .. args .. [[\ ]] - end - utils.log("parnode" .. vim.inspect(ns)) - local cmd = [[setl makeprg=go\ test\ ]] .. tag .. [[-v\ -run\ ^]] .. ns.name .. [[\ ]] .. fpath + local cmd = [[setl makeprg=go\ test\ ]] .. get_build_tags(args) .. [[-v\ -run\ ^]] .. ns.name .. [[\ ]] .. fpath .. [[ | lua require"go.asyncmake".make()]] utils.log("test cmd", cmd) vim.cmd(cmd) @@ -28,13 +40,10 @@ M.test_file = function(args) local workfolder = vim.lsp.buf.list_workspace_folders()[1] local tag = '' utils.log(args) - if args ~= nil then - tag = [[-tags\ ]] .. args .. [[\ ]] - end local fpath = vim.fn.expand("%:p:h") .. '/..' -- local fpath = './' .. vim.fn.expand('%:h') .. '/...' utils.log("fpath" .. fpath) - local cmd = [[setl makeprg=go\ test\ ]] .. tag .. [[-v\ -run\ ]] .. fpath + local cmd = [[setl makeprg=go\ test\ ]] .. get_build_tags(args) .. [[-v\ -run\ ]] .. fpath .. [[| lua require"go.asyncmake".make()]] utils.log("test cmd", cmd) vim.cmd(cmd)