issue #141 the result of shell command should be checked before proccess

the output
pull/144/head
ray-x 2 years ago
parent 5504f55b4f
commit 3f8551cc11

@ -35,6 +35,11 @@ local function extract_filepath(msg)
end
local cmd = "find ./ -type f -name " .. s
local path = vim.fn.systemlist(cmd)
if vim.v.shell_error ~= 0 then
util.warn("find failed" .. vim.inspect(path))
return
end
for _, value in pairs(path) do
local st, _ = value:find(s)
log(value, st)

@ -1,5 +1,6 @@
local utils = require("go.utils")
local log = utils.log
local runner = require("go.runner")
local curl = "curl"
local run = function(...)
local query = select(1, ...)
@ -9,36 +10,43 @@ local run = function(...)
local cmd = string.format("%s cht.sh/go/%s?T", curl, query)
cmd = vim.split(cmd, " ")
log(cmd)
local opts = {
on_exit = function(code, signal, output)
if code ~= 0 or signal ~= 0 then
log(string.format("command exited with code: %s, signal: %s", code, signal))
return
end
vim.schedule(function()
local data = vim.split(output, "\n")
data = utils.handle_job_data(data)
local data = vim.fn.systemlist(cmd, vim.fn.bufnr("%"))
if #data > 4 then
data = vim.list_slice(data, 4, #data)
local TextView = utils.load_plugin("guihua.lua", "guihua.textview")
if TextView then
local win = TextView:new({
loc = "top_center",
syntax = "go",
rect = { height = #data, pos_x = 0, pos_y = 4 },
data = data,
enter = true,
})
log("draw data", data)
win:on_draw(data)
else
local name = vim.fn.tempname() .. ".go"
vim.fn.writefile(data, name)
cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
vim.cmd("e")
end
else
vim.notify("No result " .. vim.inspect(data))
end
end)
end,
}
data = utils.handle_job_data(data)
if not data then
return
end
-- log(data)
if #data > 4 then
data = vim.list_slice(data, 4, #data)
local TextView = utils.load_plugin("guihua.lua", "guihua.textview")
if TextView then
local win = TextView:new({
loc = "top_center",
syntax = "go",
rect = { height = #data, pos_x = 0, pos_y = 4 },
data = data,
enter = true,
})
log("draw data", data)
win:on_draw(data)
else
local name = vim.fn.tempname() .. ".go"
vim.fn.writefile(data, name)
cmd = " silent exe 'e " .. name .. "'"
vim.cmd(cmd)
vim.cmd("e")
end
else
vim.notify("No result " .. vim.inspect(data))
end
runner.run(cmd, opts)
end
return { run = run }

@ -45,6 +45,7 @@ local function match_partial_item_name(pkg, pattern)
local cmd = string.format("go doc %s", pkg)
local doc = vfn.systemlist(cmd)
if vim.v.shell_error ~= 0 then
utils.warn("go doc failed", vim.inspect(doc))
return
end

@ -26,7 +26,8 @@ function M.run(args)
end
end
local opts = {
after = function()
update_buffer = true,
on_exit = function()
vim.schedule(function()
utils.restart()
end)

@ -1,26 +0,0 @@
-- local ts_utils = require 'nvim-treesitter.ts_utils'
local utils = require("go.utils")
local golangci_lint = "golangci-lint"
local run = function(...)
require("go.install").install(golangci_lint)
local fname = vim.fn.expand("%:p") -- %:p:h ? %:p
local cmd = string.format("golangci-lint -pos %d", byte_offset)
local data = vim.fn.systemlist(cmd, vim.fn.bufnr("%"))
data = utils.handle_job_data(data)
if not data then
return
end
local pos = vim.fn.getcurpos()[2]
vim.fn.append(pos, data)
vim.cmd("silent normal! j=2j")
vim.fn.setpos(".", pos)
vim.cmd("silent normal! 4j")
--
end
return { run = run }

@ -381,6 +381,11 @@ M.test_file = function(...)
local run_in_floaterm = optarg["F"] or _GO_NVIM_CFG.run_in_floaterm
local tests = vfn.systemlist(cmd)
if vim.v.shell_error ~= 0 then
utils.warn("iferr failed" .. vim.inspect(tests))
return
end
utils.log(cmd, tests)
tests = tests[1]
if vfn.empty(tests) == 1 then

@ -16,6 +16,10 @@ local run = function()
if not data then
return
end
if vim.v.shell_error ~= 0 then
utils.warn("iferr failed" .. vim.inspect(data))
return
end
local pos = vfn.getcurpos()[2]
vfn.append(pos, data)

@ -71,6 +71,10 @@ local run = function(...)
if not data then
return
end
if vim.v.shell_error ~= 0 then
utils.warn("impl failed" .. vim.inspect(data))
return
end
--
local pos = vfn.getcurpos()[2]
table.insert(data, 1, "")
@ -89,6 +93,7 @@ local function match_iface_name(part)
local cmd = string.format("go doc %s", pkg)
local doc = vfn.systemlist(cmd)
if vim.v.shell_error ~= 0 then
utils.warn("go doc failed" .. vim.inspect(doc))
return
end

@ -10,6 +10,7 @@ function M.list(mod, args)
vim.list_extend(cmd, args or {'.'})
out = vim.fn.systemlist(table.concat(cmd, ' '))
if vim.v.shell_error ~= 0 then
require('go.utils').warn('go list failed', vim.inspect(out))
return false
end
for i, e in ipairs(out) do

@ -9,7 +9,7 @@ function M.run(...)
cmd = vim.list_extend(cmd, args)
utils.log(cmd)
local opts = {
after = function()
on_exit = function()
vim.schedule(function()
utils.restart()
end)

@ -54,7 +54,7 @@ local show_float = function(result)
if not textview then
util.log("Failed to load guihua.textview")
vim.fn.setloclist({}, " ", {
vim.fn.setloclist(0, {}, " ", {
title = "go package outline",
lines = result,
})
@ -172,7 +172,7 @@ local show_panel = function(result, pkg, rerender)
})
p:open(true)
else
vim.fn.setloclist({}, " ", {
vim.fn.setloclist(0, {}, " ", {
title = "go package outline",
lines = defs,
})

@ -2,6 +2,7 @@ local uv, api = vim.loop, vim.api
local util = require("go.utils")
local log = require("go.utils").log
-- run command with loop
local run = function(cmd, opts)
opts = opts or {}
log(cmd)
@ -30,63 +31,60 @@ local run = function(cmd, opts)
local stderr = uv.new_pipe(false)
-- local file = api.nvim_buf_get_name(0)
local handle = nil
-- setup output
api.nvim_command("10new")
-- assert(cwd and M.path.is_dir(cwd), "sh: Invalid directory")
local winnr = api.nvim_get_current_win()
local bufnr = api.nvim_get_current_buf()
local output_buf = ""
local function update_chunk(err, chunk)
local function update_chunk_fn(err, chunk)
if err then
vim.notify("error " .. err, vim.lsp.log_levels.INFO)
return vim.notify("error " .. tostring(err) .. vim.inspect(chunk or {}), vim.lsp.log_levels.INFO)
end
if chunk then
output_buf = output_buf .. chunk
local lines = vim.split(output_buf, "\n", true)
api.nvim_buf_set_option(bufnr, "modifiable", true)
api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
api.nvim_buf_set_option(bufnr, "modifiable", false)
api.nvim_buf_set_option(bufnr, "modified", false)
if api.nvim_win_is_valid(winnr) then
api.nvim_win_set_cursor(winnr, { #lines, 0 })
end
end
end
update_chunk = vim.schedule_wrap(update_chunk)
local update_chunk = vim.schedule_wrap(opts.update_chunk or update_chunk_fn)
log("job:", cmd, job_options)
handle, _ = uv.spawn(
cmd,
{ stdio = { stdin, stdout, stderr }, args = job_options.args },
function(code, signal) -- on exit
update_chunk(nil, cmd_str .. " finished with code " .. code .. " / signal " .. signal)
stdin:read_stop()
stdin:close()
stdout:read_stop()
stdout:close()
stderr:read_stop()
stderr:close()
handle:close()
if opts and opts.after then
opts.after()
log(output_buf)
if code == 0 then
if opts and opts.on_exit then
-- if on_exit hook is on the hook output is what we want to show in loc
-- this avoid show samething in both on_exit and loc
output_buf = opts.on_exit(code, signal, output_buf)
end
vim.schedule(function()
update_chunk(nil, cmd_str .. " finished with code " .. code .. " / signal " .. signal)
local lines = vim.split(output_buf, "\n", true)
local locopts = {
title = vim.inspect(cmd),
lines = lines,
}
if opts.efm then
locopts.efm = opts.efm
end
log(locopts)
if #lines > 0 then
vim.fn.setloclist(0, {}, " ", locopts)
vim.cmd("lopen")
end
end)
end
end
)
api.nvim_buf_attach(bufnr, false, {
on_detach = function()
if not handle:is_closing() then
handle:kill(15)
end
end,
})
-- Return to previous cursor position
api.nvim_command("wincmd p")
-- uv.read_start(stdout, vim.schedule_wrap(on_stdout))
uv.read_start(stderr, function(err, data)
assert(not err, err)
if data then
@ -94,7 +92,7 @@ local run = function(cmd, opts)
end
end)
stdout:read_start(update_chunk)
stderr:read_start(update_chunk)
-- stderr:read_start(update_chunk)
end
local function make(...)

Loading…
Cancel
Save