From ccaed8c5735a26430dec466eaf17e2ab2f02e431 Mon Sep 17 00:00:00 2001 From: ray-x Date: Fri, 3 Jun 2022 19:07:05 +1000 Subject: [PATCH] cli improvement for gomodifytags, gotest --- lua/go/chtsh.lua | 4 +++- lua/go/dap.lua | 8 ++++++-- lua/go/gotest.lua | 4 ++-- lua/go/tags.lua | 27 +++++++++++++++++++++++---- lua/go/utils.lua | 7 +++++-- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/lua/go/chtsh.lua b/lua/go/chtsh.lua index 56e7600..c4b58ca 100644 --- a/lua/go/chtsh.lua +++ b/lua/go/chtsh.lua @@ -16,7 +16,7 @@ local run = function(...) return end -- log(data) - if #data > 0 then + if #data > 4 then data = vim.list_slice(data, 4, #data) local TextView = utils.load_plugin("guihua.lua", "guihua.textview") if TextView then @@ -36,6 +36,8 @@ local run = function(...) vim.cmd(cmd) vim.cmd("e") end + else + vim.notify("No result " .. vim.inspect(data)) end end return { run = run } diff --git a/lua/go/dap.lua b/lua/go/dap.lua index 077fcc6..599dae0 100644 --- a/lua/go/dap.lua +++ b/lua/go/dap.lua @@ -359,11 +359,15 @@ M.run = function(...) -- if breakpoint is not set add breakpoint at current pos local pts = require("dap.breakpoints").get() - if #pts == 0 then - require("dap").toggle_breakpoint() + if utils.empty(pts) then + require("dap").set_breakpoint() end testfunc = require('go.gotest').get_test_func_name() + + if testfunc then + optarg["t"] = true + end if optarg["t"] then dap_cfg.name = dap_cfg.name .. " test" dap_cfg.mode = "test" diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index a2be26b..8e5bf24 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -60,7 +60,7 @@ M.get_build_tags = function(args) if optarg["t"] then table.insert(tags, optarg["t"]) end - if #tags > 0 then + if type(tags) == 'table' and #tags > 0 then return "-tags=" .. table.concat(tags, ","), reminder end end @@ -128,7 +128,7 @@ local function run_test(path, args) end if not empty(tags) then - cmd = vim.list_extend(cmd, tags) + cmd = vim.list_extend(cmd, {tags}) end if not empty(reminder) then cmd = vim.list_extend(cmd, reminder) diff --git a/lua/go/tags.lua b/lua/go/tags.lua index 26f6946..9d5c0b5 100644 --- a/lua/go/tags.lua +++ b/lua/go/tags.lua @@ -1,4 +1,5 @@ local utils = require("go.utils") +local log = utils.log local tags = {} -- support -add-tags, --add-options, -remove-tags, -remove-options, clear-tags, clear-options @@ -32,18 +33,27 @@ tags.modify = function(...) table.insert(setup, "-struct") table.insert(setup, struct_name) end - if transform then - table.insert(setup, "-transform") - table.insert(setup, transform) - end local arg = { ... } + local transflg = false for _, v in ipairs(arg) do table.insert(setup, v) + if v == "-transform" or v == "-t" then + transflg = true + end + end + if not transflg then + if transform then + table.insert(setup, "-transform") + table.insert(setup, transform) + end end if #arg == 1 and arg[1] ~= "-clear-tags" then table.insert(setup, "json") end + + log(setup) + -- vim.notify(vim.inspect(setup), vim.lsp.log_levels.DEBUG) vim.fn.jobstart(setup, { on_stdout = function(_, data, _) @@ -68,15 +78,24 @@ tags.modify = function(...) }) end + +-- e.g {"json,xml", "-transform", "camelcase"} tags.add = function(...) local cmd = { "-add-tags" } local arg = { ... } if #arg == 0 then arg = { "json" } end + + local tg = select(1, ...) + if tg == "-transform" then + table.insert(cmd, 'json') + end + for _, v in ipairs(arg) do table.insert(cmd, v) end + log(cmd) tags.modify(unpack(cmd)) end diff --git a/lua/go/utils.lua b/lua/go/utils.lua index 085c3ed..8b1f1bb 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -336,7 +336,7 @@ function util.load_plugin(name, modulename) if has then return plugin end - local pkg = packer_plugins or nil + local pkg = packer_plugins or nil if pkg ~= nil then -- packer installed local has_packer = pcall(require, "packer") @@ -469,6 +469,9 @@ function util.empty(t) if t == nil then return true end + if type(t) ~= "table" then + return false + end return next(t) == nil end @@ -540,7 +543,7 @@ function util.lines_from(file) end function util.list_directory() - return fn.map(fn.glob(fn.fnameescape("./") .. "/{,.}*/", 1, 1), 'fnamemodify(v:val, ":h:t")') + return fn.map(fn.glob(fn.fnameescape("./") .. "/{,.}*/", 1, 1), 'fnamemodify(v:val, ":h:t")') end function util.get_active_buf()