feat(format): make stdout/stderr selection mutually exclusive
This commit is contained in:
@@ -19,8 +19,7 @@ return {
|
|||||||
rhs = function()
|
rhs = function()
|
||||||
utils.format({
|
utils.format({
|
||||||
cmd = { "shfmt", "-s", "-i", "4", "-" },
|
cmd = { "shfmt", "-s", "-i", "4", "-" },
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ return {
|
|||||||
"--quiet",
|
"--quiet",
|
||||||
"-",
|
"-",
|
||||||
},
|
},
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
utils.format({
|
utils.format({
|
||||||
cmd = {
|
cmd = {
|
||||||
@@ -53,8 +52,7 @@ return {
|
|||||||
"--quiet",
|
"--quiet",
|
||||||
"-",
|
"-",
|
||||||
},
|
},
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
@@ -73,8 +71,7 @@ return {
|
|||||||
"--line-ranges=%row_start%-%row_end%",
|
"--line-ranges=%row_start%-%row_end%",
|
||||||
"-",
|
"-",
|
||||||
},
|
},
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ return {
|
|||||||
rhs = function()
|
rhs = function()
|
||||||
utils.format({
|
utils.format({
|
||||||
cmd = { "stylua", "-" },
|
cmd = { "stylua", "-" },
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
@@ -34,8 +33,7 @@ return {
|
|||||||
"--range-end",
|
"--range-end",
|
||||||
"%byte_end%",
|
"%byte_end%",
|
||||||
},
|
},
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -44,8 +44,7 @@ return {
|
|||||||
"--quiet",
|
"--quiet",
|
||||||
"-",
|
"-",
|
||||||
},
|
},
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
utils.format({
|
utils.format({
|
||||||
cmd = {
|
cmd = {
|
||||||
@@ -53,8 +52,7 @@ return {
|
|||||||
"--quiet",
|
"--quiet",
|
||||||
"-",
|
"-",
|
||||||
},
|
},
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
@@ -73,8 +71,7 @@ return {
|
|||||||
"--line-ranges=%row_start%-%row_end%",
|
"--line-ranges=%row_start%-%row_end%",
|
||||||
"-",
|
"-",
|
||||||
},
|
},
|
||||||
stdin = true,
|
output = "stdout",
|
||||||
stdout = true,
|
|
||||||
})
|
})
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
|
|||||||
+26
-21
@@ -144,6 +144,10 @@ function M.has_module(module)
|
|||||||
return has_module
|
return has_module
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@alias OutputStream
|
||||||
|
---| '"stdout"'
|
||||||
|
---| '"stderr"'
|
||||||
|
|
||||||
---@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:
|
||||||
--- * %file% - path to the current file
|
--- * %file% - path to the current file
|
||||||
@@ -154,8 +158,7 @@ end
|
|||||||
--- * %col_end% - last column position of selection
|
--- * %col_end% - last column position of selection
|
||||||
--- * %byte_start% - byte count of first cell in selection
|
--- * %byte_start% - byte count of first cell in selection
|
||||||
--- * %byte_end% - byte count of last cell in selection
|
--- * %byte_end% - byte count of last cell in selection
|
||||||
---@field stdout? boolean Use stdout as the result. False by default.
|
---@field output OutputStream What stream to use as the result. May be one of `stdout` or `stderr`.
|
||||||
---@field stderr? boolean Use stderr as the result. False by default.
|
|
||||||
---@field auto_indent? boolean Perform auto indent on formatted range. False by default.
|
---@field auto_indent? boolean Perform auto indent on formatted range. False by default.
|
||||||
---@field only_selection? boolean Only send the selected lines to `stdin`. False by default.
|
---@field only_selection? boolean Only send the selected lines to `stdin`. False by default.
|
||||||
|
|
||||||
@@ -164,14 +167,13 @@ end
|
|||||||
function M.format(opts)
|
function M.format(opts)
|
||||||
opts = {
|
opts = {
|
||||||
cmd = opts.cmd,
|
cmd = opts.cmd,
|
||||||
stdout = opts.stdout or false,
|
output = opts.output,
|
||||||
stderr = opts.stderr or false,
|
|
||||||
auto_indent = opts.auto_indent or false,
|
auto_indent = opts.auto_indent or false,
|
||||||
only_selection = opts.only_selection or false,
|
only_selection = opts.only_selection or false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if not opts.stdout and not opts.stderr then
|
if opts.output ~= "stdout" and opts.output ~= "stderr" then
|
||||||
M.err("one of `stdout` or `stderr` must be set")
|
M.err("`output` must be set to either `stdout` or `stderr`.")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -222,7 +224,7 @@ function M.format(opts)
|
|||||||
local stdout, stderr, err
|
local stdout, stderr, err
|
||||||
local resp = vim.system(opts.cmd, {
|
local resp = vim.system(opts.cmd, {
|
||||||
stdin = input,
|
stdin = input,
|
||||||
stdout = opts.stdout and function(e, data)
|
stdout = opts.output == "stdout" and function(e, data)
|
||||||
if data then
|
if data then
|
||||||
stdout = stdout and stdout .. data or data
|
stdout = stdout and stdout .. data or data
|
||||||
end
|
end
|
||||||
@@ -231,7 +233,7 @@ function M.format(opts)
|
|||||||
err = err and err .. e or e
|
err = err and err .. e or e
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
stderr = opts.stderr and function(e, data)
|
stderr = function(e, data)
|
||||||
if data then
|
if data then
|
||||||
stderr = stderr and stderr .. data or data
|
stderr = stderr and stderr .. data or data
|
||||||
end
|
end
|
||||||
@@ -247,19 +249,24 @@ function M.format(opts)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if resp.code ~= 0 or resp.signal ~= 0 then
|
if
|
||||||
M.err(("Failed to format:\n%s"):format(stderr or ""))
|
resp.code ~= 0
|
||||||
|
or resp.signal ~= 0
|
||||||
|
or (opts.output ~= "stderr" and stderr)
|
||||||
|
then
|
||||||
|
local msg
|
||||||
|
if stdout then
|
||||||
|
msg = "\n" .. stdout
|
||||||
|
end
|
||||||
|
if stderr then
|
||||||
|
msg = "\n" .. stderr
|
||||||
|
end
|
||||||
|
|
||||||
|
M.err(("Failed to format:%s"):format(msg or ""))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local output
|
local output = opts.output == "stdout" and stdout or stderr or ""
|
||||||
if opts.stdout then
|
|
||||||
output = stdout or ""
|
|
||||||
end
|
|
||||||
if opts.stderr then
|
|
||||||
output = stderr or ""
|
|
||||||
end
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
@@ -271,9 +278,7 @@ function M.format(opts)
|
|||||||
|
|
||||||
if opts.auto_indent then
|
if opts.auto_indent then
|
||||||
if is_visual then
|
if is_visual then
|
||||||
vim.api.nvim_command(
|
vim.api.nvim_command(("%d,%dnormal! =="):format(row_start, row_start + #output_lines))
|
||||||
("%d,%dnormal! =="):format(row_start, row_start + #output_lines)
|
|
||||||
)
|
|
||||||
else
|
else
|
||||||
vim.api.nvim_command("normal! gg=G")
|
vim.api.nvim_command("normal! gg=G")
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user