feat(format): add in_place option for output
This commit is contained in:
+32
-3
@@ -149,6 +149,7 @@ end
|
|||||||
---@alias OutputStream
|
---@alias OutputStream
|
||||||
---| '"stdout"'
|
---| '"stdout"'
|
||||||
---| '"stderr"'
|
---| '"stderr"'
|
||||||
|
---| '"in_place"'
|
||||||
|
|
||||||
---@class FormatOptions
|
---@class FormatOptions
|
||||||
---@field cmd string[] Command to run. The following keywords get replaces by the specified values:
|
---@field cmd string[] Command to run. The following keywords get replaces by the specified values:
|
||||||
@@ -175,8 +176,14 @@ function M.format(opts)
|
|||||||
only_selection = opts.only_selection or false,
|
only_selection = opts.only_selection or false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.output ~= "stdout" and opts.output ~= "stderr" then
|
if
|
||||||
M.err("`output` must be set to either `stdout` or `stderr`.")
|
opts.output ~= "stdout"
|
||||||
|
and opts.output ~= "stderr"
|
||||||
|
and opts.output ~= "in_place"
|
||||||
|
then
|
||||||
|
M.err(
|
||||||
|
"`output` must be set to either `stdout`, `stderr` or `in_place`."
|
||||||
|
)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -211,6 +218,13 @@ function M.format(opts)
|
|||||||
input = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
input = vim.api.nvim_buf_get_lines(0, 0, -1, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local tmp
|
||||||
|
if opts.output == "in_place" then
|
||||||
|
tmp = os.tmpname()
|
||||||
|
vim.fn.writefile(input, tmp, "s")
|
||||||
|
file = tmp
|
||||||
|
end
|
||||||
|
|
||||||
for i, arg in ipairs(opts.cmd) do
|
for i, arg in ipairs(opts.cmd) do
|
||||||
arg = arg:gsub("%%file%%", file)
|
arg = arg:gsub("%%file%%", file)
|
||||||
arg = arg:gsub("%%filename%%", filename)
|
arg = arg:gsub("%%filename%%", filename)
|
||||||
@@ -248,6 +262,12 @@ function M.format(opts)
|
|||||||
end,
|
end,
|
||||||
}):wait()
|
}):wait()
|
||||||
|
|
||||||
|
local tmp_out
|
||||||
|
if tmp then
|
||||||
|
tmp_out = table.concat(vim.fn.readfile(tmp), "\n")
|
||||||
|
os.remove(tmp)
|
||||||
|
end
|
||||||
|
|
||||||
if err then
|
if err then
|
||||||
M.err("Error during formatting:\n%s" .. err)
|
M.err("Error during formatting:\n%s" .. err)
|
||||||
return
|
return
|
||||||
@@ -266,7 +286,16 @@ function M.format(opts)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local output = opts.output == "stdout" and stdout or stderr or ""
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
output = output or ""
|
||||||
output = output:gsub("\n$", "")
|
output = output:gsub("\n$", "")
|
||||||
local output_lines = vim.fn.split(output, "\n", true)
|
local output_lines = vim.fn.split(output, "\n", true)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user