refactor: commands runner (#42)

* feat(utils): first impl of own commands runner

* refactor(gotests): uses own runner instead of vendored

* refactor(utils): back to plenary.job

* refactor(gotests): use new runner, clean code

* fix(runner): now it returns output correctly

* refactor(iferr): use vim.system

i have tried to use _utils.runner, but i can't figure out how to make `< file.go` for the command

* refactor(impl): use new runner

* refactor(installer): use new runner

* refactor(struct_tags): use new runner

* refactor: commands such as :GoGet runs with new runner

* refactor: throw errors in more lua way, i think

* refactor(utils): notify now has title

* refactor: use more correct way of notifying

* refactor(runner): write error message on error
This commit is contained in:
Smirnov Oleksandr 2023-08-10 12:04:33 +03:00
parent 011769b99b
commit 2e89cea6f3
11 changed files with 163 additions and 178 deletions

View file

@ -1,43 +0,0 @@
local Job = require "plenary.job"
local c = require("gopher.config").commands
local u = require "gopher._utils"
---Run any go commands like `go generate`, `go get`, `go mod`
---@param cmd string
---@param ... string|string[]
return function(cmd, ...)
local args = { ... }
if #args == 0 then
u.deferred_notify("please provice any arguments", vim.log.levels.ERROR)
return
end
if cmd == "generate" and #args == 1 and args[1] == "%" then
args[1] = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
elseif cmd == "get" then
for i, arg in ipairs(args) do
---@diagnostic disable-next-line: param-type-mismatch
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
table.remove(args, i)
table.insert(args, i, m)
end
end
local cmd_args = vim.list_extend({ cmd }, args) ---@diagnostic disable-line: missing-parameter
Job:new({
command = c.go,
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
u.deferred_notify(
"command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval,
vim.log.levels.ERROR
)
u.deferred_notify(cmd .. " " .. unpack(cmd_args), vim.log.levels.DEBUG)
return
end
u.deferred_notify("go " .. cmd .. " was success runned", vim.log.levels.INFO)
end,
}):start()
end

View file

@ -1,5 +1,7 @@
local utils = {}
local TITLE = "gopher.nvim"
---@param t table
---@return boolean
function utils.is_tbl_empty(t)
@ -10,13 +12,24 @@ function utils.is_tbl_empty(t)
end
---@param msg string
---@param lvl any
---@param lvl number
function utils.deferred_notify(msg, lvl)
vim.defer_fn(function()
vim.notify(msg, lvl)
vim.notify(msg, lvl, {
title = TITLE,
})
end, 0)
end
---@param msg string
---@param lvl? number
function utils.notify(msg, lvl)
lvl = lvl or vim.log.levels.INFO
vim.notify(msg, lvl, {
title = TITLE,
})
end
-- safe require
---@param module string module name
function utils.sreq(module)

View file

@ -0,0 +1,53 @@
local r = require "gopher._utils.runner"
local c = require("gopher.config").commands
local u = require "gopher._utils"
local gocmd = {}
---@param args string[]
---@return string[]
local function if_get(args)
for i, arg in ipairs(args) do
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
table.remove(args, i)
table.insert(args, i, m)
end
return args
end
---@param args unknown[]
---@return string[]
local function if_generate(args)
if #args == 1 and args[1] == "%" then
args[1] = vim.fn.expand "%"
end
return args
end
---@param subcmd string
---@param args string[]
---@return string[]|nil
function gocmd.run(subcmd, args)
if #args == 0 then
error "please provice any arguments"
end
if subcmd == "get" then
args = if_get(args)
end
if subcmd == "generate" then
args = if_generate(args)
end
return r.sync(c.go, {
args = { subcmd, unpack(args) },
on_exit = function(data, status)
if status ~= 0 then
error("gocmd failed: " .. data)
end
u.notify(c.go .. " " .. subcmd .. " successful runned")
end,
})
end
return gocmd

View file

@ -0,0 +1,33 @@
local Job = require "plenary.job"
local runner = {}
---@class gopher.RunnerOpts
---@field args? string[]
---@field cwd? string?
---@field on_exit? fun(data:string, status:number)
---@param cmd string
---@param opts gopher.RunnerOpts
---@return string[]|nil
function runner.sync(cmd, opts)
local output
Job:new({
command = cmd,
args = opts.args,
cwd = opts.cwd,
on_stderr = function(_, data)
vim.print(data)
end,
on_exit = function(data, status)
output = data:result()
vim.schedule(function()
if opts.on_exit then
opts.on_exit(output, status)
end
end)
end,
}):sync(60000 --[[1 min]])
return output
end
return runner