2021-03-10 12:15:06 +00:00
|
|
|
-- golines A golang formatter that fixes long lines
|
2021-09-25 02:44:52 +00:00
|
|
|
-- golines + goimport
|
2021-03-10 12:15:06 +00:00
|
|
|
local api = vim.api
|
2021-07-07 05:56:22 +00:00
|
|
|
local utils = require("go.utils")
|
2021-08-24 04:53:37 +00:00
|
|
|
local log = utils.log
|
2021-08-14 00:27:55 +00:00
|
|
|
local max_len = _GO_NVIM_CFG.max_line_len or 120
|
2021-09-02 12:27:40 +00:00
|
|
|
local goimport = _GO_NVIM_CFG.goimport or "goimports"
|
2021-09-02 10:34:57 +00:00
|
|
|
local gofmt = _GO_NVIM_CFG.gofmt or "gofumpt"
|
2021-12-02 06:44:22 +00:00
|
|
|
local gofmt_args = _GO_NVIM_CFG.gofmt_args or {
|
|
|
|
"--max-len=" .. tostring(max_len), "--base-formatter=" .. gofmt
|
|
|
|
}
|
2021-03-10 12:15:06 +00:00
|
|
|
|
2021-12-02 06:44:22 +00:00
|
|
|
local goimport_args = _GO_NVIM_CFG.goimport_args or {
|
|
|
|
"--max-len=" .. tostring(max_len), "--base-formatter=" .. goimport
|
|
|
|
}
|
2021-03-10 12:15:06 +00:00
|
|
|
|
2021-09-02 16:15:54 +00:00
|
|
|
local run = function(fmtargs, from_buffer, cmd)
|
|
|
|
local args = vim.deepcopy(fmtargs)
|
2021-03-10 12:15:06 +00:00
|
|
|
if not from_buffer then
|
|
|
|
table.insert(args, api.nvim_buf_get_name(0))
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify('formatting buffer... ' .. api.nvim_buf_get_name(0) .. vim.inspect(args), vim.lsp.log_levels.DEBUG)
|
2021-09-02 16:15:54 +00:00
|
|
|
else
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify('formatting... ' .. vim.inspect(args), vim.lsp.log_levels.DEBUG)
|
2021-03-10 12:15:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local old_lines = api.nvim_buf_get_lines(0, 0, -1, true)
|
2021-09-01 01:22:05 +00:00
|
|
|
if cmd then
|
|
|
|
table.insert(args, 1, cmd)
|
|
|
|
else
|
|
|
|
table.insert(args, 1, "golines")
|
|
|
|
end
|
2021-09-02 17:14:04 +00:00
|
|
|
log("fmt cmd:", args)
|
2021-03-10 12:15:06 +00:00
|
|
|
|
2021-07-11 11:33:22 +00:00
|
|
|
local j = vim.fn.jobstart(args, {
|
|
|
|
on_stdout = function(job_id, data, event)
|
|
|
|
data = utils.handle_job_data(data)
|
|
|
|
if not data then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if not utils.check_same(old_lines, data) then
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify("updating codes", vim.lsp.log_levels.DEBUG)
|
2021-07-11 11:33:22 +00:00
|
|
|
api.nvim_buf_set_lines(0, 0, -1, false, data)
|
|
|
|
api.nvim_command("write")
|
|
|
|
else
|
2021-12-22 03:34:10 +00:00
|
|
|
vim.notify("already formatted", vim.lsp.log_levels.DEBUG)
|
2021-07-11 11:33:22 +00:00
|
|
|
end
|
2021-08-24 04:53:37 +00:00
|
|
|
-- log("stdout" .. vim.inspect(data))
|
2021-07-11 11:33:22 +00:00
|
|
|
old_lines = nil
|
2021-03-10 12:15:06 +00:00
|
|
|
|
2021-07-11 11:33:22 +00:00
|
|
|
end,
|
|
|
|
on_stderr = function(job_id, data, event)
|
2021-12-27 08:47:59 +00:00
|
|
|
data = utils.handle_job_data(data)
|
2021-12-02 06:44:22 +00:00
|
|
|
log(vim.inspect(data) .. "from stderr")
|
2021-07-11 11:33:22 +00:00
|
|
|
end,
|
|
|
|
on_exit = function(id, data, event)
|
2021-08-24 07:10:04 +00:00
|
|
|
-- log(vim.inspect(data) .. "exit")
|
2021-08-24 04:53:37 +00:00
|
|
|
-- log("current data " .. vim.inspect(new_lines))
|
2021-07-11 11:33:22 +00:00
|
|
|
old_lines = nil
|
|
|
|
end,
|
|
|
|
stdout_buffered = true,
|
|
|
|
stderr_buffered = true
|
|
|
|
})
|
2021-03-10 12:15:06 +00:00
|
|
|
vim.fn.chansend(j, old_lines)
|
|
|
|
vim.fn.chanclose(j, "stdin")
|
|
|
|
end
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
M.gofmt = function(buf)
|
2021-07-11 11:52:39 +00:00
|
|
|
if _GO_NVIM_CFG.gofmt == 'gopls' then
|
2021-08-24 04:53:37 +00:00
|
|
|
-- log("gopls format")
|
2021-07-11 11:33:22 +00:00
|
|
|
vim.lsp.buf.formatting()
|
|
|
|
return
|
|
|
|
end
|
2021-04-21 11:11:44 +00:00
|
|
|
vim.env.GO_FMT = "gofumpt"
|
2021-03-10 12:15:06 +00:00
|
|
|
buf = buf or false
|
|
|
|
require("go.install").install(gofmt)
|
|
|
|
require("go.install").install("golines")
|
|
|
|
local a = {}
|
2021-07-07 05:56:22 +00:00
|
|
|
utils.copy_array(gofmt_args, a)
|
2021-03-10 12:15:06 +00:00
|
|
|
run(a, buf)
|
|
|
|
end
|
|
|
|
|
2021-08-16 22:56:17 +00:00
|
|
|
M.org_imports = function(wait_ms)
|
2021-09-25 02:44:52 +00:00
|
|
|
local codeaction = require('go.lsp').codeaction
|
2021-09-20 09:05:04 +00:00
|
|
|
codeaction('', 'source.organizeImports', wait_ms)
|
2021-10-01 08:49:58 +00:00
|
|
|
vim.lsp.buf.formatting_sync(nil, wait_ms)
|
2021-07-11 11:33:22 +00:00
|
|
|
end
|
|
|
|
|
2021-09-02 16:59:45 +00:00
|
|
|
M.goimport = function(...)
|
2021-07-11 11:52:39 +00:00
|
|
|
if _GO_NVIM_CFG.goimport == 'gopls' then
|
2021-08-16 22:56:17 +00:00
|
|
|
M.org_imports(1000)
|
2021-07-11 11:33:22 +00:00
|
|
|
return
|
|
|
|
end
|
2021-09-02 16:59:45 +00:00
|
|
|
local args = {...}
|
|
|
|
local a1 = select(1, args)
|
|
|
|
local buf = true
|
2021-09-02 17:14:04 +00:00
|
|
|
if #args > 0 and type(args[1]) == "boolean" then
|
2021-09-02 16:59:45 +00:00
|
|
|
buf = a1
|
|
|
|
table.remove(args, 1)
|
|
|
|
end
|
2021-03-10 12:15:06 +00:00
|
|
|
require("go.install").install(goimport)
|
|
|
|
require("go.install").install("golines")
|
2021-09-02 16:59:45 +00:00
|
|
|
local a = vim.deepcopy(goimport_args)
|
|
|
|
if #args > 0 and _GO_NVIM_CFG.goimport == 'goimports' then -- dont use golines
|
|
|
|
return run(args, buf, 'goimports')
|
2021-09-01 01:22:05 +00:00
|
|
|
end
|
2021-09-02 16:59:45 +00:00
|
|
|
run(goimport_args, buf)
|
2021-03-10 12:15:06 +00:00
|
|
|
end
|
2021-08-16 22:56:17 +00:00
|
|
|
|
2021-03-10 12:15:06 +00:00
|
|
|
return M
|