diff --git a/lua/gopher/_utils/commands.lua b/lua/gopher/_utils/commands.lua index 33926a4..447a3af 100644 --- a/lua/gopher/_utils/commands.lua +++ b/lua/gopher/_utils/commands.lua @@ -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` ---@param cmd string ---@param ... string|string[] return function(cmd, ...) - local Job = require "plenary.job" - local c = require("gopher.config").config.commands - local u = require "gopher._utils" - local args = { ... } if #args == 0 then - u.notify("please provice any arguments", "error") + u.deferred_notify("please provice any arguments", vim.log.levels.ERROR) return end @@ -29,12 +29,15 @@ return function(cmd, ...) args = cmd_args, on_exit = function(_, retval) if retval ~= 0 then - u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") - u.notify(cmd .. " " .. unpack(cmd_args), "debug") + 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.notify("go " .. cmd .. " was success runned", "info") + u.deferred_notify("go " .. cmd .. " was success runned", vim.log.levels.INFO) end, }):start() end diff --git a/lua/gopher/_utils/health.lua b/lua/gopher/_utils/health.lua index 5b12026..b651fe4 100644 --- a/lua/gopher/_utils/health.lua +++ b/lua/gopher/_utils/health.lua @@ -1,17 +1,26 @@ -return { - ---@param lib string - ---@return boolean - lualib_is_found = function(lib) - local is_found, _ = pcall(require, lib) - return is_found - end, +local h = vim.health or require "health" +local health = {} - ---@param bin string - ---@return boolean - binary_is_found = function(bin) - if vim.fn.executable(bin) == 1 then - return true - end - return false - end, -} +health.start = h.start or h.report_start +health.ok = h.ok or h.report_ok +health.warn = h.warn or h.report_warn +health.error = h.error or h.report_error +health.info = h.info or h.report_info + +---@param module string +---@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 diff --git a/lua/gopher/_utils/init.lua b/lua/gopher/_utils/init.lua index 190574f..a083a78 100644 --- a/lua/gopher/_utils/init.lua +++ b/lua/gopher/_utils/init.lua @@ -1,48 +1,39 @@ ----@diagnostic disable: param-type-mismatch -return { - ---@param t table - ---@return boolean - empty = function(t) - if t == nil then - return true - end +local utils = {} - return next(t) == nil - end, +---@param t table +---@return boolean +function utils.is_tbl_empty(t) + if t == nil then + return true + end + return next(t) == nil +end - ---@param s string - ---@return string - rtrim = function(s) - local n = #s - while n > 0 and s:find("^%s", n) do - n = n - 1 - end +---@param s string +---@return string +function utils.rtrim(s) + local n = #s + while n > 0 and s:find("^%s", n) do + n = n - 1 + end - return s:sub(1, n) - end, + return s:sub(1, n) +end - ---@param msg string - ---@param lvl string|integer - notify = function(msg, lvl) - local l - if lvl == "error" or lvl == 4 then - l = vim.log.levels.ERROR - elseif lvl == "info" or lvl == 2 then - l = vim.log.levels.INFO - elseif lvl == "debug" or lvl == 1 then - l = vim.log.levels.DEBUG - end +---@param msg string +---@param lvl any +function utils.deferred_notify(msg, lvl) + vim.defer_fn(function() + vim.notify(msg, lvl) + end, 0) +end - vim.defer_fn(function() - vim.notify(msg, l) - end, 0) - end, +-- safe require +---@param module string module name +function utils.sreq(module) + local ok, m = pcall(require, module) + assert(ok, string.format("gopher.nvim dependency error: %s not installed", module)) + return m +end - ---safe require - ---@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, -} +return utils diff --git a/lua/gopher/_utils/ts/init.lua b/lua/gopher/_utils/ts/init.lua index ee24f49..aba3a94 100644 --- a/lua/gopher/_utils/ts/init.lua +++ b/lua/gopher/_utils/ts/init.lua @@ -1,7 +1,7 @@ ---@diagnostic disable: param-type-mismatch local nodes = require "gopher._utils.ts.nodes" local u = require "gopher._utils" -local M = { +local ts = { querys = { 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]], @@ -27,14 +27,14 @@ end ---@param bufnr string|nil ---@param do_notify boolean|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 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 ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) if ns == nil then if notify then - u.notify("struct not found", "warn") + u.deferred_notify("struct not found", vim.log.levels.WARN) end else return ns[#ns] @@ -46,14 +46,14 @@ end ---@param bufnr string|nil ---@param do_notify boolean|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 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 ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) if ns == nil then if notify then - u.notify("function not found", "warn") + u.deferred_notify("function not found", vim.log.levels.WARN) end else return ns[#ns] @@ -65,16 +65,16 @@ end ---@param bufnr string|nil ---@param do_notify boolean|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 -- stylua: ignore 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 ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) if ns == nil then if notify then - u.notify("package not found", "warn") + u.deferred_notify("package not found", vim.log.levels.WARN) return nil end else @@ -87,18 +87,18 @@ end ---@param bufnr string|nil ---@param do_notify boolean|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 query = M.querys.interface + local query = ts.querys.interface local bufn = bufnr or vim.api.nvim_get_current_buf() local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) if ns == nil then if notify then - u.notify("interface not found", "warn") + u.deferred_notify("interface not found", vim.log.levels.WARN) end else return ns[#ns] end end -return M +return ts diff --git a/lua/gopher/_utils/ts/nodes.lua b/lua/gopher/_utils/ts/nodes.lua index 87286a2..e9c730d 100644 --- a/lua/gopher/_utils/ts/nodes.lua +++ b/lua/gopher/_utils/ts/nodes.lua @@ -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 function intersects(row, col, sRow, sCol, eRow, eCol) @@ -53,10 +57,6 @@ end ---@param pos_row string ---@return string 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 pos_row = pos_row or 30000 @@ -113,8 +113,6 @@ end ---@param col string ---@return table function M.nodes_at_cursor(query, default, bufnr, row, col) - local u = require "gopher._utils" - bufnr = bufnr or vim.api.nvim_get_current_buf() local ft = vim.api.nvim_buf_get_option(bufnr, "ft") 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) 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 end nodes = M.sort_nodes(M.intersect_nodes(nodes, row, col)) 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 end diff --git a/lua/gopher/api.lua b/lua/gopher/api.lua deleted file mode 100644 index 6e854af..0000000 --- a/lua/gopher/api.lua +++ /dev/null @@ -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 diff --git a/lua/gopher/config.lua b/lua/gopher/config.lua index a467f83..36b5773 100644 --- a/lua/gopher/config.lua +++ b/lua/gopher/config.lua @@ -1,33 +1,27 @@ ----@class Config ----@field commands ConfigCommands +---@class gopher.Config +local config = {} ----@class ConfigCommands ----@field go string ----@field gomodifytags string ----@field gotests string ----@field impl string ----@field iferr string ----@field dlv string - -local M = { - ---@type Config - config = { - ---set custom commands for tools - commands = { - go = "go", - gomodifytags = "gomodifytags", - gotests = "gotests", - impl = "impl", - iferr = "iferr", - dlv = "dlv", - }, +---@class gopher.Config +---@field commands gopher.ConfigCommands +local default_config = { + ---@class gopher.ConfigCommands + commands = { + go = "go", + gomodifytags = "gomodifytags", + gotests = "gotests", + impl = "impl", + iferr = "iferr", + dlv = "dlv", }, } ----Plugin setup function ----@param opts Config user config -function M.setup(opts) - M.config = vim.tbl_deep_extend("force", M.config, opts or {}) +---@param user_config gopher.Config|nil +function config.setup(user_config) + config = vim.tbl_deep_extend("force", {}, default_config, user_config or {}) end -return M +-- setup ifself, needs for ability to get +-- default config without calling .setup() +config.setup() + +return config diff --git a/lua/gopher/dap.lua b/lua/gopher/dap.lua new file mode 100644 index 0000000..af16438 --- /dev/null +++ b/lua/gopher/dap.lua @@ -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 diff --git a/lua/gopher/dap/config.lua b/lua/gopher/dap/config.lua deleted file mode 100644 index 80b2196..0000000 --- a/lua/gopher/dap/config.lua +++ /dev/null @@ -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}", - }, - }, -} diff --git a/lua/gopher/dap/init.lua b/lua/gopher/dap/init.lua deleted file mode 100644 index 9b0b626..0000000 --- a/lua/gopher/dap/init.lua +++ /dev/null @@ -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 diff --git a/lua/gopher/gotests.lua b/lua/gopher/gotests.lua index 724971c..cf9a0f1 100644 --- a/lua/gopher/gotests.lua +++ b/lua/gopher/gotests.lua @@ -1,21 +1,24 @@ +local c = require("gopher.config").commands 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 local function run(cmd_args) - local Job = require "plenary.job" - local c = require("gopher.config").config.commands - Job:new({ command = c.gotests, args = cmd_args, on_exit = function(_, retval) 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 end - u.notify("unit test(s) generated", "info") + u.deferred_notify("unit test(s) generated", vim.log.levels.INFO) end, }):start() end @@ -30,12 +33,10 @@ end ---generate unit test for one function ---@param parallel boolean -function M.func_test(parallel) - local ts_utils = require "gopher._utils.ts" - +function gotests.func_test(parallel) 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.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 end @@ -49,7 +50,7 @@ end ---generate unit tests for all functions in current file ---@param parallel boolean -function M.all_tests(parallel) +function gotests.all_tests(parallel) local cmd_args = { "-all" } if parallel then table.insert(cmd_args, "-parallel") @@ -60,7 +61,7 @@ end ---generate unit tests for all exported functions ---@param parallel boolean -function M.all_exported_tests(parallel) +function gotests.all_exported_tests(parallel) local cmd_args = {} if parallel then table.insert(cmd_args, "-parallel") @@ -70,4 +71,4 @@ function M.all_exported_tests(parallel) add_test(cmd_args) end -return M +return gotests diff --git a/lua/gopher/health.lua b/lua/gopher/health.lua index 935fbac..4b3f3a2 100644 --- a/lua/gopher/health.lua +++ b/lua/gopher/health.lua @@ -1,16 +1,7 @@ local health = {} -local cmd = require("gopher.config").config.commands +local cmd = require("gopher.config").commands 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 = { plugin = { { lib = "dap", msg = "required for `gopher.dap`", optional = true }, @@ -36,29 +27,29 @@ local deps = { } function health.check() - h.start "required plugins" + u.start "required plugins" for _, plugin in ipairs(deps.plugin) do - if u.lualib_is_found(plugin.lib) then - h.ok(plugin.lib .. " installed") + if u.is_lualib_found(plugin.lib) then + u.ok(plugin.lib .. " installed") else if plugin.optional then - h.warn(plugin.lib .. " not found, " .. plugin.msg) + u.warn(plugin.lib .. " not found, " .. plugin.msg) else - h.error(plugin.lib .. " not found, " .. plugin.msg) + u.error(plugin.lib .. " not found, " .. plugin.msg) end end end - h.start "required binaries" - h.info "all those binaries can be installed by `:GoInstallDeps`" + u.start "required binaries" + u.info "all those binaries can be installed by `:GoInstallDeps`" for _, bin in ipairs(deps.bin) do - if u.binary_is_found(bin.bin) then - h.ok(bin.bin .. " installed") + if u.is_lualib_found(bin.bin) then + u.ok(bin.bin .. " installed") else if bin.optional then - h.warn(bin.bin .. " not found, " .. bin.msg) + u.warn(bin.bin .. " not found, " .. bin.msg) else - h.error(bin.bin .. " not found, " .. bin.msg) + u.error(bin.bin .. " not found, " .. bin.msg) end end end diff --git a/lua/gopher/iferr.lua b/lua/gopher/iferr.lua index 7f43318..fbeddf7 100644 --- a/lua/gopher/iferr.lua +++ b/lua/gopher/iferr.lua @@ -1,16 +1,18 @@ ----Add iferr declaration ----That's Lua of vimscript implementation of: ----github.com/koron/iferr -return function() - local c = require("gopher.config").config.commands - local u = require "gopher._utils" +local c = require("gopher.config").commands +local u = require "gopher._utils" +local iferr = {} +-- That's Lua of vimscript implementation of: github.com/koron/iferr +iferr.iferr = function() local boff = vim.fn.wordcount().cursor_bytes local cmd = (c.iferr .. " -pos " .. boff) local data = vim.fn.systemlist(cmd, vim.fn.bufnr "%") 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 end @@ -19,3 +21,5 @@ return function() vim.cmd [[silent normal! j=2j]] vim.fn.setpos(".", pos) end + +return iferr diff --git a/lua/gopher/impl.lua b/lua/gopher/impl.lua index a279889..6eddc0f 100644 --- a/lua/gopher/impl.lua +++ b/lua/gopher/impl.lua @@ -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 impl = {} ---@return string 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))) 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 "" end @@ -18,10 +20,7 @@ local function get_struct() return ns.name end -return function(...) - local c = require("gopher.config").config.commands - local Job = require "plenary.job" - +function impl.impl(...) local args = { ... } local iface, recv_name = "", "" local recv = get_struct() @@ -30,7 +29,7 @@ return function(...) iface = vim.fn.input "impl: generating method stubs for interface: " vim.cmd "redraw!" 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 end elseif #args == 1 then -- :GoImpl io.Reader @@ -61,7 +60,10 @@ return function(...) args = cmd_args, on_exit = function(data, retval) 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 end @@ -73,3 +75,5 @@ return function(...) table.insert(res_data, 1, "") vim.fn.append(pos, res_data) end + +return impl diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 46b649e..2f6f32e 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -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 diff --git a/lua/gopher/installer.lua b/lua/gopher/installer.lua index fdd6242..939d50f 100644 --- a/lua/gopher/installer.lua +++ b/lua/gopher/installer.lua @@ -1,3 +1,8 @@ +local Job = require "plenary.job" +local c = require("gopher.config").commands +local u = require "gopher._utils" +local installer = {} + local urls = { gomodifytags = "github.com/fatih/gomodifytags", impl = "github.com/josharian/impl", @@ -8,28 +13,30 @@ local urls = { ---@param pkg string local function install(pkg) - local Job = require "plenary.job" - local u = require "gopher._utils" - local url = urls[pkg] .. "@latest" Job:new({ - command = "go", + command = c.go, args = { "install", url }, on_exit = function(_, retval) 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 end - u.notify("install " .. url .. " finished", "info ") + u.deferred_notify("install " .. url .. " finished", vim.log.levels.INFO) end, }):start() end ---Install required go deps -return function() +function installer.install_deps() for pkg, _ in pairs(urls) do install(pkg) end end + +return installer diff --git a/lua/gopher/struct_tags.lua b/lua/gopher/struct_tags.lua index a91901a..690eae1 100644 --- a/lua/gopher/struct_tags.lua +++ b/lua/gopher/struct_tags.lua @@ -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 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 ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) if ns == nil then @@ -47,9 +46,14 @@ local function modify(...) args = cmd_args, on_exit = function(data, retval) if retval ~= 0 then - u.notify( - "command 'gomodifytags " .. unpack(cmd_args) .. "' exited with code " .. retval, - "error" + u.deferred_notify( + "command '" + .. c.gomodifytags + .. " " + .. unpack(cmd_args) + .. "' exited with code " + .. retval, + vim.log.levels.ERROR ) return end @@ -66,7 +70,7 @@ local function modify(...) or tagged["start"] == nil or tagged["start"] == 0 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 for i, v in ipairs(tagged.lines) do @@ -86,7 +90,7 @@ end ---add tags to struct under cursor ---@param ... unknown -function M.add(...) +function struct_tags.add(...) local arg = { ... } if #arg == nil or arg == "" then arg = { "json" } @@ -102,7 +106,7 @@ end ---remove tags to struct under cursor ---@param ... unknown -function M.remove(...) +function struct_tags.remove(...) local arg = { ... } if #arg == nil or arg == "" then arg = { "json" } @@ -116,4 +120,4 @@ function M.remove(...) modify(unpack(cmd_args)) end -return M +return struct_tags diff --git a/plugin/gopher.vim b/plugin/gopher.vim index e797966..bdffbbe 100644 --- a/plugin/gopher.vim +++ b/plugin/gopher.vim @@ -1,13 +1,13 @@ -command! -nargs=* GoTagAdd :lua require"gopher.api".tags_add() -command! -nargs=* GoTagRm :lua require"gopher.api".tags_rm() -command! -nargs=* GoTestAdd :lua require"gopher.api".test_add() -command! -nargs=* GoTestsAll :lua require"gopher.api".tests_all() -command! -nargs=* GoTestsExp :lua require"gopher.api".test_exported() -command! -nargs=* GoMod :lua require"gopher.api".mod() -command! -nargs=* GoGet :lua require"gopher.api".get() -command! -nargs=* GoWork :lua require"gopher.api".work() -command! -nargs=* GoImpl :lua require"gopher.api".impl() -command! -nargs=* GoGenerate :lua require"gopher.api".generate() -command! GoCmt :lua require"gopher.api".comment() -command! GoIfErr :lua require"gopher.api".iferr() -command! GoInstallDeps :lua require"gopher.api".install_deps() +command! -nargs=* GoTagAdd :lua require"gopher".tags_add() +command! -nargs=* GoTagRm :lua require"gopher".tags_rm() +command! -nargs=* GoTestAdd :lua require"gopher".test_add() +command! -nargs=* GoTestsAll :lua require"gopher".tests_all() +command! -nargs=* GoTestsExp :lua require"gopher".test_exported() +command! -nargs=* GoMod :lua require"gopher".mod() +command! -nargs=* GoGet :lua require"gopher".get() +command! -nargs=* GoWork :lua require"gopher".work() +command! -nargs=* GoImpl :lua require"gopher".impl() +command! -nargs=* GoGenerate :lua require"gopher".generate() +command! GoCmt :lua require"gopher".comment() +command! GoIfErr :lua require"gopher".iferr() +command! GoInstallDeps :lua require"gopher".install_deps()