refactor: correctness fixes, API modernization, and cleanup

This commit is contained in:
2026-04-13 01:20:52 +02:00
parent 62e64182de
commit fc7060e0ed
12 changed files with 166 additions and 223 deletions
+22 -64
View File
@@ -4,10 +4,7 @@ local Util = {}
Util.os_name = vim.uv.os_uname().sysname
---@alias OutputStream
---| '"stdout"'
---| '"stderr"'
---| '"in_place"'
---@alias OutputStream "stdout" | "stderr" | "in_place"
---@class ow.FormatOptions
---@field buf? integer Buffer to apply formatting to
@@ -26,37 +23,20 @@ Util.os_name = vim.uv.os_uname().sysname
---@field ignore_ret? boolean Ignore non-zero return codes
---@field ignore_stderr? boolean Ignore stderr output when not using stderr for output
---@field env? table<string, string> Map of environment variables
local FormatOptions = {}
FormatOptions.__index = FormatOptions
function FormatOptions.from_opts(opts)
return setmetatable({
buf = opts.buf or vim.api.nvim_get_current_buf(),
cmd = opts.cmd,
output = opts.output or "stdout",
auto_indent = opts.auto_indent or false,
only_selection = opts.only_selection or false,
ignore_ret = opts.ignore_ret or false,
ignore_stderr = opts.ignore_stderr or false,
env = opts.env,
}, FormatOptions)
end
--- Format buffer
---@param opts ow.FormatOptions
function Util.format(opts)
opts = FormatOptions.from_opts(opts)
if
opts.output ~= "stdout"
and opts.output ~= "stderr"
and opts.output ~= "in_place"
then
log.error(
"`output` must be set to either `stdout`, `stderr` or `in_place`."
)
return
end
opts = {
buf = opts.buf or vim.api.nvim_get_current_buf(),
cmd = opts.cmd,
output = opts.output or "stdout",
auto_indent = opts.auto_indent,
only_selection = opts.only_selection,
ignore_ret = opts.ignore_ret,
ignore_stderr = opts.ignore_stderr,
env = opts.env,
}
local file = vim.api.nvim_buf_get_name(opts.buf)
local filename = vim.fn.fnamemodify(file, ":t")
@@ -143,29 +123,12 @@ function Util.format(opts)
opts.cmd[i] = arg
end
local stdout, stderr, err
local resp = vim.system(opts.cmd, {
stdin = input,
stdout = opts.output == "stdout" and function(e, data)
if data then
stdout = stdout and stdout .. data or data
end
if e then
err = err and err .. e or e
end
end,
stderr = not opts.ignore_stderr and function(e, data)
if e then
err = err and err .. e or e
end
if data then
stderr = stderr and stderr .. data or data
end
end,
env = opts.env,
}):wait()
local stdout = resp.stdout or ""
local stderr = resp.stderr or ""
local tmp_out
if tmp then
@@ -173,17 +136,12 @@ function Util.format(opts)
os.remove(tmp)
end
if err then
log.error("Error during formatting:\n%s", err)
return
end
if
not opts.ignore_ret and resp.code ~= 0
or (opts.output ~= "stderr" and stderr)
(not opts.ignore_ret and resp.code ~= 0)
or (opts.output ~= "stderr" and not opts.ignore_stderr and stderr ~= "")
then
local msg = ""
if stderr then
if stderr ~= "" then
msg = ":\n" .. stderr
end
@@ -191,13 +149,13 @@ function Util.format(opts)
return
end
local output
local output = ""
if opts.output == "stdout" then
output = stdout
elseif opts.output == "stderr" then
output = stderr
elseif opts.output == "in_place" then
output = tmp_out
output = tmp_out or ""
end
output = output:gsub("%s+$", "")
@@ -258,7 +216,7 @@ function Util.format(opts)
local view = vim.fn.winsaveview()
vim.lsp.util.apply_text_edits(text_edits, opts.buf, vim.o.encoding)
vim.lsp.util.apply_text_edits(text_edits, opts.buf, "utf-16")
if opts.auto_indent then
vim.api.nvim_cmd({
@@ -335,10 +293,10 @@ end
--- Creates a debounced function that delays execution of `fn` until after `delay` milliseconds have
--- elapsed since the last time it was invoked.
---@param fn fun(...) Function to be debounced
---@param delay number Debounce delay in milliseconds
---@param delay integer Debounce delay in milliseconds
---@return fun(...) function Debounced function
function Util.debounce(fn, delay)
---@type uv_timer_t?
---@type uv.uv_timer_t?
local timer = nil
return function(...)
@@ -358,7 +316,7 @@ end
--- Creates a debounced function that delays execution of `fn` until after `delay` milliseconds have
--- elapsed since the last time it was invoked with the same unique identifier.
---@param fn fun(...) Function to be debounced
---@param delay number Debounce delay in milliseconds
---@param delay integer Debounce delay in milliseconds
---@return fun(id: any, ...) function Debounced function, where `id` is a unique identifier
function Util.debounce_with_id(fn, delay)
local map = {}