-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
11 changed files
with
163 additions
and
178 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
return output | ||
end | ||
|
||
return runner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,45 @@ | ||
local c = require("gopher.config").commands | ||
local u = require "gopher._utils" | ||
local ts_utils = require "gopher._utils.ts" | ||
local Job = require "plenary.job" | ||
local r = require "gopher._utils.runner" | ||
local u = require "gopher._utils" | ||
local gotests = {} | ||
|
||
---@param cmd_args table | ||
local function run(cmd_args) | ||
Job:new({ | ||
command = c.gotests, | ||
args = cmd_args, | ||
on_exit = function(_, retval) | ||
if retval ~= 0 then | ||
u.deferred_notify( | ||
"command '" .. c.gotests .. " " .. unpack(cmd_args) .. "' exited with code " .. retval, | ||
vim.log.levels.ERROR | ||
) | ||
return | ||
end | ||
|
||
u.deferred_notify("unit test(s) generated", vim.log.levels.INFO) | ||
end, | ||
}):start() | ||
end | ||
|
||
---@param args table | ||
local function add_test(args) | ||
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter | ||
table.insert(args, "-w") | ||
table.insert(args, fpath) | ||
run(args) | ||
table.insert(args, vim.fn.expand "%") | ||
|
||
return r.sync(c.gotests, { | ||
args = args, | ||
on_exit = function(data, status) | ||
if not status == 0 then | ||
error("gotests failed: " .. data) | ||
end | ||
|
||
u.notify "unit test(s) generated" | ||
end, | ||
}) | ||
end | ||
|
||
---generate unit test for one function | ||
---@param parallel boolean | ||
function gotests.func_test(parallel) | ||
function gotests.func_test() | ||
local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) | ||
if ns == nil or ns.name == nil then | ||
u.deferred_notify("cursor on func/method and execute the command again", vim.log.levels.INFO) | ||
u.notify("cursor on func/method and execute the command again", vim.log.levels.WARN) | ||
return | ||
end | ||
|
||
local cmd_args = { "-only", ns.name } | ||
if parallel then | ||
table.insert(cmd_args, "-parallel") | ||
end | ||
|
||
add_test(cmd_args) | ||
add_test { "-only", ns.name } | ||
end | ||
|
||
---generate unit tests for all functions in current file | ||
---@param parallel boolean | ||
function gotests.all_tests(parallel) | ||
local cmd_args = { "-all" } | ||
if parallel then | ||
table.insert(cmd_args, "-parallel") | ||
end | ||
|
||
add_test(cmd_args) | ||
function gotests.all_tests() | ||
add_test { "-all" } | ||
end | ||
|
||
---generate unit tests for all exported functions | ||
---@param parallel boolean | ||
function gotests.all_exported_tests(parallel) | ||
local cmd_args = {} | ||
if parallel then | ||
table.insert(cmd_args, "-parallel") | ||
end | ||
|
||
table.insert(cmd_args, "-exported") | ||
add_test(cmd_args) | ||
function gotests.all_exported_tests() | ||
add_test { "-exported" } | ||
end | ||
|
||
return gotests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.