feat(lsp): add format function

This commit is contained in:
2024-04-19 17:22:28 +02:00
parent ad7b8848d1
commit 7d7699474d
+132
View File
@@ -136,6 +136,138 @@ function M.try_require(module)
M.err(("Failed to load module %s:\n%s"):format(module, resp))
end
---@class FormatOptions
---@field cmd string[] Command to run. The following keywords get replaces by the specified values:
--- * %file% - path to the current file
--- * %row_start% - first row of selection
--- * %row_end% - last row of selection
--- * %col_start% - first column position of selection
--- * %col_end% - last column position of selection
--- * %byte_start% - byte count of first cell in selection
--- * %byte_end% - byte count of last cell in selection
---@field stdin boolean? Pass text to stdin. False by default.
---@field stdout boolean? Use stdout as the result. False by default.
---@field stderr boolean? Use stderr as the result. False by default.
---@field in_place boolean? The file is formatted in-place by `cmd`. False by default.
---@field auto_indent boolean? Perform auto indent on formatted range. False by default.
---@field selection boolean? Only format the currently selected lines. False by default.
--- Format buffer
---@param opts FormatOptions
function M.format(opts)
opts = {
cmd = opts.cmd,
stdin = opts.stdin or false,
stdout = opts.stdout or false,
stderr = opts.stderr or false,
in_place = opts.in_place or false,
auto_indent = opts.auto_indent or false,
selection = opts.selection or false,
}
if not opts.in_place and not opts.stdout and not opts.stderr then
M.err("One of `in_place`, `stdout` or `stderr` must be true.")
return
elseif opts.in_place and (opts.selection or opts.stdin or opts.stdout or opts.stderr) then
M.err(
"`in_place` is not valid together with any of "
.. "`selection`, `stdin`, `stdout` or `stderr`"
)
return
end
local file = vim.fn.expand("%")
local mode = vim.fn.mode()
local is_visual = mode == "v" or mode == "V" or mode == ""
local row_start, row_end, col_start, col_end, byte_start, byte_end
if is_visual then
row_start, col_start = unpack(vim.fn.getpos("v"), 2, 3)
row_end, col_end = unpack(vim.fn.getpos("."), 2, 3)
if row_start > row_end then
row_start, row_end, col_start, col_end = row_end, row_start, col_end, col_start
end
if mode == "V" then
col_start = 1
col_end = #vim.fn.getline(row_end)
end
byte_start = vim.fn.line2byte(row_start) + col_start - 1
byte_end = vim.fn.line2byte(row_end) + col_end - 1
end
local input
if opts.selection then
input = vim.api.nvim_buf_get_lines(0, row_start - 1, row_end, false)
else
input = vim.api.nvim_buf_get_lines(0, 0, -1, false)
end
if opts.in_place then
vim.api.nvim_buf_call(0, vim.cmd.write)
end
for i, arg in ipairs(opts.cmd) do
arg = arg:gsub("%%file%%", file)
if is_visual then
arg = arg:gsub("%%row_start%%", row_start)
arg = arg:gsub("%%row_end%%", row_end)
arg = arg:gsub("%%col_start%%", col_start)
arg = arg:gsub("%%col_end%%", col_end)
arg = arg:gsub("%%byte_start%%", byte_start)
arg = arg:gsub("%%byte_end%%", byte_end)
end
opts.cmd[i] = arg
end
vim.system(
opts.cmd,
{
stdin = opts.stdin and input or nil,
},
vim.schedule_wrap(function(out)
if out.code ~= 0 or out.signal ~= 0 then
local err = out.stderr or ""
M.err(("Failed to format:\n%s"):format(err))
return
end
if opts.in_place then
vim.api.nvim_buf_call(0, vim.cmd.edit)
else
local output
if opts.stdout then
output = out.stdout or ""
end
if opts.stderr then
output = out.stderr or ""
end
output = output:gsub("\n$", "")
local output_lines = vim.fn.split(output, "\n", true)
if opts.selection then
vim.api.nvim_buf_set_lines(0, row_start - 1, row_end, false, output_lines)
else
vim.api.nvim_buf_set_lines(0, 0, -1, false, output_lines)
end
if opts.auto_indent then
if is_visual then
vim.api.nvim_command(
("%d,%dnormal! =="):format(row_start, row_start + #output_lines)
)
else
vim.api.nvim_command("normal! gg=G")
end
end
end
end)
)
end
--- Check if `val` is a list of type `t` (if given)
---@param val any
---@param t type?