diff --git a/lua/go.lua b/lua/go.lua index 7f46985..92d93c4 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -121,7 +121,6 @@ function go.setup(cfg) vim.cmd([[command! -nargs=* GoStop lua require("go.asyncmake").stopjob()]]) -- if you want to output to quickfix - local sep = require("go.utils").sep() -- example to running test in split buffer -- vim.cmd( -- [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTest :setl makeprg=go\ test\ -v\ | lua require'go.runner'.make()]]) @@ -145,7 +144,9 @@ function go.setup(cfg) vim.cmd([[command! -nargs=* GoTestFunc lua require('go.gotest').test_func()]]) -- e.g. GoTestFile unit - vim.cmd([[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestFile lua require('go.gotest').test_file()]]) + vim.cmd( + [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestFile lua require('go.gotest').test_file()]] + ) vim.cmd( [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.package_complete GoTestPkg lua require('go.gotest').test_package()]] ) @@ -159,6 +160,9 @@ function go.setup(cfg) vim.cmd([[command! GoCodeLenAct lua require("go.codelens").run_action()]]) vim.cmd([[command! GoCodeAction lua require("go.codeaction").run_action()]]) + vim.cmd( + [[command! -nargs=* -complete=custom,v:lua.package.loaded.go.modify_tags_complete GoModifyTag lua require("go.tags").modify()]] + ) vim.cmd([[command! -nargs=* GoAddTag lua require("go.tags").add()]]) vim.cmd([[command! -nargs=* GoRmTag lua require("go.tags").rm()]]) vim.cmd( @@ -182,7 +186,7 @@ function go.setup(cfg) vim.cmd([[command! GoClearTag lua require("go.tags").clear()]]) vim.cmd([[command! GoCmt lua require("go.comment").gen()]]) - vim.cmd([[command! GoRename lua require("go.rename").run()]]) + vim.cmd([[command! GoRename lua require("go.rename").lsprename()]]) vim.cmd([[command! GoIfErr lua require("go.iferr").run()]]) vim.cmd([[command! GoFillStruct lua require("go.reftool").fillstruct()]]) vim.cmd([[command! GoFillSwitch lua require("go.reftool").fillswitch()]]) @@ -260,13 +264,14 @@ go.set_test_runner = function(runner) vim.notify("runner not supported " .. runner, vim.lsp.log_levels.ERROR) end -go.dbg_complete = function(arglead, cmdline, cursorpos) +-- go.dbg_complete = function(arglead, cmdline, cursorpos) +go.dbg_complete = function(_, _, _) -- richgo, go test, richgo, dlv, ginkgo local testopts = { "--test", "--nearest", "--file", "--package", "--attach", "--stop", "--restart" } return table.concat(testopts, "\n") end -go.tools_complete = function(arglead, cmdline, cursorpos) +go.tools_complete = function(_, _, _) local gotools = require("go.install").gotools table.sort(gotools) return table.concat(gotools, "\n") @@ -280,4 +285,16 @@ go.impl_complete = function(arglead, cmdline, cursorpos) -- return table.concat(testopts, "\n") end +go.modify_tags_complete = function(_, _, _) + local opts = { + "-add-tags", + "-add-options", + "-remove-tags", + "-remove-options", + "-clear-tags", + "-clear-options", + } + return table.concat(opts, "\n") +end + return go diff --git a/lua/go/alternate.lua b/lua/go/alternate.lua index 1d5f355..2506701 100644 --- a/lua/go/alternate.lua +++ b/lua/go/alternate.lua @@ -6,8 +6,8 @@ function M.alternate() vim.notify("no buffer name", vim.lsp.log_levels.ERROR) return end - local s, e = string.find(file, "_test%.go$") - local s2, e2 = string.find(file, "%.go$") + local s = string.find(file, "_test%.go$") + local s2 = string.find(file, "%.go$") if s ~= nil then alt_file = string.gsub(file, "_test.go", ".go") elseif s2 ~= nil then diff --git a/lua/go/asyncmake.lua b/lua/go/asyncmake.lua index 3629b0c..0ff950e 100644 --- a/lua/go/asyncmake.lua +++ b/lua/go/asyncmake.lua @@ -66,9 +66,9 @@ function M.make(...) local bufnr = vim.api.nvim_win_get_buf(winnr) local makeprg = vim.api.nvim_buf_get_option(bufnr, "makeprg") - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) + local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts) log(makeprg, args, optarg, reminder) - local indent = "%\\%( %\\)" + -- local indent = "%\\%( %\\)" if not makeprg then log("makeprog not setup") return @@ -242,7 +242,7 @@ function M.make(...) efm = efm, }) failed = true - log(errorlines[1]) + log(errorlines[1], job_id) vim.cmd([[echo v:shell_error]]) elseif #lines > 0 then vim.fn.setqflist({}, " ", { diff --git a/lua/go/codeaction.lua b/lua/go/codeaction.lua index b0aab08..2a4f7fa 100644 --- a/lua/go/codeaction.lua +++ b/lua/go/codeaction.lua @@ -1,6 +1,5 @@ local utils = require("go.utils") local log = utils.log -local api = vim.api -- ONLY SUPPORT GOPLS diff --git a/lua/go/codelens.lua b/lua/go/codelens.lua index 8cbbb2b..d2c2d0f 100644 --- a/lua/go/codelens.lua +++ b/lua/go/codelens.lua @@ -1,7 +1,5 @@ local utils = require("go.utils") -local log = utils.log local codelens = require("vim.lsp.codelens") -local api = vim.api local M = {} diff --git a/lua/go/coverage.lua b/lua/go/coverage.lua index 2f32512..78c0774 100644 --- a/lua/go/coverage.lua +++ b/lua/go/coverage.lua @@ -2,10 +2,11 @@ local utils = require("go.utils") local log = utils.log local coverage = {} local api = vim.api +local vfn = vim.fn local empty = utils.empty local M = {} local visable = false -_GO_NVIM_CFG = _GO_NVIM_CFG +-- _GO_NVIM_CFG = _GO_NVIM_CFG or {} local sign_define_cache = {} M.sign_map = { covered = "goCoverageCovered", uncover = "goCoverageUncover" } @@ -20,7 +21,7 @@ local function sign_get(bufnr, name) sign_define_cache[bufnr] = {} end if not sign_define_cache[bufnr][name] then - local s = vim.fn.sign_getdefined(name) + local s = vfn.sign_getdefined(name) if not vim.tbl_isempty(s) then sign_define_cache[bufnr][name] = s end @@ -31,10 +32,10 @@ end -- all windows and buffers local function all_bufnr() local bufnrl = {} - local buffers = vim.fn.getbufinfo({ bufloaded = 1, buflisted = 1 }) + local buffers = vfn.getbufinfo({ bufloaded = 1, buflisted = 1 }) for _, b in pairs(buffers) do - if not (vim.fn.empty(b.name) == 1 or b.hidden == 1) then + if not (vfn.empty(b.name) == 1 or b.hidden == 1) then local name = b.name local ext = string.sub(name, #name - 2) @@ -53,20 +54,20 @@ function M.define(bufnr, name, opts, redefine) -- vim.notify(bufnr .. name .. opts .. redefine, vim.lsp.log_levels.DEBUG) if redefine then sign_define_cache[bufnr][name] = nil - vim.fn.sign_undefine(name) - vim.fn.sign_define(name, opts) + vfn.sign_undefine(name) + vfn.sign_define(name, opts) elseif not sign_get(name) then -- log("define sign", name, vim.inspect(opts)) - vim.fn.sign_define(name, opts) + vfn.sign_define(name, opts) end -- vim.cmd([[sign list]]) end function M.remove(bufnr, lnum) if bufnr == nil then - bufnr = vim.fn.bufnr("$") + bufnr = vfn.bufnr("$") end - vim.fn.sign_unplace(ns, { buffer = bufnr, id = lnum }) + vfn.sign_unplace(ns, { buffer = bufnr, id = lnum }) end local function remove_all() @@ -100,7 +101,7 @@ function M.add(bufnr, signs) end -- log("placing", to_place) - vim.fn.sign_placelist(to_place) + vfn.sign_placelist(to_place) return to_place -- for testing end @@ -115,7 +116,7 @@ M.highlight = function() end local function augroup() - vim.cmd([[ augroup gopher.vim-coverage ]]) + vim.cmd([[ augroup gopher_vim_coverage ]]) vim.cmd([[ au! ]]) vim.cmd([[ au ColorScheme * lua require'go.coverage'.highlight() ]]) vim.cmd([[ au BufWinLeave *.go lua require'go.coverage'remove() ]]) @@ -126,11 +127,7 @@ end local function enable_all() local bufnrs = all_bufnr() for _, bufnr in pairs(bufnrs) do - -- enable - -- local bufnr = vim.fn.winbufnr(id) - local fn = vim.fn.bufname(bufnr) - - local filename = vim.fn.fnamemodify(fn, ":t") + local fn = vfn.bufname(bufnr) if coverage[fn] ~= nil then M.add(bufnr, coverage[fn]) end @@ -151,13 +148,13 @@ M.toggle = function(show) end local function parse_line(line) - local m = vim.fn.matchlist(line, [[\v([^:]+):(\d+)\.(\d+),(\d+)\.(\d+) (\d+) (\d+)]]) + local m = vfn.matchlist(line, [[\v([^:]+):(\d+)\.(\d+),(\d+)\.(\d+) (\d+) (\d+)]]) if empty(m) then return {} end local path = m[2] - local filename = vim.fn.fnamemodify(m[2], ":t") + local filename = vfn.fnamemodify(m[2], ":t") return { file = path, filename = filename, @@ -170,26 +167,26 @@ local function parse_line(line) } end -if vim.tbl_isempty(vim.fn.sign_getdefined(sign_covered)) then - vim.fn.sign_define(sign_covered, { +if vim.tbl_isempty(vfn.sign_getdefined(sign_covered)) then + vfn.sign_define(sign_covered, { text = _GO_NVIM_CFG.gocoverage_sign, texthl = "goCoverageCovered", }) end -if vim.tbl_isempty(vim.fn.sign_getdefined(sign_uncover)) then - vim.fn.sign_define(sign_uncover, { +if vim.tbl_isempty(vfn.sign_getdefined(sign_uncover)) then + vfn.sign_define(sign_uncover, { text = _GO_NVIM_CFG.gocoverage_sign, texthl = "goCoverageUncover", }) end M.read_cov = function(covfn) - if vim.fn.filereadable(covfn) == 0 then + if vfn.filereadable(covfn) == 0 then vim.notify(string.format("cov file not exist: %s", covfn), vim.lsp.log_levels.WARN) return end - local cov = vim.fn.readfile(covfn) + local cov = vfn.readfile(covfn) -- log(vim.inspect(cov)) for _, line in pairs(cov) do local cl = parse_line(line) @@ -207,8 +204,8 @@ M.read_cov = function(covfn) local added = {} for _, bid in pairs(bufnrs) do -- if added[bid] == nil then - local fn = vim.fn.bufname(bid) - fn = vim.fn.fnamemodify(fn, ":t") + local fn = vfn.bufname(bid) + fn = vfn.fnamemodify(fn, ":t") log(bid, fn) M.add(bid, coverage[fn]) visable = true @@ -220,8 +217,8 @@ end M.run = function(...) local get_build_tags = require("go.gotest").get_build_tags - -- local cov = vim.fn.tempname() - local pwd = vim.fn.getcwd() + -- local cov = vfn.tempname() + local pwd = vfn.getcwd() local cov = pwd .. utils.sep() .. "cover.cov" local args = { ... } @@ -256,7 +253,6 @@ M.run = function(...) log("run coverage", cmd) - local argsstr = "" if _GO_NVIM_CFG.run_in_floaterm then cmd = table.concat(cmd, " ") if empty(args2) then @@ -268,9 +264,9 @@ M.run = function(...) return end - local j = vim.fn.jobstart(cmd, { + vfn.jobstart(cmd, { on_stdout = function(jobid, data, event) - log("go coverage " .. vim.inspect(data)) + log("go coverage " .. vim.inspect(data), jobid, event) vim.list_extend(lines, data) end, on_stderr = function(job_id, data, event) @@ -298,13 +294,13 @@ M.run = function(...) local lp = table.concat(lines, "\n") vim.notify(string.format("test finished:\n %s", lp), vim.lsp.log_levels.INFO) coverage = M.read_cov(cov) - vim.fn.setqflist({}, " ", { + vfn.setqflist({}, " ", { title = cmd, lines = lines, efm = vim.o.efm .. [[,]] .. require("go.gotest").efm(), }) - vim.api.nvim_command("doautocmd QuickFixCmdPost") - vim.fn.delete(cov) + api.nvim_command("doautocmd QuickFixCmdPost") + vfn.delete(cov) end, }) end diff --git a/lua/go/dap.lua b/lua/go/dap.lua index a446bef..fde1092 100644 --- a/lua/go/dap.lua +++ b/lua/go/dap.lua @@ -128,7 +128,7 @@ M.save_brks = function() local bks = require("dap.breakpoints").get() local all_bks = {} if bks and next(bks) then - local cfg, fld = require("go.project").setup() + local _, fld = require("go.project").setup() for bufnr, bk in pairs(bks) do local uri = vim.uri_from_bufnr(bufnr) local _bk = {} @@ -181,7 +181,7 @@ M.clear_bks = function() if not vim.api.nvim_buf_is_loaded(bufnr) then vim.fn.bufload(bufnr) end - for index, lnum in ipairs(brk) do + for _, lnum in ipairs(brk) do require("dap.breakpoints").set({}, bufnr, lnum.line) end end diff --git a/lua/go/format.lua b/lua/go/format.lua index 0946208..bd53caa 100644 --- a/lua/go/format.lua +++ b/lua/go/format.lua @@ -6,6 +6,7 @@ local log = utils.log local max_len = _GO_NVIM_CFG.max_line_len or 120 local goimport = _GO_NVIM_CFG.goimport or "goimports" local gofmt = _GO_NVIM_CFG.gofmt or "gofumpt" +local vfn = vim.fn local gofmt_args = _GO_NVIM_CFG.gofmt_args or { "--max-len=" .. tostring(max_len), "--base-formatter=" .. gofmt, @@ -22,7 +23,7 @@ local run = function(fmtargs, bufnr, cmd) bufnr = bufnr or 0 if _GO_NVIM_CFG.gofmt == "gopls" then if not vim.api.nvim_buf_is_loaded(bufnr) then - vim.fn.bufload(bufnr) + vfn.bufload(bufnr) end vim.cmd("write") @@ -35,7 +36,7 @@ local run = function(fmtargs, bufnr, cmd) log("formatting buffer... " .. vim.inspect(args), vim.lsp.log_levels.DEBUG) if bufnr == 0 then - if vim.fn.getbufinfo("%")[1].changed == 1 then + if vfn.getbufinfo("%")[1].changed == 1 then vim.cmd("write") end end @@ -48,8 +49,8 @@ local run = function(fmtargs, bufnr, cmd) end log("fmt cmd:", args) - local j = vim.fn.jobstart(args, { - on_stdout = function(job_id, data, event) + local j = vfn.jobstart(args, { + on_stdout = function(_, data, _) data = utils.handle_job_data(data) if not data then return @@ -64,19 +65,18 @@ local run = function(fmtargs, bufnr, cmd) -- log("stdout" .. vim.inspect(data)) old_lines = nil end, - on_stderr = function(job_id, data, event) + on_stderr = function(_, data, _) data = utils.handle_job_data(data) log(vim.inspect(data) .. "from stderr") end, - on_exit = function(id, data, event) + on_exit = function(_, data, _) -- id, data, event -- log(vim.inspect(data) .. "exit") - -- log("current data " .. vim.inspect(new_lines)) if data.code ~= 0 then return vim.notify("golines failed " .. tostring(data), vim.lsp.log_levels.ERROR) end old_lines = nil vim.defer_fn(function() - if vim.fn.getbufinfo("%")[1].changed == 1 then + if vfn.getbufinfo("%")[1].changed == 1 then vim.cmd("write") end end, 300) @@ -84,8 +84,8 @@ local run = function(fmtargs, bufnr, cmd) stdout_buffered = true, stderr_buffered = true, }) - vim.fn.chansend(j, old_lines) - vim.fn.chanclose(j, "stdin") + vfn.chansend(j, old_lines) + vfn.chanclose(j, "stdin") end local M = {} @@ -98,7 +98,7 @@ M.gofmt = function(...) local args = ... or {} local getopt = require("go.alt_getopt") - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) + local optarg = getopt.get_opts(args, short_opts, long_opts) log(optarg) vim.env.GO_FMT = "gofumpt" @@ -121,7 +121,7 @@ M.gofmt = function(...) run(a, b.bufnr) end else - if vim.fn.getbufinfo("%")[1].changed == 1 then + if vfn.getbufinfo("%")[1].changed == 1 then vim.cmd("write") end run(a, 0) @@ -139,7 +139,7 @@ end M.goimport = function(...) local args = { ... } if _GO_NVIM_CFG.goimport == "gopls" then - if vim.fn.empty(args) == 1 then + if vfn.empty(args) == 1 then return M.org_imports(1000) else local path = select(1, ...) @@ -147,7 +147,6 @@ M.goimport = function(...) return gopls.import(path) end end - local a1 = select(1, args) local buf = vim.api.nvim_get_current_buf() require("go.install").install(goimport) -- specified the pkg name diff --git a/lua/go/ginkgo.lua b/lua/go/ginkgo.lua index 6065c15..aeeb644 100644 --- a/lua/go/ginkgo.lua +++ b/lua/go/ginkgo.lua @@ -2,6 +2,7 @@ local M = {} local utils = require("go.utils") local log = utils.log +local vfn = vim.fn local long_opts = { verbose = "v", compile = "c", @@ -17,7 +18,7 @@ local short_opts = "vct:bsF" local function get_build_tags(args) local tags = {} - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) + local optarg = getopt.get_opts(args, short_opts, long_opts) if optarg['t'] then table.insert(tags, optarg['t']) end @@ -63,8 +64,8 @@ end M.test_func = function(...) local args = { ... } log(args) - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) - local fpath = vim.fn.expand("%:p:h") + local optarg = getopt.get_opts(args, short_opts, long_opts) + local fpath = vfn.expand("%:p:h") local row, col = unpack(vim.api.nvim_win_get_cursor(0)) row, col = row, col + 1 @@ -104,8 +105,8 @@ M.test_file = function(...) local args = { ... } log(args) -- require sed - local fpath = vim.fn.expand("%:p:h") - local fname = vim.fn.expand("%:p") + local fpath = vfn.expand("%:p:h") + local fname = vfn.expand("%:p") log(fpath, fname) diff --git a/lua/go/godoc.lua b/lua/go/godoc.lua index 1ffbf15..f35c1b1 100644 --- a/lua/go/godoc.lua +++ b/lua/go/godoc.lua @@ -2,19 +2,20 @@ local utils = require("go.utils") local log = utils.log local gopls = require("go.gopls") local help_items = {} +local vfn = vim.fn local m = {} -function m.help_complete(arglead, cmdline, cursorpos) +function m.help_complete(_, _, _) if #help_items < 1 then - local doc = vim.fn.systemlist("go help") + local doc = vfn.systemlist("go help") if vim.v.shell_error ~= 0 then vim.notify(string.format("failed to run go help %d", vim.v.shell_error), vim.lsp.log_levels.ERROR) return end for _, line in ipairs(doc) do - local m = string.match(line, "^%s+([%w-]+)") - if m ~= nil and m ~= "go" then - table.insert(help_items, m) + local m1 = string.match(line, "^%s+([%w-]+)") + if m1 ~= nil and m1 ~= "go" then + table.insert(help_items, m1) end end table.sort(help_items) @@ -42,7 +43,7 @@ end local function match_partial_item_name(pkg, pattern) local cmd = string.format("go doc %s", pkg) - local doc = vim.fn.systemlist(cmd) + local doc = vfn.systemlist(cmd) if vim.v.shell_error ~= 0 then return end @@ -71,7 +72,7 @@ local function match_partial_item_name(pkg, pattern) return items end -function m.doc_complete(arglead, cmdline, cursorpos) +function m.doc_complete(_, cmdline, _) local words = vim.split(cmdline, "%s+") if string.match(words[#words], "^-") then log(words) @@ -113,7 +114,7 @@ function m.doc_complete(arglead, cmdline, cursorpos) return "" end -m.run = function(kind, func, ...) +m.run = function(kind, func) log(func) if func == nil or next(func) == nil then @@ -123,8 +124,8 @@ m.run = function(kind, func, ...) local setup = { "go", kind, unpack(func) } -- - local j = vim.fn.jobstart(setup, { - on_stdout = function(jobid, data, event) + vfn.jobstart(setup, { + on_stdout = function(_, data, _) data = utils.handle_job_data(data) if not data then return diff --git a/lua/go/golint.lua b/lua/go/golint.lua new file mode 100644 index 0000000..b1c510f --- /dev/null +++ b/lua/go/golint.lua @@ -0,0 +1,26 @@ +-- local ts_utils = require 'nvim-treesitter.ts_utils' +local utils = require("go.utils") + +local golangci_lint = "golangci-lint" +local run = function(...) + require("go.install").install(golangci_lint) + local fname = vim.fn.expand("%:p") -- %:p:h ? %:p + + local cmd = string.format("golangci-lint -pos %d", byte_offset) + + local data = vim.fn.systemlist(cmd, vim.fn.bufnr("%")) + + data = utils.handle_job_data(data) + if not data then + return + end + + local pos = vim.fn.getcurpos()[2] + vim.fn.append(pos, data) + + vim.cmd("silent normal! j=2j") + vim.fn.setpos(".", pos) + vim.cmd("silent normal! 4j") + -- +end +return { run = run } diff --git a/lua/go/gopls.lua b/lua/go/gopls.lua index f777581..63e58ca 100644 --- a/lua/go/gopls.lua +++ b/lua/go/gopls.lua @@ -1,5 +1,6 @@ local utils = require("go.utils") local log = utils.log +local vfn = vim.fn local M = {} local cmds = {} @@ -108,17 +109,17 @@ end -- check_for_upgrades({Modules = {'package'}}) function M.version() - local cache_dir = vim.fn.stdpath("cache") + local cache_dir = vfn.stdpath("cache") local path = string.format("%s%sversion.txt", cache_dir, utils.sep()) local cfg = _GO_NVIM_CFG or {} local gopls = cfg.gopls_cmd or { "gopls" } - if vim.fn.executable(gopls[1]) == 0 then + if vfn.executable(gopls[1]) == 0 then vim.notify("gopls not found", vim.log.levels.WARN) return end - vim.fn.jobstart({ gopls[1], "version" }, { - on_stdout = function(c, data, name) + vfn.jobstart({ gopls[1], "version" }, { + on_stdout = function(_, data, _) local msg = "" if type(data) == "table" and #data > 0 then data = table.concat(data, " ") @@ -147,7 +148,7 @@ function M.version() local f = io.open(path, "r") if f == nil then local version_cmd = gopls[1] .. " version" - return vim.fn.system(version_cmd):match("%s+v([%d%.]+)%s+") + return vfn.system(version_cmd):match("%s+v([%d%.]+)%s+") end local version = f:read("*l") f:close() diff --git a/lua/go/gotest.lua b/lua/go/gotest.lua index dc15f16..a2be26b 100644 --- a/lua/go/gotest.lua +++ b/lua/go/gotest.lua @@ -5,6 +5,7 @@ local log = utils.log local empty = utils.empty local ginkgo = require("go.ginkgo") local getopt = require("go.alt_getopt") +local vfn = vim.fn local long_opts = { verbose = "v", @@ -20,7 +21,7 @@ local short_opts = "vct:bsF" local bench_opts = { "-benchmem", "-cpuprofile", "profile.out" } M.efm = function() - local indent = [[%\\%( %\\)]] + -- local indent = [[%\\%( %\\)]] local efm = [[%-G=== RUN %.%#]] efm = efm .. [[,%-G" .. indent .. "%#--- PASS: %.%#]] efm = efm .. [[,%G--- FAIL: %\\%(Example%\\)%\\@=%m (%.%#)]] @@ -55,7 +56,7 @@ M.get_build_tags = function(args) tags = { _GO_NVIM_CFG.build_tags } end - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) + local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts) if optarg["t"] then table.insert(tags, optarg["t"]) end @@ -65,25 +66,25 @@ M.get_build_tags = function(args) end local function richgo(cmd) - if cmd[1] == "go" and vim.fn.executable("richgo") then + if cmd[1] == "go" and vfn.executable("richgo") then cmd[1] = "richgo" end return cmd end local function get_test_filebufnr() - local fn = vim.fn.expand("%") + local fn = vfn.expand("%") log(fn) local bufnr = vim.api.nvim_get_current_buf() if not fn:find("test%.go$") then fn = require("go.alternate").alternate() - fn = vim.fn.fnamemodify(fn, ":p") -- expand to full path + fn = vfn.fnamemodify(fn, ":p") -- expand to full path local uri = vim.uri_from_fname(fn) bufnr = vim.uri_to_bufnr(uri) log(fn, bufnr, uri) if not vim.api.nvim_buf_is_loaded(bufnr) then - vim.fn.bufload(bufnr) + vfn.bufload(bufnr) end end return bufnr @@ -94,9 +95,9 @@ local function run_test(path, args) log(args) local compile = false local bench = false - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) + local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts) if optarg["c"] then - path = utils.rel_path(true) -- vim.fn.expand("%:p:h") can not resolve releative path + path = utils.rel_path(true) -- vfn.expand("%:p:h") can not resolve releative path compile = true end if optarg["b"] then @@ -183,9 +184,9 @@ M.test = function(...) } local test_short_opts = "vct:bsfnpF" - local optarg, optind, reminder = getopt.get_opts(args, test_short_opts, test_opts) + local optarg, _, reminder = getopt.get_opts(args, test_short_opts, test_opts) - vim.fn.setqflist({}) + vfn.setqflist({}) if optarg["n"] then --nearest optarg["n"] = nil @@ -234,7 +235,7 @@ end M.test_package = function(...) local args = { ... } log(args) - local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":.") .. sep .. "..." + local fpath = "." .. sep .. vfn.fnamemodify(vfn.expand("%:h"), ":.") .. sep .. "..." utils.log("fpath: " .. fpath) return run_test(fpath, args) end @@ -246,6 +247,9 @@ M.get_test_func_name = function() if empty(ns) then return nil end + if ns == nil or ns.name == nil then + return nil + end if not string.find(ns.name, "[T|t]est") then -- not in a test function local fns = M.get_testfunc() @@ -265,11 +269,11 @@ M.test_func = function(...) local args = { ... } log(args) - ns = M.get_test_func_name() + local ns = M.get_test_func_name() if empty(ns) then return M.select_tests() end - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) + local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts) local tags = M.get_build_tags(args) utils.log("parnode" .. vim.inspect(ns)) @@ -302,6 +306,9 @@ M.test_func = function(...) if tags and tags ~= "" then table.insert(cmd, tags) end + if ns == nil or ns.name == nil then + return + end if ns.name:find("Bench") then local bench = "-bench=" .. ns.name @@ -312,7 +319,7 @@ M.test_func = function(...) table.insert(cmd, [[^]] .. ns.name) end - local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:h"), ":.") + local fpath = "." .. sep .. vfn.fnamemodify(vfn.expand("%:h"), ":.") table.insert(cmd, fpath) if test_runner == "dlv" then @@ -344,24 +351,24 @@ M.test_file = function(...) -- require sed -- local testcases = [[sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']] - -- local fpath = vim.fn.expand("%:p") + -- local fpath = vfn.expand("%:p") - local fpath = "." .. sep .. vim.fn.fnamemodify(vim.fn.expand("%:p"), ":.") + local fpath = "." .. sep .. vfn.fnamemodify(vfn.expand("%:p"), ":.") -- utils.log(args) local cmd = [[cat ]] .. fpath .. [[| sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\|/g']] -- TODO maybe with treesitter or lsp list all functions in current file and regex with Test - if vim.fn.executable("sed") == 0 then + if vfn.executable("sed") == 0 then M.test_package(...) return end - local optarg, optind, reminder = getopt.get_opts(args, short_opts, long_opts) + local optarg, _, reminder = getopt.get_opts(args, short_opts, long_opts) local run_in_floaterm = optarg["F"] or _GO_NVIM_CFG.run_in_floaterm - local tests = vim.fn.systemlist(cmd) + local tests = vfn.systemlist(cmd) utils.log(cmd, tests) tests = tests[1] - if vim.fn.empty(tests) == 1 then + if vfn.empty(tests) == 1 then vim.notify("no test found fallback to package test", vim.lsp.log_levels.DEBUG) M.test_package(...) return @@ -434,7 +441,7 @@ M.run_file = function() local query = vim.treesitter.parse_query("go", require("go.ts.textobjects").query_test_func) local test_names = {} - for id, node, metadata in query:iter_captures(tree:root(), bufnr, 0, -1) do + for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do local name = query.captures[id] -- name of the capture in the query if name == "test_name" then table.insert(test_names, vim.treesitter.query.get_node_text(node, bufnr)) @@ -457,7 +464,7 @@ M.get_testfunc = function() local query = vim.treesitter.parse_query("go", require("go.ts.go").query_test_func) local test_names = {} - for id, node, metadata in query:iter_captures(tree:root(), bufnr, 0, -1) do + for id, node in query:iter_captures(tree:root(), bufnr, 0, -1) do local name = query.captures[id] -- name of the capture in the query if name == "test_name" then table.insert(test_names, vim.treesitter.query.get_node_text(node, bufnr)) diff --git a/lua/go/gotests.lua b/lua/go/gotests.lua index b37783a..25df994 100644 --- a/lua/go/gotests.lua +++ b/lua/go/gotests.lua @@ -47,7 +47,9 @@ ut.fun_test = function(parallel) if empty(ns) then return end - + if ns == nil or ns.name == nil then + return + end -- utils.log("parnode" .. vim.inspect(ns)) local funame = ns.name -- local rs, re = ns.dim.s.r, ns.dim.e.r diff --git a/lua/go/health.lua b/lua/go/health.lua index 8dcdd32..469e2db 100644 --- a/lua/go/health.lua +++ b/lua/go/health.lua @@ -10,12 +10,13 @@ local ok = health.report_ok local error = health.report_error local warn = health.report_warn local info = health.report_info +local vfn = vim.fn local function binary_check() health.report_start("Binaries") local no_err = true local go_bin = _GO_NVIM_CFG.go or "go" - if vim.fn.executable(go_bin) == 1 then + if vfn.executable(go_bin) == 1 then info(go_bin .. " installed.") else error(go_bin .. " is not installed.") @@ -23,7 +24,7 @@ local function binary_check() end for _, val in ipairs(tools) do log(val) - if vim.fn.executable(val) == 1 then + if vfn.executable(val) == 1 then info("Tool installed: " .. val) else warn("Missing tool: " .. val) @@ -33,19 +34,19 @@ local function binary_check() ok("All binaries installed") end - if vim.fn.executable('sed') == 1 then + if vfn.executable('sed') == 1 then info("sed installed.") else warn("sed is not installed.") end - if vim.fn.executable('sed') == 1 then + if vfn.executable('sed') == 1 then info("sed installed.") else warn("sed is not installed.") end - if vim.fn.executable('curl') == 1 then + if vfn.executable('curl') == 1 then info("curl installed.") else warn("curl is not installed.") @@ -77,8 +78,8 @@ local function plugin_check() end end if ts_installed then - local info = require("nvim-treesitter.info").installed_parsers() - if vim.tbl_contains(info, "go") then + local _info = require("nvim-treesitter.info").installed_parsers() + if vim.tbl_contains(_info, "go") then ok("nvim-treesitter-go is installed") else warn("nvim-treesitter-go is not installed, Please run TSInstall go to install") diff --git a/lua/go/iferr.lua b/lua/go/iferr.lua index 8bfb2e4..c75d9c2 100644 --- a/lua/go/iferr.lua +++ b/lua/go/iferr.lua @@ -1,27 +1,27 @@ -- local ts_utils = require 'nvim-treesitter.ts_utils' local utils = require("go.utils") +local vfn = vim.fn local iferr = "iferr" -local run = function(...) +local run = function() require("go.install").install(iferr) - local fname = vim.fn.expand("%:p") -- %:p:h ? %:p - local byte_offset = vim.fn.wordcount().cursor_bytes + local byte_offset = vfn.wordcount().cursor_bytes local cmd = string.format('iferr -pos %d', byte_offset) - local data = vim.fn.systemlist(cmd, vim.fn.bufnr('%')) + local data = vfn.systemlist(cmd, vfn.bufnr('%')) data = utils.handle_job_data(data) if not data then return end - local pos = vim.fn.getcurpos()[2] - vim.fn.append(pos, data) + local pos = vfn.getcurpos()[2] + vfn.append(pos, data) vim.cmd('silent normal! j=2j') - vim.fn.setpos('.', pos) + vfn.setpos('.', pos) vim.cmd('silent normal! 4j') -- diff --git a/lua/go/impl.lua b/lua/go/impl.lua index f02b35f..7a0a94e 100644 --- a/lua/go/impl.lua +++ b/lua/go/impl.lua @@ -1,6 +1,6 @@ -- local ts_utils = require 'nvim-treesitter.ts_utils' local utils = require("go.utils") - +local vfn = vim.fn local impl = "impl" -- GoImpl f *Foo io.Writer -- use ts to get name local function get_struct_name() @@ -33,7 +33,7 @@ local run = function(...) local recv = get_struct_name() if #arg == 0 then - iface = vim.fn.input("Impl: generating method stubs for interface: ") + iface = vfn.input("Impl: generating method stubs for interface: ") vim.cmd("redraw!") if iface == "" then print("Impl: please input interface name e.g. io.Reader") @@ -61,23 +61,23 @@ local run = function(...) end utils.log(#arg, recv_name, recv, iface) - local dir = vim.fn.fnameescape(vim.fn.expand("%:p:h")) + local dir = vfn.fnameescape(vfn.expand("%:p:h")) setup = { setup, "-dir", dir, recv, iface } utils.log(setup) -- vim.cmd("normal! $%") -- do a bracket match. changed to treesitter - local data = vim.fn.systemlist(setup) + local data = vfn.systemlist(setup) data = utils.handle_job_data(data) if not data then return end -- - local pos = vim.fn.getcurpos()[2] + local pos = vfn.getcurpos()[2] table.insert(data, 1, "") - vim.fn.append(pos, data) + vfn.append(pos, data) -- vim.cmd("silent normal! j=2j") - -- vim.fn.setpos(".", pos) + -- vfn.setpos(".", pos) -- vim.cmd("silent normal! 4j") -- end @@ -87,7 +87,7 @@ local function match_iface_name(part) utils.log(pkg, iface) local cmd = string.format("go doc %s", pkg) - local doc = vim.fn.systemlist(cmd) + local doc = vfn.systemlist(cmd) if vim.v.shell_error ~= 0 then return end @@ -103,7 +103,8 @@ local function match_iface_name(part) return ifaces end -function complete(arglead, cmdline, cursorpos) +-- function complete(arglead, cmdline, cursorpos) +local function complete(_, cmdline, _) local words = vim.split(cmdline, [[%s+]]) local gopls = require("go.gopls") local last = words[#words] @@ -115,8 +116,7 @@ function complete(arglead, cmdline, cursorpos) end end - local bnum = vim.api.nvim_get_current_buf() - return vim.fn.uniq(vim.fn.sort(gopls.list_pkgs(bnum))) + return vfn.uniq(vfn.sort(gopls.list_pkgs())) end return { run = run, complete = complete } diff --git a/lua/go/install.lua b/lua/go/install.lua index d09854e..8a45ae4 100644 --- a/lua/go/install.lua +++ b/lua/go/install.lua @@ -1,5 +1,5 @@ local uv = vim.loop -local DIR_SEP = package.config:sub(1, 1) +local DIR_SEP = package.config[1] local log = require("go.utils").log local url = { @@ -28,7 +28,7 @@ local function is_installed(bin) local env_path = os.getenv("PATH") local base_paths = vim.split(env_path, ":", true) - for key, value in pairs(base_paths) do + for _, value in pairs(base_paths) do if uv.fs_stat(value .. DIR_SEP .. bin) then return true end @@ -50,7 +50,7 @@ local function go_install(pkg) local setup = { "go", "install", u } vim.fn.jobstart(setup, { - on_stdout = function(c, data, name) + on_stdout = function(_, data, _) log(setup) if type(data) == "table" and #data > 0 then data = table.concat(data, " ") @@ -84,13 +84,13 @@ local function update(bin) end local function install_all() - for key, value in pairs(url) do + for key, _ in pairs(url) do install(key) end end local function update_all() - for key, value in pairs(url) do + for key, _ in pairs(url) do update(key) end end diff --git a/lua/go/launch.lua b/lua/go/launch.lua index bfb8537..cf0a971 100644 --- a/lua/go/launch.lua +++ b/lua/go/launch.lua @@ -84,13 +84,13 @@ local util = require("go.utils") local log = util.log local M = {} local sep = require("go.utils").sep() - +local vfn = vim.fn function M.vs_launch() - local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd() + local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd() local launch_json = _GO_NVIM_CFG.launch_json or (workfolder .. sep .. ".vscode" .. sep .. "launch.json") log(launch_json) - if vim.fn.filereadable(launch_json) == 1 then + if vfn.filereadable(launch_json) == 1 then return true, launch_json else return false, launch_json @@ -98,23 +98,23 @@ function M.vs_launch() end function M.config() - local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd() + local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd() local launch_json = _GO_NVIM_CFG.launch_json or (workfolder .. sep .. ".vscode" .. sep .. "launch.json") local launch_dir = string.match(launch_json, ".*" .. sep) local cmd = "e " .. launch_json - if vim.fn.isdirectory(launch_dir) == 0 then - vim.fn.mkdir(launch_dir) + if vfn.isdirectory(launch_dir) == 0 then + vfn.mkdir(launch_dir) end - if vim.fn.filereadable(launch_json) == 1 then + if vfn.filereadable(launch_json) == 1 then return vim.cmd(cmd) end - -- vim.fn.writefile(launch_json_content, launch_json) - local contents = vim.fn.split(launch_json_content, "\n") - vim.fn.writefile(contents, launch_json) + -- vfn.writefile(launch_json_content, launch_json) + local contents = vfn.split(launch_json_content, "\n") + vfn.writefile(contents, launch_json) vim.cmd(cmd) end diff --git a/lua/go/lsp.lua b/lua/go/lsp.lua index 47890e8..2d04d3b 100644 --- a/lua/go/lsp.lua +++ b/lua/go/lsp.lua @@ -50,7 +50,7 @@ local on_attach = function(client, bufnr) buf_set_keymap("n", "wr", "lua vim.lsp.buf.remove_workspace_folder()", opts) buf_set_keymap("n", "wl", "lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))", opts) buf_set_keymap("n", "D", "lua vim.lsp.buf.type_definition()", opts) - buf_set_keymap("n", "rn", "lua require('go.rename').lsprename()", opts) + buf_set_keymap("n", "rn", "lua require('go.rename').run()", opts) buf_set_keymap("n", "ca", "lua require('go.codeaction').run_action()", opts) buf_set_keymap("n", "gr", "lua vim.lsp.buf.references()", opts) buf_set_keymap("n", "e", "lua vim.lsp.diagnostic.show_line_diagnostics()", opts) diff --git a/lua/go/lsp_diag.lua b/lua/go/lsp_diag.lua index f98489c..204e075 100644 --- a/lua/go/lsp_diag.lua +++ b/lua/go/lsp_diag.lua @@ -4,6 +4,7 @@ -- New signature on_publish_diagnostics({_}, {result}, {ctx}, {config}) debug = debug or nil local nvim_0_6 = false +local vfn = vim.fn if debug.getinfo(vim.lsp.handlers["textDocument/publishDiagnostics"]).nparams == 4 then nvim_0_6 = true end @@ -14,7 +15,7 @@ local function hdlr(result) local s = result.uri local fname = s for _, v in ipairs(result.diagnostics) do - local i, j = string.find(s, "file://") + local _, j = string.find(s, "file://") if j then fname = string.sub(s, j + 1) end @@ -25,23 +26,23 @@ local function hdlr(result) text = v.message, }) end - local old_items = vim.fn.getqflist() + local old_items = vfn.getqflist() for _, old_item in ipairs(old_items) do if vim.uri_from_bufnr(old_item.bufnr) ~= result.uri then table.insert(item_list, old_item) end end - vim.fn.setqflist({}, " ", { title = "LSP", items = item_list }) + vfn.setqflist({}, " ", { title = "LSP", items = item_list }) end end local diag_hdlr_0_5 = function(err, result, ctx, config) - -- vim.lsp.diagnostic.clear(vim.fn.bufnr(), client.id, nil, nil) + -- vim.lsp.diagnostic.clear(vfn.bufnr(), client.id, nil, nil) vim.lsp.diagnostic.on_publish_diagnostics(err, result, ctx, config) hdlr(result) end local diag_hdlr_0_6 = function(err, result, ctx, config) - -- vim.lsp.diagnostic.clear(vim.fn.bufnr(), client.id, nil, nil) + -- vim.lsp.diagnostic.clear(vfn.bufnr(), client.id, nil, nil) vim.lsp.diagnostic.on_publish_diagnostics(err, result, ctx, config) hdlr(result) end diff --git a/lua/go/package.lua b/lua/go/package.lua index c3956ec..5a10270 100644 --- a/lua/go/package.lua +++ b/lua/go/package.lua @@ -1,6 +1,7 @@ local golist = require'go.list'.list local util = require 'go.utils' local log = util.log +local vfn = vim.fn return { complete = function() local ok, l = golist(false, {util.all_pkgs()}) @@ -8,12 +9,12 @@ return { log('Failed to find all packages for current module/project.') end local curpkgmatch = false - local curpkg = vim.fn.fnamemodify(vim.fn.expand('%'), ':h:.') + local curpkg = vfn.fnamemodify(vfn.expand('%'), ':h:.') local pkgs = {} for _, p in ipairs(l) do - local d = vim.fn.fnamemodify(p.Dir, ':.') + local d = vfn.fnamemodify(p.Dir, ':.') if curpkg ~= d then - if d ~= vim.fn.getcwd() then + if d ~= vfn.getcwd() then table.insert(pkgs, util.relative_to_cwd(d)) end else diff --git a/lua/go/project.lua b/lua/go/project.lua index 0fbff53..587b9c9 100644 --- a/lua/go/project.lua +++ b/lua/go/project.lua @@ -11,7 +11,7 @@ return { test_dir = "", gocoverage_sign_priority = 5, launch_json = nil, -- the launch.json file path, default to .vscode/launch.json - -- launch_json = vim.fn.getcwd() .. "/.vscode/launch.json", + -- launch_json = vfn.getcwd() .. "/.vscode/launch.json", build_tags = "", --- you can provide extra build tags for tests or debugger @@ -20,32 +20,32 @@ return { ]] -- if the file existed, load it into config - +local vfn = vim.fn local util = require("go.utils") local log = util.log local M = {} local sep = require("go.utils").sep() function M.project_existed() - local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd() + local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd() local gocfgfd = workfolder .. sep .. ".gonvim" local gocfgbrks = gocfgfd .. sep .. "breakpoints.lua" local gocfg = gocfgfd .. sep .. "init.lua" - if vim.fn.filereadable(gocfg) == 1 or vim.fn.filereadable(gocfgbrks) == 1 then + if vfn.filereadable(gocfg) == 1 or vfn.filereadable(gocfgbrks) == 1 then log("projects existed", gocfg, gocfgbrks) return gocfg, gocfgbrks end end function M.setup() - local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd() + local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd() local gocfgfd = workfolder .. sep .. ".gonvim" local gocfg = gocfgfd .. sep .. "init.lua" - if vim.fn.isdirectory(gocfgfd) == 0 then - vim.fn.mkdir(gocfgfd) + if vfn.isdirectory(gocfgfd) == 0 then + vfn.mkdir(gocfgfd) end - if vim.fn.filereadable(gocfg) == 0 then + if vfn.filereadable(gocfg) == 0 then local f = io.open(gocfg, "w") f:write("return {}") f:close() @@ -54,9 +54,9 @@ function M.setup() end function M.load_project() - local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vim.fn.getcwd() + local workfolder = vim.lsp.buf.list_workspace_folders()[1] or vfn.getcwd() local gocfg = workfolder .. sep .. ".gonvim" .. sep .. "init.lua" - if vim.fn.filereadable(gocfg) == 1 then + if vfn.filereadable(gocfg) == 1 then local f = assert(loadfile(gocfg)) log(f()) _GO_NVIM_CFG = vim.tbl_deep_extend("force", _GO_NVIM_CFG, f()) diff --git a/lua/go/reftool.lua b/lua/go/reftool.lua index 8d1c28f..5c420ba 100644 --- a/lua/go/reftool.lua +++ b/lua/go/reftool.lua @@ -2,10 +2,10 @@ local reftool = {} local utils = require("go.utils") local log = utils.log -local fn = vim.fn +local vfn = vim.fn local function insert_result(result) - local curpos = fn.getcurpos() + local curpos = vfn.getcurpos() local goto_l = string.format("goto %d", result["start"] + 1) vim.cmd(goto_l) local inserts = result.code @@ -17,14 +17,14 @@ local function insert_result(result) local curline = curpos[2] for i = 2, #inserts do log("append ", curline, inserts[i]) - vim.fn.append(curline, inserts[i]) + vfn.append(curline, inserts[i]) curline = curline + 1 end vim.cmd("stopinsert!") vim.cmd("write") -- format(#inserts, curpos) - fn.setpos(".", curpos) + vfn.setpos(".", curpos) vim.defer_fn(function() vim.lsp.buf.format({ async = _GO_NVIM_CFG.lsp_fmt_async }) end, 300) @@ -39,21 +39,21 @@ local function fill(cmd) require("go.install").install(cmd) log(cmd) - local file = fn.expand("%:p") - local line = fn.line(".") - local run = string.format("%s -file=%s -line=%d 2>/dev/null", cmd, file, line) + local file = vfn.expand("%:p") + local line = vfn.line(".") + -- local run = string.format("%s -file=%s -line=%d 2>/dev/null", cmd, file, line) local farg = string.format("-file=%s", file) local larg = string.format("-line=%d", line) local args = { cmd, farg, larg, "2>/dev/null" } log(args) - vim.fn.jobstart(args, { - on_stdout = function(jobid, str, event) + vfn.jobstart(args, { + on_stdout = function(_, str, _) log(str) if #str < 2 then log("reftools", cmd, "finished with no result") return end - local json = fn.json_decode(str) + local json = vfn.json_decode(str) if #json == 0 then vim.notify("reftools " .. cmd .. " finished with no result", vim.lsp.log_levels.DEBUG) end diff --git a/lua/go/rename.lua b/lua/go/rename.lua index 439e50c..553c636 100644 --- a/lua/go/rename.lua +++ b/lua/go/rename.lua @@ -2,6 +2,7 @@ local utils = require("go.utils") local gorename = "gorename" +local vfn = vim.fn local lsprename = function() @@ -20,39 +21,31 @@ end local run = function(to_identifier, ...) require("go.install").install(gorename) - local fname = vim.fn.expand("%:p") -- %:p:h ? %:p + local fname = vfn.expand("%:p") -- %:p:h ? %:p - local old_identifier = vim.fn.expand("") + local old_identifier = vfn.expand("") - local prompt = vim.fn.printf("GoRename '%s' to (may take a while) :", old_identifier) - to_identifier = to_identifier or vim.fn.input(prompt, old_identifier) - local byte_offset = vim.fn.wordcount().cursor_bytes + local prompt = vfn.printf("GoRename '%s' to (may take a while) :", old_identifier) + to_identifier = to_identifier or vfn.input(prompt, old_identifier) + local byte_offset = vfn.wordcount().cursor_bytes local clients = vim.lsp.get_active_clients() or {} if #clients > 0 then -- TODO check gopls? - return lsprename(to_identifier) + return lsprename() end local offset = string.format("%s:#%i", fname, byte_offset) local setup = { gorename, "-offset", offset, "-to", to_identifier } - -- vim.notify("setup: " .. vim.inspect(setup), vim.lsp.log_levels.DEBUG) - -- - -- local arg = {...} - -- for i, v in ipairs(arg) do - -- table.insert(setup, v) - -- end - -- - -- vim.notify(vim.inspect(setup), vim.lsp.log_levels.DEBUG) - local j = vim.fn.jobstart(setup, { - on_stdout = function(jobid, data, event) + vfn.jobstart(setup, { + on_stdout = function(_, data, _) data = utils.handle_job_data(data) if not data then return end - -- local result = vim.fn.json_decode(data) + -- local result = vfn.json_decode(data) local result = vim.json.decode(data) if result.errors ~= nil or result.lines == nil or result["start"] == nil or result["start"] == 0 then vim.notify("failed to rename" .. vim.inspect(result), vim.lsp.log_levels.ERROR) diff --git a/lua/go/runner.lua b/lua/go/runner.lua index 817b4ab..3492798 100644 --- a/lua/go/runner.lua +++ b/lua/go/runner.lua @@ -1,21 +1,9 @@ local uv, api = vim.loop, vim.api local util = require("go.utils") local log = require("go.utils").log -local check_same = function(tbl1, tbl2) - if #tbl1 ~= #tbl2 then - return - end - for k, v in ipairs(tbl1) do - if v ~= tbl2[k] then - return true - end - end - return false -end local run = function(cmd, opts) opts = opts or {} - local shell_cmd log(cmd) if type(cmd) == "string" then local split_pattern = "%s+" @@ -51,6 +39,9 @@ local run = function(cmd, opts) local output_buf = "" local function update_chunk(err, chunk) + if err then + vim.notify("error " .. err, vim.lsp.log_levels.INFO) + end if chunk then output_buf = output_buf .. chunk local lines = vim.split(output_buf, "\n", true) @@ -66,7 +57,7 @@ local run = function(cmd, opts) update_chunk = vim.schedule_wrap(update_chunk) log("job:", cmd, job_options) - handle, pid = uv.spawn( + handle, _ = uv.spawn( cmd, { stdio = { stdin, stdout, stderr }, args = job_options.args }, function(code, signal) -- on exit @@ -111,7 +102,7 @@ local function make(...) local args = { ... } local setup = {} if #args > 0 then - for i, v in ipairs(args) do + for _, v in ipairs(args) do table.insert(setup, v) end end diff --git a/lua/go/tags.lua b/lua/go/tags.lua index e442851..26f6946 100644 --- a/lua/go/tags.lua +++ b/lua/go/tags.lua @@ -9,15 +9,6 @@ local tags = {} -- gomodifytags -file demo.go -struct Server -add-tags json,xml -transform camelcase -- gomodifytags -file demo.go -line 8,11 -clear-tags xml -local opts = { - "-add-tags", - "-add-options", - "-remove-tags", - "-remove-options", - "-clear-tags", - "-clear-options", -} - local gomodify = "gomodifytags" local transform = _GO_NVIM_CFG.tag_transform tags.modify = function(...) @@ -31,7 +22,6 @@ tags.modify = function(...) -- vim.notify("parnode" .. vim.inspect(ns), vim.lsp.log_levels.DEBUG) local struct_name = ns.name - local rs, re = ns.dim.s.r, ns.dim.e.r local setup = { gomodify, "-format", "json", "-file", fname, "-w" } if struct_name == nil then @@ -47,7 +37,7 @@ tags.modify = function(...) table.insert(setup, transform) end local arg = { ... } - for i, v in ipairs(arg) do + for _, v in ipairs(arg) do table.insert(setup, v) end @@ -55,8 +45,8 @@ tags.modify = function(...) table.insert(setup, "json") end -- vim.notify(vim.inspect(setup), vim.lsp.log_levels.DEBUG) - local j = vim.fn.jobstart(setup, { - on_stdout = function(jobid, data, event) + vim.fn.jobstart(setup, { + on_stdout = function(_, data, _) data = utils.handle_job_data(data) if not data then return diff --git a/lua/go/utils.lua b/lua/go/utils.lua index a3ba506..085c3ed 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -4,7 +4,7 @@ local fn = vim.fn local os_name = vim.loop.os_uname().sysname local is_windows = os_name == "Windows" or os_name == "Windows_NT" -- Check whether current buffer contains main function -local function has_main() +function util.has_main() local output = vim.api.nvim_exec("grep func\\ main\\(\\) %", true) local matchCount = vim.split(output, "\n") @@ -99,7 +99,7 @@ function util.interface_list(pkg) util.log(p) local ifaces = {} if p then - contents = p -- vim.split(p[1], "\n") + local contents = p -- vim.split(p[1], "\n") for _, content in pairs(contents) do util.log(content) if content:find("interface") then @@ -114,10 +114,9 @@ function util.interface_list(pkg) return ifaces end -local function smartrun() +function util.smartrun() local cmd - if has_main() then - -- Found main function in current buffer + if util.has_main() then cmd = string.format("lcd %:p:h | :set makeprg=%s\\ run\\ . | :make | :lcd -", _GO_NVIM_CFG.go) vim.cmd(cmd) else @@ -126,10 +125,9 @@ local function smartrun() end end -local function smartbuild() +function util.smartbuild() local cmd - if has_main() then - -- Found main function in current buffer + if util.has_main() then cmd = string.format("lcd %:p:h | :set makeprg=%s\\ build\\ . | :make | :lcd -", _GO_NVIM_CFG.go) vim.cmd(cmd) else @@ -194,7 +192,7 @@ util.handle_job_data = function(data) return nil end -- Because the nvim.stdout's data will have an extra empty line at end on some OS (e.g. maxOS), we should remove it. - for i = 1, 3, 1 do + for _ = 1, 3, 1 do if data[#data] == "" then table.remove(data, #data) end @@ -338,7 +336,8 @@ function util.load_plugin(name, modulename) if has then return plugin end - if packer_plugins ~= nil then + local pkg = packer_plugins or nil + if pkg ~= nil then -- packer installed local has_packer = pcall(require, "packer") if not has_packer then @@ -346,10 +345,10 @@ function util.load_plugin(name, modulename) return nil end local loader = require("packer").loader - if not packer_plugins[name] or not packer_plugins[name].loaded then + if not pkg[name] or not pkg[name].loaded then util.log("packer loader " .. name) vim.cmd("packadd " .. name) -- load with default - if packer_plugins[name] ~= nil then + if pkg[name] ~= nil then loader(name) end end @@ -397,7 +396,7 @@ end -- end function util.relative_to_cwd(name) - local rel = fn.isdirectory(name) == 0 and vim.fn.fnamemodify(name, ":h:.") or vim.fn.fnamemodify(name, ":.") + local rel = fn.isdirectory(name) == 0 and fn.fnamemodify(name, ":h:.") or fn.fnamemodify(name, ":.") if rel == "." then return "." else @@ -434,7 +433,7 @@ function util.rel_path(folder) if workfolders ~= nil and next(workfolders) then fpath = "." .. fpath:sub(#workfolders[1] + 1) end - return "." .. util.sep() .. fn.fnamemodify(vim.fn.expand(mod), ":.") + return "." .. util.sep() .. fn.fnamemodify(fn.expand(mod), ":.") end function util.rtrim(s) @@ -449,16 +448,6 @@ function util.ltrim(s) return (s:gsub("^%s*", "")) end -function util.file_exists(name) - local f = io.open(name, "r") - if f ~= nil then - io.close(f) - return true - else - return false - end -end - function util.work_path() local fpath = fn.expand("%:p:h") local workfolders = vim.lsp.buf.list_workspace_folders() @@ -528,12 +517,13 @@ util.deletedir = function(dir) util.log("remove dir", dir) end -function util.file_exists(file) - local f = io.open(file, "rb") - if f then - f:close() +function util.file_exists(name) + local f = io.open(name, "r") + if f ~= nil then + io.close(f) + return true end - return f ~= nil + return false end -- get all lines from a file, returns an empty @@ -550,7 +540,7 @@ function util.lines_from(file) end function util.list_directory() - local dirs = 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()