refactor of public plugin's api (#37)

* refactor: move all plugin functionality to init.lua

* fix(commands): now it uses correct module paths

* refactor(config): change way how it handles options

* refactor(gotests): use correct config, change way how deps required, fix some typos

* fix(healthchecker): use correct config

* refactor(iferr): change api

* refactor(impl): change api

* refactor(installer): change api

* refactor(struct_tags): change way of importting deps

* refactor(struct_tags): rename M to struct_tags

* run stylua

* refactor(dap): make it all in one file, and make some refactoring

* refactor(_utils): change way how it organizes

* fix: use new _utils api

* refactor(_utils.health): reorganize module

* refactor(_utils.ts): some renameing, moving requires lines

* run stylua
This commit is contained in:
Smirnov Oleksandr 2023-07-19 23:38:23 +03:00 committed by GitHub
parent 94250bb08a
commit 26b41bf68c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 359 additions and 341 deletions

View file

@ -1,14 +1,14 @@
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` ---Run any go commands like `go generate`, `go get`, `go mod`
---@param cmd string ---@param cmd string
---@param ... string|string[] ---@param ... string|string[]
return function(cmd, ...) return function(cmd, ...)
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local args = { ... } local args = { ... }
if #args == 0 then if #args == 0 then
u.notify("please provice any arguments", "error") u.deferred_notify("please provice any arguments", vim.log.levels.ERROR)
return return
end end
@ -29,12 +29,15 @@ return function(cmd, ...)
args = cmd_args, args = cmd_args,
on_exit = function(_, retval) on_exit = function(_, retval)
if retval ~= 0 then if retval ~= 0 then
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") u.deferred_notify(
u.notify(cmd .. " " .. unpack(cmd_args), "debug") "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 return
end end
u.notify("go " .. cmd .. " was success runned", "info") u.deferred_notify("go " .. cmd .. " was success runned", vim.log.levels.INFO)
end, end,
}):start() }):start()
end end

View file

@ -1,17 +1,26 @@
return { local h = vim.health or require "health"
---@param lib string local health = {}
---@return boolean
lualib_is_found = function(lib)
local is_found, _ = pcall(require, lib)
return is_found
end,
---@param bin string health.start = h.start or h.report_start
---@return boolean health.ok = h.ok or h.report_ok
binary_is_found = function(bin) health.warn = h.warn or h.report_warn
if vim.fn.executable(bin) == 1 then health.error = h.error or h.report_error
return true health.info = h.info or h.report_info
end
return false ---@param module string
end, ---@return boolean
} function health.is_lualib_found(module)
local is_found, _ = pcall(require, module)
return is_found
end
---@param bin string
---@return boolean
function health.is_binary_found(bin)
if vim.fn.executable(bin) == 1 then
return true
end
return false
end
return health

View file

@ -1,48 +1,39 @@
---@diagnostic disable: param-type-mismatch local utils = {}
return {
---@param t table
---@return boolean
empty = function(t)
if t == nil then
return true
end
return next(t) == nil ---@param t table
end, ---@return boolean
function utils.is_tbl_empty(t)
if t == nil then
return true
end
return next(t) == nil
end
---@param s string ---@param s string
---@return string ---@return string
rtrim = function(s) function utils.rtrim(s)
local n = #s local n = #s
while n > 0 and s:find("^%s", n) do while n > 0 and s:find("^%s", n) do
n = n - 1 n = n - 1
end end
return s:sub(1, n) return s:sub(1, n)
end, end
---@param msg string ---@param msg string
---@param lvl string|integer ---@param lvl any
notify = function(msg, lvl) function utils.deferred_notify(msg, lvl)
local l vim.defer_fn(function()
if lvl == "error" or lvl == 4 then vim.notify(msg, lvl)
l = vim.log.levels.ERROR end, 0)
elseif lvl == "info" or lvl == 2 then end
l = vim.log.levels.INFO
elseif lvl == "debug" or lvl == 1 then
l = vim.log.levels.DEBUG
end
vim.defer_fn(function() -- safe require
vim.notify(msg, l) ---@param module string module name
end, 0) function utils.sreq(module)
end, local ok, m = pcall(require, module)
assert(ok, string.format("gopher.nvim dependency error: %s not installed", module))
return m
end
---safe require return utils
---@param name string module name
sreq = function(name)
local ok, m = pcall(require, name)
assert(ok, string.format("gopher.nvim dependency error: %s not installed", name))
return m
end,
}

View file

@ -1,7 +1,7 @@
---@diagnostic disable: param-type-mismatch ---@diagnostic disable: param-type-mismatch
local nodes = require "gopher._utils.ts.nodes" local nodes = require "gopher._utils.ts.nodes"
local u = require "gopher._utils" local u = require "gopher._utils"
local M = { local ts = {
querys = { querys = {
struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]],
em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]],
@ -27,14 +27,14 @@ end
---@param bufnr string|nil ---@param bufnr string|nil
---@param do_notify boolean|nil ---@param do_notify boolean|nil
---@return table|nil ---@return table|nil
function M.get_struct_node_at_pos(row, col, bufnr, do_notify) function ts.get_struct_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true local notify = do_notify or true
local query = M.querys.struct_block .. " " .. M.querys.em_struct_block local query = ts.querys.struct_block .. " " .. ts.querys.em_struct_block
local bufn = bufnr or vim.api.nvim_get_current_buf() local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then if ns == nil then
if notify then if notify then
u.notify("struct not found", "warn") u.deferred_notify("struct not found", vim.log.levels.WARN)
end end
else else
return ns[#ns] return ns[#ns]
@ -46,14 +46,14 @@ end
---@param bufnr string|nil ---@param bufnr string|nil
---@param do_notify boolean|nil ---@param do_notify boolean|nil
---@return table|nil ---@return table|nil
function M.get_func_method_node_at_pos(row, col, bufnr, do_notify) function ts.get_func_method_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true local notify = do_notify or true
local query = M.querys.func .. " " .. M.querys.method_name local query = ts.querys.func .. " " .. ts.querys.method_name
local bufn = bufnr or vim.api.nvim_get_current_buf() local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then if ns == nil then
if notify then if notify then
u.notify("function not found", "warn") u.deferred_notify("function not found", vim.log.levels.WARN)
end end
else else
return ns[#ns] return ns[#ns]
@ -65,16 +65,16 @@ end
---@param bufnr string|nil ---@param bufnr string|nil
---@param do_notify boolean|nil ---@param do_notify boolean|nil
---@return table|nil ---@return table|nil
function M.get_package_node_at_pos(row, col, bufnr, do_notify) function ts.get_package_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true local notify = do_notify or true
-- stylua: ignore -- stylua: ignore
if row > 10 then return end if row > 10 then return end
local query = M.querys.package local query = ts.querys.package
local bufn = bufnr or vim.api.nvim_get_current_buf() local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then if ns == nil then
if notify then if notify then
u.notify("package not found", "warn") u.deferred_notify("package not found", vim.log.levels.WARN)
return nil return nil
end end
else else
@ -87,18 +87,18 @@ end
---@param bufnr string|nil ---@param bufnr string|nil
---@param do_notify boolean|nil ---@param do_notify boolean|nil
---@return table|nil ---@return table|nil
function M.get_interface_node_at_pos(row, col, bufnr, do_notify) function ts.get_interface_node_at_pos(row, col, bufnr, do_notify)
local notify = do_notify or true local notify = do_notify or true
local query = M.querys.interface local query = ts.querys.interface
local bufn = bufnr or vim.api.nvim_get_current_buf() local bufn = bufnr or vim.api.nvim_get_current_buf()
local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col)
if ns == nil then if ns == nil then
if notify then if notify then
u.notify("interface not found", "warn") u.deferred_notify("interface not found", vim.log.levels.WARN)
end end
else else
return ns[#ns] return ns[#ns]
end end
end end
return M return ts

View file

@ -1,3 +1,7 @@
local ts_query = require "nvim-treesitter.query"
local parsers = require "nvim-treesitter.parsers"
local locals = require "nvim-treesitter.locals"
local u = require "gopher._utils"
local M = {} local M = {}
local function intersects(row, col, sRow, sCol, eRow, eCol) local function intersects(row, col, sRow, sCol, eRow, eCol)
@ -53,10 +57,6 @@ end
---@param pos_row string ---@param pos_row string
---@return string ---@return string
function M.get_all_nodes(query, lang, _, bufnr, pos_row, _) function M.get_all_nodes(query, lang, _, bufnr, pos_row, _)
local ts_query = require "nvim-treesitter.query"
local parsers = require "nvim-treesitter.parsers"
local locals = require "nvim-treesitter.locals"
bufnr = bufnr or 0 bufnr = bufnr or 0
pos_row = pos_row or 30000 pos_row = pos_row or 30000
@ -113,8 +113,6 @@ end
---@param col string ---@param col string
---@return table ---@return table
function M.nodes_at_cursor(query, default, bufnr, row, col) function M.nodes_at_cursor(query, default, bufnr, row, col)
local u = require "gopher._utils"
bufnr = bufnr or vim.api.nvim_get_current_buf() bufnr = bufnr or vim.api.nvim_get_current_buf()
local ft = vim.api.nvim_buf_get_option(bufnr, "ft") local ft = vim.api.nvim_buf_get_option(bufnr, "ft")
if row == nil or col == nil then if row == nil or col == nil then
@ -123,13 +121,19 @@ function M.nodes_at_cursor(query, default, bufnr, row, col)
local nodes = M.get_all_nodes(query, ft, default, bufnr, row, col) local nodes = M.get_all_nodes(query, ft, default, bufnr, row, col)
if nodes == nil then if nodes == nil then
u.notify("Unable to find any nodes. Place your cursor on a go symbol and try again", "debug") u.deferred_notify(
"Unable to find any nodes. Place your cursor on a go symbol and try again",
vim.log.levels.DEBUG
)
return nil return nil
end end
nodes = M.sort_nodes(M.intersect_nodes(nodes, row, col)) nodes = M.sort_nodes(M.intersect_nodes(nodes, row, col))
if nodes == nil or #nodes == 0 then if nodes == nil or #nodes == 0 then
u.notify("Unable to find any nodes at pos. " .. tostring(row) .. ":" .. tostring(col), "debug") u.deferred_notify(
"Unable to find any nodes at pos. " .. tostring(row) .. ":" .. tostring(col),
vim.log.levels.DEBUG
)
return nil return nil
end end

View file

@ -1,29 +0,0 @@
local API = {}
local tags = require "gopher.struct_tags"
local tests = require "gopher.gotests"
local cmd = require "gopher._utils.commands"
API.install_deps = require "gopher.installer"
API.tags_add = tags.add
API.tags_rm = tags.remove
API.impl = require "gopher.impl"
API.iferr = require "gopher.iferr"
API.comment = require "gopher.comment"
API.test_add = tests.func_test
API.test_exported = tests.all_exported_tests
API.tests_all = tests.all_tests
API.get = function(...)
cmd("get", ...)
end
API.mod = function(...)
cmd("mod", ...)
end
API.generate = function(...)
cmd("generate", ...)
end
API.work = function(...)
cmd("work", ...)
end
return API

View file

@ -1,33 +1,27 @@
---@class Config ---@class gopher.Config
---@field commands ConfigCommands local config = {}
---@class ConfigCommands ---@class gopher.Config
---@field go string ---@field commands gopher.ConfigCommands
---@field gomodifytags string local default_config = {
---@field gotests string ---@class gopher.ConfigCommands
---@field impl string commands = {
---@field iferr string go = "go",
---@field dlv string gomodifytags = "gomodifytags",
gotests = "gotests",
local M = { impl = "impl",
---@type Config iferr = "iferr",
config = { dlv = "dlv",
---set custom commands for tools
commands = {
go = "go",
gomodifytags = "gomodifytags",
gotests = "gotests",
impl = "impl",
iferr = "iferr",
dlv = "dlv",
},
}, },
} }
---Plugin setup function ---@param user_config gopher.Config|nil
---@param opts Config user config function config.setup(user_config)
function M.setup(opts) config = vim.tbl_deep_extend("force", {}, default_config, user_config or {})
M.config = vim.tbl_deep_extend("force", M.config, opts or {})
end end
return M -- setup ifself, needs for ability to get
-- default config without calling .setup()
config.setup()
return config

116
lua/gopher/dap.lua Normal file
View file

@ -0,0 +1,116 @@
local u = require "gopher._utils"
local dap = {}
dap.adapter = function(callback, config)
local host = config.host or "127.0.0.1"
local port = config.port or "38697"
local addr = string.format("%s:%s", host, port)
local handle, pid_or_err
local stdout = assert(vim.loop.new_pipe(false))
local opts = {
stdio = { nil, stdout },
args = { "dap", "-l", addr },
detached = true,
}
handle, pid_or_err = vim.loop.spawn("dlv", opts, function(status)
if not stdout or not handle then
return
end
stdout:close()
handle:close()
if status ~= 0 then
print("dlv exited with code", status)
end
end)
assert(handle, "Error running dlv: " .. tostring(pid_or_err))
if stdout then
stdout:read_start(function(err, chunk)
assert(not err, err)
if chunk then
vim.schedule(function()
require("dap.repl").append(chunk)
end)
end
end)
end
-- wait for delve to start
vim.defer_fn(function()
callback { type = "server", host = "127.0.0.1", port = port }
end, 100)
end
local function args_input()
vim.ui.input({ prompt = "Args: " }, function(input)
return vim.split(input or "", " ")
end)
end
local function get_arguments()
local co = coroutine.running()
if co then
return coroutine.create(function()
local args = args_input()
coroutine.resume(co, args)
end)
else
return args_input()
end
end
dap.configuration = {
{
type = "go",
name = "Debug",
request = "launch",
program = "${file}",
},
{
type = "go",
name = "Debug (Arguments)",
request = "launch",
program = "${file}",
args = get_arguments,
},
{
type = "go",
name = "Debug Package",
request = "launch",
program = "${fileDirname}",
},
{
type = "go",
name = "Attach",
mode = "local",
request = "attach",
processId = require("dap.utils").pick_process,
},
{
type = "go",
name = "Debug test",
request = "launch",
mode = "test",
program = "${file}",
},
{
type = "go",
name = "Debug test (go.mod)",
request = "launch",
mode = "test",
program = "./${relativeFileDirname}",
},
}
---setup `nvim-dap` for Go
function dap.setup()
local d = u.sreq "dap"
d.adapters.go = dap.adapter
d.configurations.go = dap.configuration
end
return dap

View file

@ -1,98 +0,0 @@
---@diagnostic disable: param-type-mismatch
local function get_arguments()
local function get()
vim.ui.input({ prompt = "Args: " }, function(input)
return vim.split(input or "", " ") ---@diagnostic disable-line: missing-parameter
end)
end
local co = coroutine.running()
if co then
return coroutine.create(function()
local args = get()
coroutine.resume(co, args)
end)
else
return get()
end
end
return {
adapter = function(callback, config)
local handle, pid_or_err
local stdout = vim.loop.new_pipe(false)
local host = config.host or "127.0.0.1"
local port = config.port or "38697"
local addr = string.format("%s:%s", host, port)
local opts = {
stdio = { nil, stdout },
args = { "dap", "-l", addr },
detached = true,
}
handle, pid_or_err = vim.loop.spawn("dlv", opts, function(code)
stdout:close()
handle:close()
if code ~= 0 then
print("dlv exited with code", code)
end
end)
assert(handle, "Error running dlv: " .. tostring(pid_or_err))
stdout:read_start(function(err, chunk)
assert(not err, err)
if chunk then
vim.schedule(function()
require("dap.repl").append(chunk)
end)
end
end)
-- Wait for delve to start
vim.defer_fn(function()
callback { type = "server", host = "127.0.0.1", port = port }
end, 100)
end,
configuration = {
{
type = "go",
name = "Debug",
request = "launch",
program = "${file}",
},
{
type = "go",
name = "Debug (Arguments)",
request = "launch",
program = "${file}",
args = get_arguments,
},
{
type = "go",
name = "Debug Package",
request = "launch",
program = "${fileDirname}",
},
{
type = "go",
name = "Attach",
mode = "local",
request = "attach",
processId = require("dap.utils").pick_process,
},
{
type = "go",
name = "Debug test",
request = "launch",
mode = "test",
program = "${file}",
},
{
type = "go",
name = "Debug test (go.mod)",
request = "launch",
mode = "test",
program = "./${relativeFileDirname}",
},
},
}

View file

@ -1,13 +0,0 @@
local M = {}
---setup nvim-dap for golang using
function M.setup()
local cfg = require "gopher.dap.config"
local u = require "gopher._utils"
local dap = u.sreq "dap"
dap.adapters.go = cfg.adapter
dap.configurations.go = cfg.configuration
end
return M

View file

@ -1,21 +1,24 @@
local c = require("gopher.config").commands
local u = require "gopher._utils" local u = require "gopher._utils"
local M = {} local ts_utils = require "gopher._utils.ts"
local Job = require "plenary.job"
local gotests = {}
---@param cmd_args table ---@param cmd_args table
local function run(cmd_args) local function run(cmd_args)
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
Job:new({ Job:new({
command = c.gotests, command = c.gotests,
args = cmd_args, args = cmd_args,
on_exit = function(_, retval) on_exit = function(_, retval)
if retval ~= 0 then if retval ~= 0 then
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") u.deferred_notify(
"command '" .. c.gotests .. " " .. unpack(cmd_args) .. "' exited with code " .. retval,
vim.log.levels.ERROR
)
return return
end end
u.notify("unit test(s) generated", "info") u.deferred_notify("unit test(s) generated", vim.log.levels.INFO)
end, end,
}):start() }):start()
end end
@ -30,12 +33,10 @@ end
---generate unit test for one function ---generate unit test for one function
---@param parallel boolean ---@param parallel boolean
function M.func_test(parallel) function gotests.func_test(parallel)
local ts_utils = require "gopher._utils.ts"
local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) 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 if ns == nil or ns.name == nil then
u.notify("cursor on func/method and execute the command again", "info") u.deferred_notify("cursor on func/method and execute the command again", vim.log.levels.INFO)
return return
end end
@ -49,7 +50,7 @@ end
---generate unit tests for all functions in current file ---generate unit tests for all functions in current file
---@param parallel boolean ---@param parallel boolean
function M.all_tests(parallel) function gotests.all_tests(parallel)
local cmd_args = { "-all" } local cmd_args = { "-all" }
if parallel then if parallel then
table.insert(cmd_args, "-parallel") table.insert(cmd_args, "-parallel")
@ -60,7 +61,7 @@ end
---generate unit tests for all exported functions ---generate unit tests for all exported functions
---@param parallel boolean ---@param parallel boolean
function M.all_exported_tests(parallel) function gotests.all_exported_tests(parallel)
local cmd_args = {} local cmd_args = {}
if parallel then if parallel then
table.insert(cmd_args, "-parallel") table.insert(cmd_args, "-parallel")
@ -70,4 +71,4 @@ function M.all_exported_tests(parallel)
add_test(cmd_args) add_test(cmd_args)
end end
return M return gotests

View file

@ -1,16 +1,7 @@
local health = {} local health = {}
local cmd = require("gopher.config").config.commands local cmd = require("gopher.config").commands
local u = require "gopher._utils.health" local u = require "gopher._utils.health"
local _h = vim.health or require "health"
local h = {
start = _h.start or _h.report_start,
ok = _h.ok or _h.report_ok,
warn = _h.warn or _h.report_warn,
error = _h.error or _h.report_error,
info = _h.info or _h.report_info,
}
local deps = { local deps = {
plugin = { plugin = {
{ lib = "dap", msg = "required for `gopher.dap`", optional = true }, { lib = "dap", msg = "required for `gopher.dap`", optional = true },
@ -36,29 +27,29 @@ local deps = {
} }
function health.check() function health.check()
h.start "required plugins" u.start "required plugins"
for _, plugin in ipairs(deps.plugin) do for _, plugin in ipairs(deps.plugin) do
if u.lualib_is_found(plugin.lib) then if u.is_lualib_found(plugin.lib) then
h.ok(plugin.lib .. " installed") u.ok(plugin.lib .. " installed")
else else
if plugin.optional then if plugin.optional then
h.warn(plugin.lib .. " not found, " .. plugin.msg) u.warn(plugin.lib .. " not found, " .. plugin.msg)
else else
h.error(plugin.lib .. " not found, " .. plugin.msg) u.error(plugin.lib .. " not found, " .. plugin.msg)
end end
end end
end end
h.start "required binaries" u.start "required binaries"
h.info "all those binaries can be installed by `:GoInstallDeps`" u.info "all those binaries can be installed by `:GoInstallDeps`"
for _, bin in ipairs(deps.bin) do for _, bin in ipairs(deps.bin) do
if u.binary_is_found(bin.bin) then if u.is_lualib_found(bin.bin) then
h.ok(bin.bin .. " installed") u.ok(bin.bin .. " installed")
else else
if bin.optional then if bin.optional then
h.warn(bin.bin .. " not found, " .. bin.msg) u.warn(bin.bin .. " not found, " .. bin.msg)
else else
h.error(bin.bin .. " not found, " .. bin.msg) u.error(bin.bin .. " not found, " .. bin.msg)
end end
end end
end end

View file

@ -1,16 +1,18 @@
---Add iferr declaration local c = require("gopher.config").commands
---That's Lua of vimscript implementation of: local u = require "gopher._utils"
---github.com/koron/iferr local iferr = {}
return function()
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
-- That's Lua of vimscript implementation of: github.com/koron/iferr
iferr.iferr = function()
local boff = vim.fn.wordcount().cursor_bytes local boff = vim.fn.wordcount().cursor_bytes
local cmd = (c.iferr .. " -pos " .. boff) local cmd = (c.iferr .. " -pos " .. boff)
local data = vim.fn.systemlist(cmd, vim.fn.bufnr "%") local data = vim.fn.systemlist(cmd, vim.fn.bufnr "%")
if vim.v.shell_error ~= 0 then if vim.v.shell_error ~= 0 then
u.notify("command " .. cmd .. " exited with code " .. vim.v.shell_error, "error") u.deferred_notify(
"command " .. cmd .. " exited with code " .. vim.v.shell_error,
vim.log.levels.ERROR
)
return return
end end
@ -19,3 +21,5 @@ return function()
vim.cmd [[silent normal! j=2j]] vim.cmd [[silent normal! j=2j]]
vim.fn.setpos(".", pos) vim.fn.setpos(".", pos)
end end
return iferr

View file

@ -1,12 +1,14 @@
local c = require("gopher.config").commands
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils" local u = require "gopher._utils"
local impl = {}
---@return string ---@return string
local function get_struct() local function get_struct()
local ts_utils = require "gopher._utils.ts"
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.notify("put cursor on a struct or specify a receiver", "info") u.deferred_notify("put cursor on a struct or specify a receiver", vim.log.levels.INFO)
return "" return ""
end end
@ -18,10 +20,7 @@ local function get_struct()
return ns.name return ns.name
end end
return function(...) function impl.impl(...)
local c = require("gopher.config").config.commands
local Job = require "plenary.job"
local args = { ... } local args = { ... }
local iface, recv_name = "", "" local iface, recv_name = "", ""
local recv = get_struct() local recv = get_struct()
@ -30,7 +29,7 @@ return function(...)
iface = vim.fn.input "impl: generating method stubs for interface: " iface = vim.fn.input "impl: generating method stubs for interface: "
vim.cmd "redraw!" vim.cmd "redraw!"
if iface == "" then if iface == "" then
u.notify("usage: GoImpl f *File io.Reader", "info") u.deferred_notify("usage: GoImpl f *File io.Reader", vim.log.levels.INFO)
return return
end end
elseif #args == 1 then -- :GoImpl io.Reader elseif #args == 1 then -- :GoImpl io.Reader
@ -61,7 +60,10 @@ return function(...)
args = cmd_args, args = cmd_args,
on_exit = function(data, retval) on_exit = function(data, retval)
if retval ~= 0 then if retval ~= 0 then
u.notify("command 'impl " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") u.deferred_notify(
"command '" .. c.impl .. " " .. unpack(cmd_args) .. "' exited with code " .. retval,
vim.log.levels.ERROR
)
return return
end end
@ -73,3 +75,5 @@ return function(...)
table.insert(res_data, 1, "") table.insert(res_data, 1, "")
vim.fn.append(pos, res_data) vim.fn.append(pos, res_data)
end end
return impl

View file

@ -1,5 +1,35 @@
local GOPHER = {} local tags = require "gopher.struct_tags"
local tests = require "gopher.gotests"
local uc = require "gopher._utils.commands"
local gopher = {}
GOPHER.setup = require("gopher.config").setup gopher.setup = require("gopher.config").setup
gopher.install_deps = require("gopher.installer").install_deps
gopher.impl = require("gopher.impl").impl
gopher.iferr = require("gopher.iferr").iferr
gopher.comment = require "gopher.comment"
return GOPHER gopher.tags_add = tags.add
gopher.tags_rm = tags.remove
gopher.test_add = tests.func_test
gopher.test_exported = tests.all_exported_tests
gopher.tests_all = tests.all_tests
gopher.get = function(...)
uc("get", ...)
end
gopher.mod = function(...)
uc("mod", ...)
end
gopher.generate = function(...)
uc("generate", ...)
end
gopher.work = function(...)
uc("work", ...)
end
return gopher

View file

@ -1,3 +1,8 @@
local Job = require "plenary.job"
local c = require("gopher.config").commands
local u = require "gopher._utils"
local installer = {}
local urls = { local urls = {
gomodifytags = "github.com/fatih/gomodifytags", gomodifytags = "github.com/fatih/gomodifytags",
impl = "github.com/josharian/impl", impl = "github.com/josharian/impl",
@ -8,28 +13,30 @@ local urls = {
---@param pkg string ---@param pkg string
local function install(pkg) local function install(pkg)
local Job = require "plenary.job"
local u = require "gopher._utils"
local url = urls[pkg] .. "@latest" local url = urls[pkg] .. "@latest"
Job:new({ Job:new({
command = "go", command = c.go,
args = { "install", url }, args = { "install", url },
on_exit = function(_, retval) on_exit = function(_, retval)
if retval ~= 0 then if retval ~= 0 then
u.notify("command 'go install " .. url .. "' exited with code " .. retval, "error") u.deferred_notify(
"command 'go install " .. url .. "' exited with code " .. retval,
vim.log.levels.ERROR
)
return return
end end
u.notify("install " .. url .. " finished", "info ") u.deferred_notify("install " .. url .. " finished", vim.log.levels.INFO)
end, end,
}):start() }):start()
end end
---Install required go deps ---Install required go deps
return function() function installer.install_deps()
for pkg, _ in pairs(urls) do for pkg, _ in pairs(urls) do
install(pkg) install(pkg)
end end
end end
return installer

View file

@ -1,11 +1,10 @@
local M = {} local ts_utils = require "gopher._utils.ts"
local Job = require "plenary.job"
local c = require("gopher.config").commands
local u = require "gopher._utils"
local struct_tags = {}
local function modify(...) local function modify(...)
local ts_utils = require "gopher._utils.ts"
local Job = require "plenary.job"
local c = require("gopher.config").config.commands
local u = require "gopher._utils"
local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter
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
@ -47,9 +46,14 @@ local function modify(...)
args = cmd_args, args = cmd_args,
on_exit = function(data, retval) on_exit = function(data, retval)
if retval ~= 0 then if retval ~= 0 then
u.notify( u.deferred_notify(
"command 'gomodifytags " .. unpack(cmd_args) .. "' exited with code " .. retval, "command '"
"error" .. c.gomodifytags
.. " "
.. unpack(cmd_args)
.. "' exited with code "
.. retval,
vim.log.levels.ERROR
) )
return return
end end
@ -66,7 +70,7 @@ local function modify(...)
or tagged["start"] == nil or tagged["start"] == nil
or tagged["start"] == 0 or tagged["start"] == 0
then then
u.notify("failed to set tags " .. vim.inspect(tagged), "error") u.deferred_notify("failed to set tags " .. vim.inspect(tagged), vim.log.levels.ERROR)
end end
for i, v in ipairs(tagged.lines) do for i, v in ipairs(tagged.lines) do
@ -86,7 +90,7 @@ end
---add tags to struct under cursor ---add tags to struct under cursor
---@param ... unknown ---@param ... unknown
function M.add(...) function struct_tags.add(...)
local arg = { ... } local arg = { ... }
if #arg == nil or arg == "" then if #arg == nil or arg == "" then
arg = { "json" } arg = { "json" }
@ -102,7 +106,7 @@ end
---remove tags to struct under cursor ---remove tags to struct under cursor
---@param ... unknown ---@param ... unknown
function M.remove(...) function struct_tags.remove(...)
local arg = { ... } local arg = { ... }
if #arg == nil or arg == "" then if #arg == nil or arg == "" then
arg = { "json" } arg = { "json" }
@ -116,4 +120,4 @@ function M.remove(...)
modify(unpack(cmd_args)) modify(unpack(cmd_args))
end end
return M return struct_tags

View file

@ -1,13 +1,13 @@
command! -nargs=* GoTagAdd :lua require"gopher.api".tags_add(<f-args>) command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>)
command! -nargs=* GoTagRm :lua require"gopher.api".tags_rm(<f-args>) command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>)
command! -nargs=* GoTestAdd :lua require"gopher.api".test_add(<f-args>) command! -nargs=* GoTestAdd :lua require"gopher".test_add(<f-args>)
command! -nargs=* GoTestsAll :lua require"gopher.api".tests_all(<f-args>) command! -nargs=* GoTestsAll :lua require"gopher".tests_all(<f-args>)
command! -nargs=* GoTestsExp :lua require"gopher.api".test_exported(<f-args>) command! -nargs=* GoTestsExp :lua require"gopher".test_exported(<f-args>)
command! -nargs=* GoMod :lua require"gopher.api".mod(<f-args>) command! -nargs=* GoMod :lua require"gopher".mod(<f-args>)
command! -nargs=* GoGet :lua require"gopher.api".get(<f-args>) command! -nargs=* GoGet :lua require"gopher".get(<f-args>)
command! -nargs=* GoWork :lua require"gopher.api".work(<f-args>) command! -nargs=* GoWork :lua require"gopher".work(<f-args>)
command! -nargs=* GoImpl :lua require"gopher.api".impl(<f-args>) command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>)
command! -nargs=* GoGenerate :lua require"gopher.api".generate(<f-args>) command! -nargs=* GoGenerate :lua require"gopher".generate(<f-args>)
command! GoCmt :lua require"gopher.api".comment() command! GoCmt :lua require"gopher".comment()
command! GoIfErr :lua require"gopher.api".iferr() command! GoIfErr :lua require"gopher".iferr()
command! GoInstallDeps :lua require"gopher.api".install_deps() command! GoInstallDeps :lua require"gopher".install_deps()