You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go.nvim/lua/go/format.lua

114 lines
3.1 KiB
Lua

3 years ago
-- golines A golang formatter that fixes long lines
-- golines + goimport
3 years ago
local api = vim.api
local utils = require("go.utils")
local log = utils.log
local max_len = _GO_NVIM_CFG.max_line_len or 120
3 years ago
local goimport = _GO_NVIM_CFG.goimport or "goimports"
3 years ago
local gofmt = _GO_NVIM_CFG.gofmt or "gofumpt"
local gofmt_args = _GO_NVIM_CFG.gofmt_args or {
"--max-len=" .. tostring(max_len),
"--base-formatter=" .. gofmt,
}
3 years ago
local goimport_args = _GO_NVIM_CFG.goimport_args
or {
"--max-len=" .. tostring(max_len),
"--base-formatter=" .. goimport,
}
3 years ago
3 years ago
local run = function(fmtargs, from_buffer, cmd)
local args = vim.deepcopy(fmtargs)
3 years ago
if not from_buffer then
table.insert(args, api.nvim_buf_get_name(0))
vim.notify("formatting buffer... " .. api.nvim_buf_get_name(0) .. vim.inspect(args), vim.lsp.log_levels.DEBUG)
3 years ago
else
vim.notify("formatting... " .. vim.inspect(args), vim.lsp.log_levels.DEBUG)
3 years ago
end
local old_lines = api.nvim_buf_get_lines(0, 0, -1, true)
if cmd then
table.insert(args, 1, cmd)
else
table.insert(args, 1, "golines")
end
3 years ago
log("fmt cmd:", args)
3 years ago
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
vim.notify("updating codes", vim.lsp.log_levels.DEBUG)
api.nvim_buf_set_lines(0, 0, -1, false, data)
api.nvim_command("write")
else
vim.notify("already formatted", vim.lsp.log_levels.DEBUG)
end
-- log("stdout" .. vim.inspect(data))
old_lines = nil
end,
on_stderr = function(job_id, data, event)
data = utils.handle_job_data(data)
log(vim.inspect(data) .. "from stderr")
end,
on_exit = function(id, data, event)
3 years ago
-- log(vim.inspect(data) .. "exit")
-- log("current data " .. vim.inspect(new_lines))
old_lines = nil
end,
stdout_buffered = true,
stderr_buffered = true,
})
3 years ago
vim.fn.chansend(j, old_lines)
vim.fn.chanclose(j, "stdin")
end
local M = {}
M.gofmt = function(buf)
if _GO_NVIM_CFG.gofmt == "gopls" then
-- log("gopls format")
vim.lsp.buf.formatting()
return
end
3 years ago
vim.env.GO_FMT = "gofumpt"
3 years ago
buf = buf or false
require("go.install").install(gofmt)
require("go.install").install("golines")
local a = {}
utils.copy_array(gofmt_args, a)
3 years ago
run(a, buf)
end
M.org_imports = function(wait_ms)
local codeaction = require("go.lsp").codeaction
codeaction("", "source.organizeImports", wait_ms)
vim.lsp.buf.formatting_sync(nil, wait_ms)
end
3 years ago
M.goimport = function(...)
local args = { ... }
if vim.fn.empty(args) == 1 and _GO_NVIM_CFG.goimport == "gopls" then
M.org_imports(1000)
return
end
3 years ago
local a1 = select(1, args)
local buf = true
3 years ago
if #args > 0 and type(args[1]) == "boolean" then
3 years ago
buf = a1
table.remove(args, 1)
end
3 years ago
require("go.install").install(goimport)
if #args > 0 and _GO_NVIM_CFG.goimport == "goimports" then -- dont use golines
return run(args, buf, "goimports")
end
local a = vim.deepcopy(goimport_args)
require("go.install").install("golines")
run(a, buf, "golines")
3 years ago
end
3 years ago
return M