merge github workflow and goimport fix

pull/32/head
ray-x 3 years ago
commit ee0d43ce5c

@ -4,19 +4,21 @@ local api = vim.api
local utils = require("go.utils") local utils = require("go.utils")
local log = utils.log local log = utils.log
local max_len = _GO_NVIM_CFG.max_line_len or 120 local max_len = _GO_NVIM_CFG.max_line_len or 120
local goimport = _GO_NVIM_CFG.goimport ~= nil and _GO_NVIM_CFG.goimport or "gofumports" local goimport = _GO_NVIM_CFG.goimport or "goimports"
local gofmt = _GO_NVIM_CFG.gofmt ~= nil and _GO_NVIM_CFG.gofmt or "gofumpt" local gofmt = _GO_NVIM_CFG.gofmt or "gofumpt"
local gofmt_args = _GO_NVIM_CFG.gofmt_args and _GO_NVIM_CFG.gofmt_args local gofmt_args = _GO_NVIM_CFG.gofmt_args
or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. gofmt} or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. gofmt}
local goimport_args = _GO_NVIM_CFG.goimport_args and _GO_NVIM_CFG.goimport_args local goimport_args = _GO_NVIM_CFG.goimport_args
or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. goimport} or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. goimport}
local run = function(args, from_buffer, cmd) local run = function(fmtargs, from_buffer, cmd)
local args = vim.deepcopy(fmtargs)
if not from_buffer then if not from_buffer then
table.insert(args, api.nvim_buf_get_name(0)) table.insert(args, api.nvim_buf_get_name(0))
print('formatting... ' .. api.nvim_buf_get_name(0) .. vim.inspect(args)) print('formatting buffer... ' .. api.nvim_buf_get_name(0) .. vim.inspect(args))
else
print('formatting... ' .. vim.inspect(args))
end end
local old_lines = api.nvim_buf_get_lines(0, 0, -1, true) local old_lines = api.nvim_buf_get_lines(0, 0, -1, true)
@ -25,7 +27,7 @@ local run = function(args, from_buffer, cmd)
else else
table.insert(args, 1, "golines") table.insert(args, 1, "golines")
end end
log(args) log("fmt cmd:", args)
local j = vim.fn.jobstart(args, { local j = vim.fn.jobstart(args, {
on_stdout = function(job_id, data, event) on_stdout = function(job_id, data, event)
@ -109,15 +111,19 @@ M.goimport = function(...)
return return
end end
local args = {...} local args = {...}
local a1 = select(1, args)
local buf = true
if #args > 0 and type(args[1]) == "boolean" then
buf = a1
table.remove(args, 1)
end
require("go.install").install(goimport) require("go.install").install(goimport)
require("go.install").install("golines") require("go.install").install("golines")
local a = vim.deepcopy(goimport_args)
if args and _GO_NVIM_CFG.goimport == 'goimports' then if #args > 0 and _GO_NVIM_CFG.goimport == 'goimports' then -- dont use golines
run(args, true, 'goimports') return run(args, buf, 'goimports')
else
utils.copy_array(goimport_args, a)
run({}, true)
end end
run(goimport_args, buf)
end end
return M return M

@ -120,6 +120,8 @@ function M.setup()
if _GO_NVIM_CFG.gopls_cmd then if _GO_NVIM_CFG.gopls_cmd then
gopls.cmd = _GO_NVIM_CFG.gopls_cmd gopls.cmd = _GO_NVIM_CFG.gopls_cmd
else
gopls.cmd = {'gopls'}
end end
if _GO_NVIM_CFG.lsp_gofumpt then if _GO_NVIM_CFG.lsp_gofumpt then

@ -2,6 +2,6 @@ package main
import "fmt" import "fmt"
func main() { func foo() {
fmt.Println("vim-go") fmt.Println("vim-go")
} }

@ -12,7 +12,11 @@ describe("should read coveragefile", function()
print("test:" .. path) print("test:" .. path)
-- go.nvim may not auto loaded -- go.nvim may not auto loaded
vim.cmd([[packadd go.nvim]]) vim.cmd([[packadd go.nvim]])
require('go').setup({trace = true, log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log"}) require('go').setup({
trace = true,
lsp_cfg = true,
log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log"
})
local cover = require("go.coverage") local cover = require("go.coverage")
local result = cover.read_cov(path) local result = cover.read_cov(path)
@ -33,6 +37,7 @@ describe("should read coveragefile", function()
vim.cmd([[packadd go.nvim]]) vim.cmd([[packadd go.nvim]])
require('go').setup({ require('go').setup({
trace = true, trace = true,
lsp_cfg = true,
log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log", log_path = vim.fn.expand("$HOME") .. "/tmp/gonvim.log",
gocoverage_sign = '|' gocoverage_sign = '|'
}) })
@ -69,110 +74,4 @@ describe("should read coveragefile", function()
eq(result[1], sign) eq(result[1], sign)
-- eq(result[n][1], "github.com/go.nvim/branch.go") -- eq(result[n][1], "github.com/go.nvim/branch.go")
end) end)
-- it("should run fmt sending from buffer", function()
-- local name = vim.fn.tempname() .. ".go"
-- print("tmp:" .. name)
-- --
-- local path = cur_dir .. "/lua/tests/fixtures/fmt/hello.go" -- %:p:h ? %:p
-- print("test:" .. path)
-- local lines = vim.fn.readfile(path)
-- vim.fn.writefile(lines, name)
-- local expected = vim.fn.join(vim.fn.readfile(
-- cur_dir .. "/lua/tests/fixtures/fmt/hello_golden.go"), "\n")
-- local cmd = " silent exe 'e " .. name .. "'"
-- vim.cmd(cmd)
-- local l = vim.api.nvim_buf_get_lines(0, 0, -1, true)
-- print("buf read: " .. vim.inspect(l))
--
-- vim.bo.filetype = "go"
--
-- print("exp:" .. vim.inspect(expected))
-- print("tmp" .. name)
--
-- local gofmt = require("go.format")
-- gofmt.gofmt(true)
-- -- enable the channel response
-- vim.wait(100, function()
-- end)
-- local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
-- print("fmt" .. fmt)
-- vim.fn.assert_equal(fmt, expected)
-- eq(expected, fmt)
-- local cmd = "bd! " .. name
-- vim.cmd(cmd)
-- end)
-- it("should run import from file", function()
-- local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p
-- local expected = vim.fn.join(vim.fn.readfile(cur_dir
-- .. "/lua/tests/fixtures/fmt/goimports_golden.go"),
-- "\n")
-- local name = vim.fn.tempname() .. ".go"
-- print(name)
-- local lines = vim.fn.readfile(path)
-- vim.fn.writefile(lines, name)
-- local cmd = " silent exe 'e " .. name .. "'"
-- vim.cmd(cmd)
--
-- vim.cmd([[cd %:p:h]])
-- require("go.format").goimport()
-- print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
-- vim.wait(100, function()
-- end)
-- local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
-- eq(expected, fmt)
-- cmd = "bd! " .. name
-- vim.cmd(cmd)
-- end)
-- it("should run import from file with gopls", function()
-- local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports2.go" -- %:p:h ? %:p
-- local expected = vim.fn.join(vim.fn.readfile(cur_dir
-- .. "/lua/tests/fixtures/fmt/goimports2_golden.go"),
-- "\n")
-- require("go").setup({goimport = "gopls", lsp_cfg = true})
--
-- _GO_NVIM_CFG.goimport = 'gopls'
--
-- local lines = vim.fn.readfile(path)
-- local cmd = " silent exe 'e " .. path .. "'"
-- vim.cmd(cmd)
-- vim.wait(1000, function()
-- end)
--
-- vim.cmd([[cd %:p:h]])
-- require("go.format").goimport()
--
-- print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
-- vim.wait(200, function()
-- end)
-- local fmt = vim.fn.join(vim.fn.readfile(path), "\n")
-- -- eq(expected, fmt)
-- eq(1, 1) -- still not working
-- cmd = "bd! " .. path
-- vim.cmd(cmd)
-- end)
-- it("should run import from file buffer with gofumpts", function()
-- _GO_NVIM_CFG.goimport = 'gofumports'
-- local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p
-- local expected = vim.fn.join(vim.fn.readfile(cur_dir
-- .. "/lua/tests/fixtures/fmt/goimports_golden.go"),
-- "\n")
-- local name = vim.fn.tempname() .. ".go"
-- print(name)
-- local lines = vim.fn.readfile(path)
-- local cmd = " silent exe 'e " .. name .. "'"
-- vim.fn.writefile(lines, name)
-- vim.cmd(cmd)
-- vim.cmd([[cd %:p:h]])
-- print("code write to " .. name)
-- local gofmt = require("go.format")
-- gofmt.goimport(true)
--
-- vim.wait(100, function()
-- end)
-- local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
--
-- print(fmt)
-- eq(expected, fmt)
-- end)
end) end)

@ -81,7 +81,7 @@ describe("should run gofmt", function()
vim.fn.writefile(lines, name) vim.fn.writefile(lines, name)
local cmd = " silent exe 'e " .. name .. "'" local cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd) vim.cmd(cmd)
require("go").setup({goimport = "goimports"})
vim.cmd([[cd %:p:h]]) vim.cmd([[cd %:p:h]])
require("go.format").goimport() require("go.format").goimport()
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders())) print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
@ -92,32 +92,33 @@ describe("should run gofmt", function()
cmd = "bd! " .. name cmd = "bd! " .. name
vim.cmd(cmd) vim.cmd(cmd)
end) end)
it("should run import from file buffer with gofumpts", function() it("should run import from file buffer with goimport", function()
_GO_NVIM_CFG.goimport = 'gofumports'
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports.go" -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir local expected = vim.fn.join(vim.fn.readfile(cur_dir
.. "/lua/tests/fixtures/fmt/goimports_golden.go"), .. "/lua/tests/fixtures/fmt/goimports_golden.go"),
"\n") "\n")
local name = vim.fn.tempname() .. ".go" local name = vim.fn.tempname() .. ".go"
print(name) print(name)
_GO_NVIM_CFG.goimport = 'goimports'
local lines = vim.fn.readfile(path) local lines = vim.fn.readfile(path)
local cmd = " silent exe 'e " .. name .. "'" local cmd = " silent exe 'e " .. name .. "'"
vim.fn.writefile(lines, name) vim.fn.writefile(lines, name)
vim.cmd(cmd) vim.cmd(cmd)
vim.cmd([[cd %:p:h]]) vim.cmd([[cd %:p:h]])
print("code write to " .. name) print("code write to " .. name)
require("go").setup({goimport = "goimports"})
local gofmt = require("go.format") local gofmt = require("go.format")
gofmt.goimport(true) gofmt.goimport(true)
vim.wait(100, function() vim.wait(100, function()
end) end)
vim.cmd([[w]])
local fmt = vim.fn.join(vim.fn.readfile(name), "\n") local fmt = vim.fn.join(vim.fn.readfile(name), "\n")
print('formated', fmt) print(fmt)
eq(expected, fmt) eq(expected, fmt)
end) end)
it("should run import from file with gopls", function() it("should run import from file with gopls", function()
local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports2.go" -- %:p:h ? %:p local path = cur_dir .. "/lua/tests/fixtures/fmt/goimports2.go" -- %:p:h ? %:p
local expected = vim.fn.join(vim.fn.readfile(cur_dir local expected = vim.fn.join(vim.fn.readfile(cur_dir
@ -126,13 +127,13 @@ describe("should run gofmt", function()
_GO_NVIM_CFG.goimport = 'gopls' _GO_NVIM_CFG.goimport = 'gopls'
local lines = vim.fn.readfile(path)
local cmd = " silent exe 'e " .. path .. "'" local cmd = " silent exe 'e " .. path .. "'"
vim.cmd(cmd) vim.cmd(cmd)
vim.cmd([[packadd go.nvim]])
vim.cmd([[cd %:p:h]]) vim.cmd([[cd %:p:h]])
vim.cmd([[packadd go.nvim]])
require("go").setup({goimport = "gopls", lsp_cfg = true}) require("go").setup({goimport = "gopls", lsp_cfg = true})
vim.wait(1000, function() vim.wait(2000, function()
end) end)
require("go.format").goimport() require("go.format").goimport()
@ -140,8 +141,10 @@ describe("should run gofmt", function()
print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders())) print("workspaces:", vim.inspect(vim.lsp.buf.list_workspace_folders()))
vim.wait(200, function() vim.wait(200, function()
end) end)
vim.cmd([[w]])
local fmt = vim.fn.join(vim.fn.readfile(path), "\n") local fmt = vim.fn.join(vim.fn.readfile(path), "\n")
-- eq(expected, fmt) print(vim.inspect(fmt))
eq(expected, fmt)
eq(1, 1) -- still not working eq(1, 1) -- still not working
cmd = "bd! " .. path cmd = "bd! " .. path
vim.cmd(cmd) vim.cmd(cmd)

Loading…
Cancel
Save