2021-03-10 12:15:06 +00:00
|
|
|
-- golines A golang formatter that fixes long lines
|
|
|
|
-- golines + gofumports(stricter gofmt + goimport)
|
|
|
|
local api = vim.api
|
2021-07-07 05:56:22 +00:00
|
|
|
local utils = require("go.utils")
|
2021-08-14 00:27:55 +00:00
|
|
|
local max_len = _GO_NVIM_CFG.max_line_len or 120
|
2021-07-10 11:04:24 +00:00
|
|
|
local goimport = _GO_NVIM_CFG.goimport ~= nil and _GO_NVIM_CFG.goimport or "gofumports"
|
|
|
|
local gofmt = _GO_NVIM_CFG.gofmt ~= nil and _GO_NVIM_CFG.gofmt or "gofumpt"
|
2021-07-11 11:33:22 +00:00
|
|
|
local gofmt_args = _GO_NVIM_CFG.gofmt_args and _GO_NVIM_CFG.gofmt_args
|
|
|
|
or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. gofmt}
|
2021-03-10 12:15:06 +00:00
|
|
|
|
2021-07-11 11:33:22 +00:00
|
|
|
local goimport_args = _GO_NVIM_CFG.goimport_args and _GO_NVIM_CFG.goimport_args
|
|
|
|
or {"--max-len=" .. tostring(max_len), "--base-formatter=" .. goimport}
|
2021-03-10 12:15:06 +00:00
|
|
|
|
|
|
|
local run = function(args, from_buffer)
|
2021-07-11 11:33:22 +00:00
|
|
|
|
2021-03-10 12:15:06 +00:00
|
|
|
if not from_buffer then
|
|
|
|
table.insert(args, api.nvim_buf_get_name(0))
|
|
|
|
print('formatting... ' .. api.nvim_buf_get_name(0) .. vim.inspect(args))
|
|
|
|
end
|
|
|
|
|
|
|
|
local old_lines = api.nvim_buf_get_lines(0, 0, -1, true)
|
|
|
|
table.insert(args, 1, "golines")
|
|
|
|
|
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
|
|
|
|
print("updating codes")
|
|
|
|
api.nvim_buf_set_lines(0, 0, -1, false, data)
|
|
|
|
api.nvim_command("write")
|
|
|
|
else
|
|
|
|
print("already formatted")
|
|
|
|
end
|
|
|
|
utils.log("stdout" .. vim.inspect(data))
|
|
|
|
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)
|
|
|
|
utils.log(vim.inspect(data) .. "stderr")
|
|
|
|
end,
|
|
|
|
on_exit = function(id, data, event)
|
|
|
|
utils.log(vim.inspect(data) .. "exit")
|
|
|
|
-- utils.log("current data " .. vim.inspect(new_lines))
|
|
|
|
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-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-07-11 11:33:22 +00:00
|
|
|
M.OrgImports = function(wait_ms)
|
|
|
|
local params = vim.lsp.util.make_range_params()
|
|
|
|
params.context = {only = {"source.organizeImports"}}
|
|
|
|
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, wait_ms)
|
|
|
|
for _, res in pairs(result or {}) do
|
|
|
|
for _, r in pairs(res.result or {}) do
|
|
|
|
if r.edit then
|
|
|
|
vim.lsp.util.apply_workspace_edit(r.edit)
|
|
|
|
else
|
|
|
|
vim.lsp.buf.execute_command(r.command)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
vim.lsp.buf.formatting()
|
|
|
|
end
|
|
|
|
|
2021-07-11 11:50:09 +00:00
|
|
|
M.goimport = function(buf)
|
2021-07-11 11:52:39 +00:00
|
|
|
if _GO_NVIM_CFG.goimport == 'gopls' then
|
2021-07-11 11:33:22 +00:00
|
|
|
M.OrgImports(1000)
|
|
|
|
return
|
|
|
|
end
|
2021-03-10 12:15:06 +00:00
|
|
|
buf = buf or false
|
|
|
|
require("go.install").install(goimport)
|
|
|
|
require("go.install").install("golines")
|
|
|
|
local a = {}
|
2021-07-07 05:56:22 +00:00
|
|
|
utils.copy_array(goimport_args, a)
|
2021-03-10 12:15:06 +00:00
|
|
|
run(a, buf)
|
|
|
|
end
|
|
|
|
return M
|