better workfolder

pull/59/head
ray-x 3 years ago
parent 47866b4847
commit c7c3ab885a

@ -35,7 +35,7 @@ function M.make(...)
efm = efm .. [[,%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:%c:\ %m]]
efm = efm .. [[,%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:\ %m]]
local pwd = vim.lsp.buf.list_workspace_folders()[1]
local pwd = util.work_path()
local cfg = pwd .. ".golangci.yml"
if util.file_exists(cfg) then

@ -52,7 +52,6 @@ M.test_fun = function(...)
log(args)
local fpath = vim.fn.expand("%:p:h")
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row, col + 1
@ -96,7 +95,7 @@ M.test_file = function(...)
log(fpath, fname)
local workfolder = vim.lsp.buf.list_workspace_folders()[1]
local workfolder = utils.work_path()
fname = "." .. fname:sub(#workfolder + 1)
log(workfolder, fname)

@ -32,33 +32,25 @@ M.efm = function()
end
local function get_build_tags(args)
local tags = "-tags"
-- local tags = "-tags"
local tags = {}
local space = [[\ ]]
if _GO_NVIM_CFG.run_in_floaterm then
space = " "
end
if _GO_NVIM_CFG.build_tags ~= "" then
tags = tags .. space .. _GO_NVIM_CFG.build_tags
tags = { "-tags=" .. _GO_NVIM_CFG.build_tags }
end
for i, value in pairs(args) do
if value:find("-tags") then
if tags == "-tags" then
tags = value
else
tags = tags .. "," .. value:sub(#"-tags=" + 1)
end
table.insert(tags, value)
table.remove(args, i)
break
end
end
if tags == "-tags" then
tags = ""
return tags, args
else
return space .. tags .. space, args
end
return tags, args
end
M.get_build_tags = get_build_tags
@ -72,52 +64,50 @@ local function run_test(path, args)
end
local tags, args2 = get_build_tags(args)
log(tags, args2)
if tags ~= "" then
tags = tags .. [[\ ]]
local cmd
if _GO_NVIM_CFG.run_in_floaterm then
cmd = { test_runner, "test", "-v" }
else
cmd = { "-v" }
end
if tags ~= {} then
cmd = vim.list_extend(cmd, tags)
end
if args2 ~= nil and args2 ~= {} then
cmd = vim.list_extend(cmd, args2)
else
local argsstr = "." .. utils.sep() .. "..."
cmd = vim.list_extend(cmd, argsstr)
end
local argsstr = ""
if path ~= "" then
table.insert(cmd, path)
end
utils.log(cmd)
if _GO_NVIM_CFG.run_in_floaterm then
argsstr = table.concat(args2 or {}, " ")
if argsstr == "" then
argsstr = "." .. utils.sep() .. "..."
end
local cmd = test_runner .. [[ test ]] .. tags .. [[ -v ]] .. argsstr
cmd = cmd .. tags .. [[ -v ]] .. argsstr
if path ~= "" then
cmd = cmd .. [[ ]] .. path
end
utils.log(cmd)
local term = require("go.term").run
term({ cmd = cmd, autoclose = false })
return
end
argsstr = table.concat(args2 or {}, [[\ ]])
if argsstr == "" then
argsstr = "." .. utils.sep() .. "..." .. [[\ ]]
end
path = argsstr or path
vim.cmd([[setl makeprg=go\ test]])
local cmd = [[setl makeprg=go\ test\ ]] .. tags .. path .. [[ | lua require"go.asyncmake".make()]]
utils.log("test cmd", cmd)
vim.cmd(cmd)
require("go.asyncmake").make(unpack(cmd))
end
M.test = function(...)
local args = { ... }
log(args)
local workfolder = vim.lsp.buf.list_workspace_folders()[1]
local workfolder = utils.work_path()
if workfolder == nil then
workfolder = "."
end
local fpath = workfolder .. utils.sep() .. "..."
-- local fpath = workfolder .. utils.sep() .. '...'
-- local fpath = './' .. vim.fn.expand('%:h') .. '/...'
utils.log("fpath" .. fpath)
utils.log("fpath :" .. fpath)
run_test(fpath, args)
end
@ -126,13 +116,10 @@ M.test_suit = function(...)
local args = { ... }
log(args)
local workfolder = vim.lsp.buf.list_workspace_folders()[1]
local workfolder = utils.work_path()
utils.log(args)
local fpath = workfolder .. utils.sep() .. "..."
fpath = fpath:gsub(" ", [[\ ]])
fpath = fpath:gsub("-", [[\-]])
-- local fpath = './' .. vim.fn.expand('%:h') .. '/...'
utils.log("fpath" .. fpath)
run_test(fpath, args)
@ -146,24 +133,20 @@ M.test_package = function(...)
local fpath = repath .. utils.sep() .. "..."
fpath = fpath:gsub(" ", [[\ ]])
fpath = fpath:gsub("-", [[\-]])
utils.log("fpath" .. fpath)
utils.log("fpath: " .. fpath)
args[#args + 1] = fpath
-- args[#args + 1] = fpath
run_test(fpath, args)
end
M.test_fun = function(...)
local args = { ... }
log(args)
-- for i, v in ipairs(args) do
-- table.insert(setup, v)
-- end
local fpath = vim.fn.expand("%:p:h")
fpath = fpath:gsub(" ", [[\ ]])
fpath = fpath:gsub("-", [[\-]])
-- fpath = fpath:gsub(" ", [[\ ]])
-- fpath = fpath:gsub("-", [[\-]])
-- log(fpath)
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
row, col = row, col + 1
local ns = require("go.ts.go").get_func_method_node_at_pos(row, col)
@ -176,70 +159,53 @@ M.test_fun = function(...)
utils.log("parnode" .. vim.inspect(ns))
local test_runner = "go"
if _GO_NVIM_CFG.test_runner ~= "go" then
require("go.install").install(test_runner)
test_runner = _GO_NVIM_CFG.test_runner
if test_runner == "ginkgo" then
ginkgo.test_fun(...)
end
require("go.install").install(test_runner)
end
local bench = ""
if ns.name:find("Bench") then
bench = "-bench=" .. ns.name
end
local cmd
if _GO_NVIM_CFG.run_in_floaterm then
argsstr = table.concat(args2 or {}, " ")
local cmd = test_runner .. [[ test ]] -- .. tags .. [[-v -run ^]] .. ns.name .. [[ ]] .. argstr .. [[ ]] .. fpath
if tags ~= "" then
cmd = cmd .. tags
end
cmd = { test_runner, "test", "-v" }
else
cmd = { "-v" }
end
if tags ~= {} then
cmd = vim.list_extend(cmd, tags)
end
if args2 ~= nil and args2 ~= {} then
cmd = vim.list_extend(cmd, args2)
else
argsstr = "." .. utils.sep() .. "..."
cmd = vim.list_extend(cmd, argsstr)
end
cmd = cmd .. [[-v -run ^]] .. ns.name
if ns.name:find("Bench") then
local bench = "-bench=" .. ns.name
table.insert(cmd, bench)
end
if bench ~= "" then
cmd = cmd .. [[ ]] .. bench
end
if argsstr ~= "" then
cmd = cmd .. [[ ]] .. argsstr
end
table.insert(cmd, "-run")
table.insert(cmd, [[^]] .. ns.name)
cmd = cmd .. [[ ]] .. fpath
table.insert(cmd, fpath)
if _GO_NVIM_CFG.run_in_floaterm then
utils.log(cmd)
local term = require("go.term").run
term({ cmd = cmd, autoclose = false })
return
end
argsstr = table.concat(args2 or {}, [[\ ]])
-- local cmd =
-- [[setl makeprg=]] .. test_runner .. [[\ test\ ]] .. tags .. [[-v\ -run\ ^]] .. ns.name .. [[\ ]] .. argsstr
-- .. [[\ ]] .. fpath .. [[ | lua require"go.asyncmake".make()]]
--
local cmd = [[setl makeprg=]] .. test_runner .. [[\ test\ ]]
if tags ~= "" then
cmd = cmd .. tags
end
cmd = cmd .. [[-v\ -run\ ^]] .. ns.name
if bench ~= "" then
cmd = cmd .. [[\ ]] .. bench
end
if argsstr ~= "" then
cmd = cmd .. [[\ ]] .. argsstr
end
vim.cmd([[setl makeprg=]] .. test_runner .. [[\ test]])
-- set_efm()
cmd = cmd .. [[\ ]] .. fpath .. [[ | lua require"go.asyncmake".make()]]
utils.log("test cmd", cmd)
vim.cmd(cmd)
require("go.asyncmake").make(unpack(cmd))
return true
end
@ -249,57 +215,59 @@ M.test_file = function(...)
-- require sed
-- local testcases = [[sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']]
utils.log(args)
local cmd = [[cat ]] .. vim.fn.expand("%:p") .. [[| sed -n 's/func.*\(Test.*\)(.*/\1/p' | xargs | sed 's/ /\\\|/g']]
local fpath = vim.fn.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
local tests = vim.fn.systemlist(cmd)[1]
-- local fpath = './' .. vim.fn.expand('%:h') .. '/...'
utils.log(tests)
utils.log(cmd, tests)
if tests == nil or tests == {} then
print("no test found fallback to package test")
M.test_package(...)
M.test_package(args)
return
end
local tags, args2 = get_build_tags(args)
local argsstr = ""
local test_runner = "go"
if _GO_NVIM_CFG.test_runner ~= "go" then
test_runner = _GO_NVIM_CFG.test_runner
require("go.install").install(test_runner)
if test_runner == "ginkgo" then
ginkgo.test_fun(...)
end
require("go.install").install(test_runner)
end
local relpath = utils.rel_path()
relpath = relpath:gsub(" ", [[\ ]])
relpath = relpath:gsub("-", [[\-]])
local cmd_args
if _GO_NVIM_CFG.run_in_floaterm then
cmd_args = { test_runner, "test", "-v" }
else
cmd_args = { "-v" }
end
if tags ~= nil and #tags > 1 then
cmd_args = vim.list_extend(cmd_args, tags)
end
if args2 then
cmd_args = vim.list_extend(cmd_args, args2)
end
table.insert(cmd_args, "-run")
table.insert(cmd_args, tests)
table.insert(cmd_args, relpath)
if _GO_NVIM_CFG.run_in_floaterm then
argsstr = table.concat(args2 or {} or {}, " ")
cmd = test_runner .. [[ test ]] .. tags .. [[-v -run ]] .. tests .. [[ ]] .. argsstr .. [[ ]] .. relpath
utils.log(cmd)
local term = require("go.term").run
term({ cmd = cmd, autoclose = false })
term({ cmd = cmd_args, autoclose = false })
return
end
argsstr = table.concat(args2 or {}, [[\ ]])
cmd = [[setl makeprg=go\ test\ ]]
.. tags
.. [[-v\ -run\ ]]
.. tests
.. [[\ ]]
.. argsstr
.. [[\ ]]
.. relpath
.. [[| lua require"go.asyncmake".make()]]
vim.cmd([[setl makeprg=go\ test]])
require("go.asyncmake").make(unpack(cmd_args))
utils.log("test cmd", cmd)
vim.cmd(cmd)
end
return M

@ -1,7 +1,7 @@
local util = {}
local os_name = vim.loop.os_uname().sysname
local is_windows = os_name == 'Windows' or os_name == 'Windows_NT'
local is_windows = os_name == "Windows" or os_name == "Windows_NT"
-- Check whether current buffer contains main function
local function has_main()
local output = vim.api.nvim_exec("grep func\\ main\\(\\) %", true)
@ -12,9 +12,9 @@ end
function util.sep()
if is_windows then
return '\\'
return "\\"
end
return '/'
return "/"
end
local function smartrun()
@ -48,12 +48,12 @@ util.check_same = function(tbl1, tbl2)
end
util.map = function(modes, key, result, options)
options = util.merge({noremap = true, silent = false, expr = false, nowait = false}, options or {})
options = util.merge({ noremap = true, silent = false, expr = false, nowait = false }, options or {})
local buffer = options.buffer
options.buffer = nil
if type(modes) ~= "table" then
modes = {modes}
modes = { modes }
end
for i = 1, #modes do
@ -74,7 +74,7 @@ end
util.deepcopy = function(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
if orig_type == "table" then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[util.deepcopy(orig_key)] = util.deepcopy(orig_value)
@ -91,7 +91,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.
if data[#data] == '' then
if data[#data] == "" then
table.remove(data, #data)
end
if #data < 1 then
@ -104,8 +104,13 @@ util.log = function(...)
if not _GO_NVIM_CFG.verbose then
return
end
local arg = {...}
local log_default = string.format("%s%s%s.log", vim.api.nvim_call_function("stdpath", {"data"}), util.sep(), "gonvim")
local arg = { ... }
local log_default = string.format(
"%s%s%s.log",
vim.api.nvim_call_function("stdpath", { "data" }),
util.sep(),
"gonvim"
)
local log_path = _GO_NVIM_CFG.log_path or log_default
local str = ""
@ -127,7 +132,7 @@ util.log = function(...)
return
end
if not f then
error('open file ' .. log_path, f)
error("open file " .. log_path, f)
end
io.output(f)
io.write(str .. "\n")
@ -142,8 +147,8 @@ local rhs_options = {}
function rhs_options:new()
local instance = {
cmd = '',
options = {noremap = false, silent = false, expr = false, nowait = false}
cmd = "",
options = { noremap = false, silent = false, expr = false, nowait = false },
}
setmetatable(instance, self)
self.__index = self
@ -213,7 +218,7 @@ end
function util.nvim_load_mapping(mapping)
for key, value in pairs(mapping) do
local mode, keymap = key:match("([^|]*)|?(.*)")
if type(value) == 'table' then
if type(value) == "table" then
local rhs = value.cmd
local options = value.options
vim.api.nvim_set_keymap(mode, keymap, rhs, options)
@ -230,7 +235,7 @@ function util.load_plugin(name, modulename)
end
if packer_plugins ~= nil then
-- packer installed
local loader = require"packer".loader
local loader = require("packer").loader
if not packer_plugins[name] or not packer_plugins[name].loaded then
loader(name)
end
@ -271,34 +276,35 @@ function util.check_capabilities(feature, client_id)
end
function util.relative_to_cwd(name)
local rel = vim.fn.isdirectory(name) == 0 and vim.fn.fnamemodify(name, ':h:.') or vim.fn.fnamemodify(name, ':.')
if rel == '.' then
return '.'
local rel = vim.fn.isdirectory(name) == 0 and vim.fn.fnamemodify(name, ":h:.") or vim.fn.fnamemodify(name, ":.")
if rel == "." then
return "."
else
return '.' .. util.sep() .. rel
return "." .. util.sep() .. rel
end
end
function util.all_pkgs()
return '.' .. util.sep() .. '...'
return "." .. util.sep() .. "..."
end
-- log and messages
function util.warn(msg)
vim.api.nvim_echo({{"WRN: " .. msg, "WarningMsg"}}, true, {})
vim.api.nvim_echo({ { "WRN: " .. msg, "WarningMsg" } }, true, {})
end
function util.error(msg)
vim.api.nvim_echo({{"ERR: " .. msg, "ErrorMsg"}}, true, {})
vim.api.nvim_echo({ { "ERR: " .. msg, "ErrorMsg" } }, true, {})
end
function util.info(msg)
vim.api.nvim_echo({{"Info: " .. msg}}, true, {})
vim.api.nvim_echo({ { "Info: " .. msg } }, true, {})
end
function util.rel_path()
local fpath = vim.fn.expand('%:p:h')
local workfolder = vim.lsp.buf.list_workspace_folders()[1]
local fpath = vim.fn.expand("%:p:h")
local workfolders = vim.lsp.buf.list_workspace_folders()
if workfolder ~= nil then
fpath = "." .. fpath:sub(#workfolder + 1)
end
@ -314,8 +320,30 @@ function util.rtrim(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
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 = vim.fn.expand("%:p:h")
local workfolders = vim.lsp.buf.list_workspace_folders()
if #workfolders == 1 then
return workfolders[1]
end
for _, value in pairs(workfolders) do
local mod = value .. util.sep() .. "go.mod"
if util.file_exist(mod) then
return value
end
end
return workfolders[1] or fpath
end
return util

Loading…
Cancel
Save