diff --git a/lua/gopher/_utils/_health.lua b/lua/gopher/_utils/_health.lua new file mode 100644 index 0000000..5b12026 --- /dev/null +++ b/lua/gopher/_utils/_health.lua @@ -0,0 +1,17 @@ +return { + ---@param lib string + ---@return boolean + lualib_is_found = function(lib) + local is_found, _ = pcall(require, lib) + return is_found + end, + + ---@param bin string + ---@return boolean + binary_is_found = function(bin) + if vim.fn.executable(bin) == 1 then + return true + end + return false + end, +} diff --git a/lua/gopher/_utils/commands.lua b/lua/gopher/_utils/commands.lua new file mode 100644 index 0000000..4f5a59a --- /dev/null +++ b/lua/gopher/_utils/commands.lua @@ -0,0 +1,40 @@ +local Job = require "plenary.job" +local c = require("gopher.config").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.notify("please provice any arguments", "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.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") + u.notify(cmd .. " " .. unpack(cmd_args), "debug") + return + end + + u.notify("go " .. cmd .. " was success runned", "info") + end, + }):start() +end diff --git a/lua/gopher/_utils/init.lua b/lua/gopher/_utils/init.lua index 4786fd4..190574f 100644 --- a/lua/gopher/_utils/init.lua +++ b/lua/gopher/_utils/init.lua @@ -1,3 +1,4 @@ +---@diagnostic disable: param-type-mismatch return { ---@param t table ---@return boolean @@ -20,23 +21,6 @@ return { return s:sub(1, n) end, - ---@param lib string - ---@return boolean - lualib_is_found = function(lib) - local is_found, _ = pcall(require, lib) - return is_found - end, - - ---@param bin string - ---@return boolean - binary_is_found = function(bin) - if vim.fn.executable(bin) == 1 then - return true - end - - return false - end, - ---@param msg string ---@param lvl string|integer notify = function(msg, lvl) diff --git a/lua/gopher/_utils/ts/init.lua b/lua/gopher/_utils/ts/init.lua index 6bcf8fc..ee24f49 100644 --- a/lua/gopher/_utils/ts/init.lua +++ b/lua/gopher/_utils/ts/init.lua @@ -1,3 +1,4 @@ +---@diagnostic disable: param-type-mismatch local nodes = require "gopher._utils.ts.nodes" local u = require "gopher._utils" local M = { @@ -24,13 +25,17 @@ end ---@param row string ---@param col string ---@param bufnr string|nil +---@param do_notify boolean|nil ---@return table|nil -function M.get_struct_node_at_pos(row, col, bufnr) +function M.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 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 - u.notify("struct not found", "warn") + if notify then + u.notify("struct not found", "warn") + end else return ns[#ns] end @@ -39,13 +44,17 @@ end ---@param row string ---@param col string ---@param bufnr string|nil +---@param do_notify boolean|nil ---@return table|nil -function M.get_func_method_node_at_pos(row, col, bufnr) +function M.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 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 - u.notify("function not found", "warn") + if notify then + u.notify("function not found", "warn") + end else return ns[#ns] end @@ -54,16 +63,20 @@ end ---@param row string ---@param col string ---@param bufnr string|nil +---@param do_notify boolean|nil ---@return table|nil -function M.get_package_node_at_pos(row, col, bufnr) +function M.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 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 - u.notify("package not found", "warn") - return nil + if notify then + u.notify("package not found", "warn") + return nil + end else return ns[#ns] end @@ -72,13 +85,17 @@ end ---@param row string ---@param col string ---@param bufnr string|nil +---@param do_notify boolean|nil ---@return table|nil -function M.get_interface_node_at_pos(row, col, bufnr) +function M.get_interface_node_at_pos(row, col, bufnr, do_notify) + local notify = do_notify or true local query = M.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 - u.notify("interface not found", "warn") + if notify then + u.notify("interface not found", "warn") + end else return ns[#ns] end diff --git a/lua/gopher/api.lua b/lua/gopher/api.lua new file mode 100644 index 0000000..6e854af --- /dev/null +++ b/lua/gopher/api.lua @@ -0,0 +1,29 @@ +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/comment.lua b/lua/gopher/comment.lua index 6810678..41e3f75 100644 --- a/lua/gopher/comment.lua +++ b/lua/gopher/comment.lua @@ -3,25 +3,25 @@ local ts_utils = require "gopher._utils.ts" local function generate(row, col) local comment, ns = nil, nil - ns = ts_utils.get_package_node_at_pos(row, col) + ns = ts_utils.get_package_node_at_pos(row, col, nil, false) if ns ~= nil then comment = "// Package " .. ns.name .. " provides " .. ns.name return comment, ns end - ns = ts_utils.get_struct_node_at_pos(row, col) + ns = ts_utils.get_struct_node_at_pos(row, col, nil, false) if ns ~= nil then comment = "// " .. ns.name .. " " .. ns.type .. " " return comment, ns end - ns = ts_utils.get_func_method_node_at_pos(row, col) + ns = ts_utils.get_func_method_node_at_pos(row, col, nil, false) if ns ~= nil then comment = "// " .. ns.name .. " " .. ns.type .. " " return comment, ns end - ns = ts_utils.get_interface_node_at_pos(row, col) + ns = ts_utils.get_interface_node_at_pos(row, col, nil, false) if ns ~= nil then comment = "// " .. ns.name .. " " .. ns.type .. " " return comment, ns @@ -39,6 +39,7 @@ return function() ns.dim.s.c, }) + ---@diagnostic disable-next-line: param-type-mismatch vim.fn.append(row - 1, comment) vim.api.nvim_win_set_cursor(0, { diff --git a/lua/gopher/dap/config.lua b/lua/gopher/dap/config.lua index 553f42d..80b2196 100644 --- a/lua/gopher/dap/config.lua +++ b/lua/gopher/dap/config.lua @@ -1,19 +1,19 @@ +---@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 = {} - vim.ui.input({ prompt = "Args: " }, function(input) - args = vim.split(input or "", " ") - end) + local args = get() coroutine.resume(co, args) end) else - local args = {} - vim.ui.input({ prompt = "Args: " }, function(input) - args = vim.split(input or "", " ") - end) - return args + return get() end end diff --git a/lua/gopher/gogenerate.lua b/lua/gopher/gogenerate.lua deleted file mode 100644 index 5df4390..0000000 --- a/lua/gopher/gogenerate.lua +++ /dev/null @@ -1,26 +0,0 @@ -local Job = require "plenary.job" -local c = require("gopher.config").config.commands -local u = require "gopher._utils" - ----run "go generate" -return function(...) - local args = { ... } - if #args == 1 and args[1] == "%" then - args[1] = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter - end - - local cmd_args = vim.list_extend({ "generate" }, args) - - Job:new({ - command = c.go, - args = cmd_args, - on_exit = function(_, retval) - if retval ~= 0 then - u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") - return - end - - u.notify("go generate was success runned", "info") - end, - }):start() -end diff --git a/lua/gopher/goget.lua b/lua/gopher/goget.lua deleted file mode 100644 index 2740179..0000000 --- a/lua/gopher/goget.lua +++ /dev/null @@ -1,33 +0,0 @@ -local Job = require "plenary.job" -local c = require("gopher.config").config.commands -local u = require "gopher._utils" - ----run "go get" -return function(...) - local args = { ... } - if #args == 0 then - u.notify("please provide a package url to get", "error") - return - end - - 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 - - local cmd_args = vim.list_extend({ "get" }, args) - - Job:new({ - command = c.go, - args = cmd_args, - on_exit = function(_, retval) - if retval ~= 0 then - u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") - return - end - - u.notify("go get was success runned", "info") - end, - }):start() -end diff --git a/lua/gopher/gomod.lua b/lua/gopher/gomod.lua deleted file mode 100644 index f53ba28..0000000 --- a/lua/gopher/gomod.lua +++ /dev/null @@ -1,27 +0,0 @@ -local Job = require "plenary.job" -local c = require("gopher.config").config.commands -local u = require "gopher._utils" - ----run "go mod" -return function(...) - local args = { ... } - if #args == 0 then - u.notify("please provide any mod command", "error") - return - end - - local cmd_args = vim.list_extend({ "mod" }, args) - - Job:new({ - command = c.go, - args = cmd_args, - on_exit = function(_, retval) - if retval ~= 0 then - u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") - return - end - - u.notify("go mod was success runned", "info") - end, - }):start() -end diff --git a/lua/gopher/health.lua b/lua/gopher/health.lua index 963f0cf..f1cc979 100644 --- a/lua/gopher/health.lua +++ b/lua/gopher/health.lua @@ -1,5 +1,5 @@ local health = vim.health or require "health" -local utils = require "gopher._utils" +local utils = require "gopher._utils._health" local c = require("gopher.config").config.commands local requried = "Gopher.nvim will not work without it!" diff --git a/lua/gopher/init.lua b/lua/gopher/init.lua index 39f5ca9..46b649e 100644 --- a/lua/gopher/init.lua +++ b/lua/gopher/init.lua @@ -1,19 +1,5 @@ -local tags = require "gopher.struct_tags" -local gotests = require "gopher.gotests" -local gopher = {} +local GOPHER = {} -gopher.install_deps = require "gopher.installer" -gopher.tags_add = tags.add -gopher.tags_rm = tags.remove -gopher.mod = require "gopher.gomod" -gopher.get = require "gopher.goget" -gopher.impl = require "gopher.impl" -gopher.generate = require "gopher.gogenerate" -gopher.iferr = require "gopher.iferr" -gopher.comment = require "gopher.comment" -gopher.test_add = gotests.func_test -gopher.test_exported = gotests.all_exported_tests -gopher.tests_all = gotests.all_tests -gopher.setup = require("gopher.config").setup +GOPHER.setup = require("gopher.config").setup -return gopher +return GOPHER diff --git a/plugin/gopher.vim b/plugin/gopher.vim index 089c330..e797966 100644 --- a/plugin/gopher.vim +++ b/plugin/gopher.vim @@ -1,12 +1,13 @@ -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=* 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() +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()