refactor: use vim.system instead of pleanry (#85)

* refactor!: migrate to vim.system

* refactor(gotests): use vim.system

* refactor(iferr): use vim.system

* refactor(impl): use vim.system

* refactor(installer): use vim.system and add sync mode

* test: fix gotests' tests

* refactor(struct_tags): use vim.system

* chore(ci): install all deps explicitly

* refactor(installer)!: add sync as an option

* docs: update readme
This commit is contained in:
Smirnov Oleksandr 2025-03-02 16:31:50 +02:00 committed by GitHub
parent 837897a79d
commit 6016ca57d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 169 additions and 126 deletions

View file

@ -47,7 +47,12 @@ jobs:
key: ${{ runner.os }}-tests-${{ hashFiles('${{ github.workspace }}/.tests') }} key: ${{ runner.os }}-tests-${{ hashFiles('${{ github.workspace }}/.tests') }}
- name: Install Go bins - name: Install Go bins
run: nvim --headless -u "./scripts/minimal_init.lua" -c "GoInstallDeps" -c "qa!" run: |
# TODO: install with :GoInstallDeps
go install github.com/fatih/gomodifytags@latest
go install github.com/josharian/impl@latest
go install github.com/cweill/gotests/...@latest
go install github.com/koron/iferr@latest
- name: Run Tests - name: Run Tests
run: | run: |

View file

@ -13,17 +13,15 @@ It's **NOT** an LSP tool, the main goal of this plugin is to add go tooling supp
Requirements: Requirements:
- **Neovim 0.10** or later - **Neovim 0.10** or later
- `go` treesitter parser, install by `:TSInstall go` - Treesitter `go` parser(`:TSInstall go`)
- [Go](https://github.com/golang/go) installed (tested on 1.23) - [Go](https://github.com/golang/go) installed (tested on 1.23)
```lua ```lua
{ {
"olexsmir/gopher.nvim", "olexsmir/gopher.nvim",
ft = "go", ft = "go",
-- branch = "develop", -- if you want develop branch -- branch = "develop"
-- keep in mind, it might break everything
dependencies = { dependencies = {
"nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter", "nvim-treesitter/nvim-treesitter",
}, },
-- (optional) will update plugin's deps on every update -- (optional) will update plugin's deps on every update

View file

@ -3,8 +3,9 @@ local log = require "gopher._utils.log"
local utils = {} local utils = {}
---@param msg string ---@param msg string
---@param lvl number ---@param lvl? number
function utils.deferred_notify(msg, lvl) function utils.deferred_notify(msg, lvl)
lvl = lvl or vim.log.levels.INFO
vim.defer_fn(function() vim.defer_fn(function()
vim.notify(msg, lvl, { vim.notify(msg, lvl, {
title = c.___plugin_name, title = c.___plugin_name,
@ -23,4 +24,22 @@ function utils.notify(msg, lvl)
log.debug(msg) log.debug(msg)
end end
---@param path string
---@return string
function utils.readfile_joined(path)
return table.concat(vim.fn.readfile(path), "\n")
end
---@param t string[]
---@return string[]
function utils.remove_empty_lines(t)
local res = {}
for _, line in ipairs(t) do
if line ~= "" then
table.insert(res, line)
end
end
return res
end
return utils return utils

View file

@ -1,33 +1,39 @@
local Job = require "plenary.job" local c = require "gopher.config"
local runner = {} local runner = {}
---@class gopher.RunnerOpts ---@class gopher.RunnerOpts
---@field args? string[] ---@field cwd? string
---@field cwd? string? ---@field timeout? number
---@field on_exit? fun(data:string, status:number) ---@field stdin? boolean|string|string[]
---@field text? boolean
---@param cmd string ---@param cmd (string|number)[]
---@param opts gopher.RunnerOpts ---@param on_exit fun(out:vim.SystemCompleted)
---@return string[]|nil ---@param opts? gopher.RunnerOpts
function runner.sync(cmd, opts) ---@return vim.SystemObj
local output function runner.async(cmd, on_exit, opts)
Job:new({ opts = opts or {}
command = cmd, return vim.system(cmd, {
args = opts.args, cwd = opts.cwd or nil,
cwd = opts.cwd, timeout = opts.timeout or c.timeout,
on_stderr = function(_, data) stdin = opts.stdin or nil,
vim.print(data) text = opts.text or true,
end, }, on_exit)
on_exit = function(data, status)
output = data:result()
vim.schedule(function()
if opts.on_exit then
opts.on_exit(output, status)
end end
end)
end, ---@param cmd (string|number)[]
}):sync(60000 --[[1 min]]) ---@param opts? gopher.RunnerOpts
return output ---@return vim.SystemCompleted
function runner.sync(cmd, opts)
opts = opts or {}
return vim
.system(cmd, {
cwd = opts.cwd or nil,
timeout = opts.timeout or c.timeout,
stdin = opts.stdin or nil,
text = opts.text or true,
})
:wait()
end end
return runner return runner

View file

@ -33,6 +33,10 @@ local default_config = {
---@type number ---@type number
log_level = vim.log.levels.INFO, log_level = vim.log.levels.INFO,
-- timeout for running commands
---@type number
timeout = 2000,
-- user specified paths to binaries -- user specified paths to binaries
---@class gopher.ConfigCommand ---@class gopher.ConfigCommand
commands = { commands = {

View file

@ -67,16 +67,12 @@ local function add_test(args)
log.debug("generating tests with args: ", args) log.debug("generating tests with args: ", args)
return r.sync(c.commands.gotests, { local rs = r.sync { c.commands.gotests, unpack(args) }
args = args, if rs.code ~= 0 then
on_exit = function(data, status) error("gotests failed: " .. rs.stderr)
if not status == 0 then
error("gotests failed: " .. data)
end end
u.notify "unit test(s) generated" u.notify "unit test(s) generated"
end,
})
end end
-- generate unit test for one function -- generate unit test for one function

View file

@ -4,9 +4,7 @@ local u = require "gopher._utils.health_util"
local deps = { local deps = {
plugin = { plugin = {
{ lib = "dap", msg = "required for `gopher.dap`", optional = true }, { lib = "nvim-treesitter", msg = "required for everything in gopher.nvim" },
{ lib = "plenary", msg = "required for everything in gopher.nvim", optional = false },
{ lib = "nvim-treesitter", msg = "required for everything in gopher.nvim", optional = false },
}, },
bin = { bin = {
{ {
@ -14,17 +12,17 @@ local deps = {
msg = "required for `:GoGet`, `:GoMod`, `:GoGenerate`, `:GoWork`, `:GoInstallDeps`", msg = "required for `:GoGet`, `:GoMod`, `:GoGenerate`, `:GoWork`, `:GoInstallDeps`",
optional = false, optional = false,
}, },
{ bin = cmd.gomodifytags, msg = "required for `:GoTagAdd`, `:GoTagRm`", optional = false }, { bin = cmd.gomodifytags, msg = "required for `:GoTagAdd`, `:GoTagRm`", optional = true },
{ bin = cmd.impl, msg = "required for `:GoImpl`", optional = false }, { bin = cmd.impl, msg = "required for `:GoImpl`", optional = true },
{ bin = cmd.iferr, msg = "required for `:GoIfErr`", optional = false }, { bin = cmd.iferr, msg = "required for `:GoIfErr`", optional = true },
{ {
bin = cmd.gotests, bin = cmd.gotests,
msg = "required for `:GoTestAdd`, `:GoTestsAll`, `:GoTestsExp`", msg = "required for `:GoTestAdd`, `:GoTestsAll`, `:GoTestsExp`",
optional = false, optional = true,
}, },
}, },
treesitter = { treesitter = {
{ parser = "go", msg = "required for `gopher.nvim`", optional = false }, { parser = "go", msg = "required for `gopher.nvim`" },
}, },
} }
@ -33,14 +31,10 @@ function health.check()
for _, plugin in ipairs(deps.plugin) do for _, plugin in ipairs(deps.plugin) do
if u.is_lualib_found(plugin.lib) then if u.is_lualib_found(plugin.lib) then
u.ok(plugin.lib .. " installed") u.ok(plugin.lib .. " installed")
else
if plugin.optional then
u.warn(plugin.lib .. " not found, " .. plugin.msg)
else else
u.error(plugin.lib .. " not found, " .. plugin.msg) u.error(plugin.lib .. " not found, " .. plugin.msg)
end end
end end
end
u.start "required binaries" u.start "required binaries"
u.info "all those binaries can be installed by `:GoInstallDeps`" u.info "all those binaries can be installed by `:GoInstallDeps`"

View file

@ -4,27 +4,33 @@
---@usage Execute `:GoIfErr` near any `err` variable to insert the check ---@usage Execute `:GoIfErr` near any `err` variable to insert the check
local c = require "gopher.config" local c = require "gopher.config"
local u = require "gopher._utils"
local r = require "gopher._utils.runner"
local log = require "gopher._utils.log" local log = require "gopher._utils.log"
local iferr = {} local iferr = {}
-- That's Lua implementation: github.com/koron/iferr -- That's Lua implementation: https://github.com/koron/iferr
function iferr.iferr() function iferr.iferr()
local boff = vim.fn.wordcount().cursor_bytes local curb = vim.fn.wordcount().cursor_bytes
local pos = vim.fn.getcurpos()[2] local pos = vim.fn.getcurpos()[2]
local fpath = vim.fn.expand "%"
local data = vim.fn.systemlist((c.commands.iferr .. " -pos " .. boff), vim.fn.bufnr "%") local rs = r.sync({ c.commands.iferr, "-pos", curb }, {
if vim.v.shell_error ~= 0 then stdin = u.readfile_joined(fpath),
if string.find(data[1], "no functions at") then })
vim.print "no function found"
log.warn("iferr: no function at " .. boff) if rs.code ~= 0 then
if string.find(rs.stderr, "no functions at") then
u.notify("iferr: no function at " .. curb, vim.log.levels.ERROR)
log.warn("iferr: no function at " .. curb)
return return
end end
log.error("failed. output: " .. vim.inspect(data)) log.error("ferr: failed. output: " .. rs.stderr)
error("iferr failed: " .. vim.inspect(data)) error("iferr failed: " .. rs.stderr)
end end
vim.fn.append(pos, data) vim.fn.append(pos, u.remove_empty_lines(vim.split(rs.stdout, "\n")))
vim.cmd [[silent normal! j=2j]] vim.cmd [[silent normal! j=2j]]
vim.fn.setpos(".", pos) vim.fn.setpos(".", pos)
end end

View file

@ -43,7 +43,7 @@ local impl = {}
local function get_struct() local function get_struct()
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then if ns == nil then
u.deferred_notify("put cursor on a struct or specify a receiver", vim.log.levels.INFO) u.notify "put cursor on a struct or specify a receiver"
return "" return ""
end end
@ -82,21 +82,14 @@ function impl.impl(...)
recv = string.format("%s %s", recv_name, recv) recv = string.format("%s %s", recv_name, recv)
end end
local output = r.sync(c.impl, { local rs = r.sync { c.impl, "-dir", vim.fn.fnameescape(vim.fn.expand "%:p:h"), recv, iface }
args = { if rs.code ~= 0 then
"-dir", error("failed to implement interface: " .. rs.stderr)
vim.fn.fnameescape(vim.fn.expand "%:p:h" --[[@as string]]),
recv,
iface,
},
on_exit = function(data, status)
if not status == 0 then
error("impl failed: " .. data)
end end
end,
})
local pos = vim.fn.getcurpos()[2] local pos = vim.fn.getcurpos()[2]
local output = u.remove_empty_lines(vim.split(rs.stdout, "\n"))
table.insert(output, 1, "") table.insert(output, 1, "")
vim.fn.append(pos, output) vim.fn.append(pos, output)
end end

View file

@ -1,6 +1,7 @@
local c = require("gopher.config").commands local c = require("gopher.config").commands
local r = require "gopher._utils.runner" local r = require "gopher._utils.runner"
local u = require "gopher._utils" local u = require "gopher._utils"
local log = require "gopher._utils.log"
local installer = {} local installer = {}
local urls = { local urls = {
@ -10,25 +11,42 @@ local urls = {
iferr = "github.com/koron/iferr", iferr = "github.com/koron/iferr",
} }
---@param pkg string ---@param opt vim.SystemCompleted
local function install(pkg) ---@param url string
local url = urls[pkg] .. "@latest" local function handle_intall_exit(opt, url)
r.sync(c.go, { if opt.code ~= 0 then
args = { "install", url }, u.deferred_notify("go install failed: " .. url)
on_exit = function(data, status) log.error("go install failed:", "url", url, "opt", vim.inspect(opt))
if not status == 0 then
error("go install failed: " .. data)
return return
end end
u.notify("installed: " .. url)
end, u.deferred_notify("go install-ed: " .. url)
}) end
---@param url string
local function install(url)
r.async({ c.go, "install", url }, function(opt)
handle_intall_exit(opt, url)
end)
end
---@param url string
local function install_sync(url)
local rs = r.sync { c.go, "install", url }
handle_intall_exit(rs, url)
end end
---Install required go deps ---Install required go deps
function installer.install_deps() ---@param opts? {sync:boolean}
function installer.install_deps(opts)
opts = opts or {}
for pkg, _ in pairs(urls) do for pkg, _ in pairs(urls) do
install(pkg) local url = urls[pkg] .. "@latest"
if opts.sync then
install_sync(url)
else
install(url)
end
end end
end end

View file

@ -38,15 +38,8 @@ local function modify(...)
return return
end end
-- stylua: ignore
local cmd_args = {
"-transform", c.gotag.transform,
"-format", "json",
"-file", fpath,
"-w"
}
-- by struct name of line pos -- by struct name of line pos
local cmd_args = {}
if ns.name == nil then if ns.name == nil then
local _, csrow, _, _ = unpack(vim.fn.getpos ".") local _, csrow, _, _ = unpack(vim.fn.getpos ".")
table.insert(cmd_args, "-line") table.insert(cmd_args, "-line")
@ -67,17 +60,24 @@ local function modify(...)
table.insert(cmd_args, "json") table.insert(cmd_args, "json")
end end
local output = r.sync(c.commands.gomodifytags, { local rs = r.sync {
args = cmd_args, c.commands.gomodifytags,
on_exit = function(data, status) "-transform",
if not status == 0 then c.gotag.transform,
error("gotag failed: " .. data) "-format",
"json",
"-w",
"-file",
fpath,
unpack(cmd_args),
}
if rs.code ~= 0 then
error("failed to set tags " .. rs.stderr)
end end
end,
})
-- decode value -- decode value
local tagged = vim.json.decode(table.concat(output)) local tagged = vim.json.decode(rs.stdout)
if if
tagged.errors ~= nil tagged.errors ~= nil
or tagged.lines == nil or tagged.lines == nil

View file

@ -11,4 +11,5 @@ command! -nargs=* GoGenerate :lua require"gopher".generate(<f-args>)
command! GoCmt :lua require"gopher".comment() command! GoCmt :lua require"gopher".comment()
command! GoIfErr :lua require"gopher".iferr() command! GoIfErr :lua require"gopher".iferr()
command! GoInstallDeps :lua require"gopher".install_deps() command! GoInstallDeps :lua require"gopher".install_deps()
command! GoInstallDepsSync :lua require"gopher".install_deps({ sync = true })
command! GopherLog :lua vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile()) command! GopherLog :lua vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile())

View file

@ -8,17 +8,23 @@ local function install_plug(plugin)
local package_root = root ".tests/site/pack/deps/start/" local package_root = root ".tests/site/pack/deps/start/"
if not vim.uv.fs_stat(package_root .. name) then if not vim.uv.fs_stat(package_root .. name) then
print("Installing " .. plugin) print("Installing " .. plugin)
vim.fn.mkdir(package_root, "p") vim
vim.fn.system { .system({
"git", "git",
"clone", "clone",
"--depth=1", "--depth=1",
"https://github.com/" .. plugin .. ".git", "https://github.com/" .. plugin .. ".git",
package_root .. "/" .. name, package_root .. "/" .. name,
} })
:wait()
end end
end end
install_plug "nvim-lua/plenary.nvim"
install_plug "nvim-treesitter/nvim-treesitter"
install_plug "echasnovski/mini.doc" -- used for docs generation
install_plug "echasnovski/mini.test"
vim.env.XDG_CONFIG_HOME = root ".tests/config" vim.env.XDG_CONFIG_HOME = root ".tests/config"
vim.env.XDG_DATA_HOME = root ".tests/data" vim.env.XDG_DATA_HOME = root ".tests/data"
vim.env.XDG_STATE_HOME = root ".tests/state" vim.env.XDG_STATE_HOME = root ".tests/state"
@ -27,16 +33,16 @@ vim.env.XDG_CACHE_HOME = root ".tests/cache"
vim.cmd [[set runtimepath=$VIMRUNTIME]] vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.opt.runtimepath:append(root()) vim.opt.runtimepath:append(root())
vim.opt.packpath = { root ".tests/site" } vim.opt.packpath = { root ".tests/site" }
vim.notify = print vim.notify = vim.print
install_plug "nvim-lua/plenary.nvim"
install_plug "nvim-treesitter/nvim-treesitter"
install_plug "echasnovski/mini.doc" -- used for docs generation
install_plug "echasnovski/mini.test"
-- install go treesitter parse -- install go treesitter parse
require("nvim-treesitter.install").ensure_installed_sync "go" require("nvim-treesitter.install").ensure_installed_sync "go"
require("gopher").setup {
log_level = vim.log.levels.OFF,
timeout = 4000,
}
-- setup mini.test only when running headless nvim -- setup mini.test only when running headless nvim
if #vim.api.nvim_list_uis() == 0 then if #vim.api.nvim_list_uis() == 0 then
require("mini.test").setup { require("mini.test").setup {

View file

@ -5,4 +5,3 @@ type CloserTest2 struct{}
func (closertest *CloserTest2) Close() error { func (closertest *CloserTest2) Close() error {
panic("not implemented") // TODO: Implement panic("not implemented") // TODO: Implement
} }

View file

@ -4,5 +4,4 @@ func (r Read2) Read(p []byte) (n int, err error) {
panic("not implemented") // TODO: Implement panic("not implemented") // TODO: Implement
} }
type Read2 struct{} type Read2 struct{}

View file

@ -5,4 +5,3 @@ type WriterTest2 struct{}
func (w *WriterTest2) Write(p []byte) (n int, err error) { func (w *WriterTest2) Write(p []byte) (n int, err error) {
panic("not implemented") // TODO: Implement panic("not implemented") // TODO: Implement
} }