[mirror] Make your go dev experience better github.com/olexsmir/gopher.nvim
neovim golang

Some refactoring (#20)

* feat: move all lua api into `api` module

* feat: remove boilerplate code, add go work suport

* refactor(utils): separete module for health

* refactor(dap): remove copy-paste code

* fix: comment

* chore(lsp): disable type checking

* feat: add `go work` command

authored by olexsmir.xyz and committed by GitHub f835464d d65884b1

+17
lua/gopher/_utils/_health.lua
··· 1 + return { 2 + ---@param lib string 3 + ---@return boolean 4 + lualib_is_found = function(lib) 5 + local is_found, _ = pcall(require, lib) 6 + return is_found 7 + end, 8 + 9 + ---@param bin string 10 + ---@return boolean 11 + binary_is_found = function(bin) 12 + if vim.fn.executable(bin) == 1 then 13 + return true 14 + end 15 + return false 16 + end, 17 + }
+40
lua/gopher/_utils/commands.lua
··· 1 + local Job = require "plenary.job" 2 + local c = require("gopher.config").config.commands 3 + local u = require "gopher._utils" 4 + 5 + ---Run any go commands like `go generate`, `go get`, `go mod` 6 + ---@param cmd string 7 + ---@param ... string|string[] 8 + return function(cmd, ...) 9 + local args = { ... } 10 + if #args == 0 then 11 + u.notify("please provice any arguments", "error") 12 + return 13 + end 14 + 15 + if cmd == "generate" and #args == 1 and args[1] == "%" then 16 + args[1] = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter 17 + elseif cmd == "get" then 18 + for i, arg in ipairs(args) do 19 + ---@diagnostic disable-next-line: param-type-mismatch 20 + local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg 21 + table.remove(args, i) 22 + table.insert(args, i, m) 23 + end 24 + end 25 + 26 + local cmd_args = vim.list_extend({ cmd }, args) ---@diagnostic disable-line: missing-parameter 27 + Job:new({ 28 + command = c.go, 29 + args = cmd_args, 30 + on_exit = function(_, retval) 31 + if retval ~= 0 then 32 + u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") 33 + u.notify(cmd .. " " .. unpack(cmd_args), "debug") 34 + return 35 + end 36 + 37 + u.notify("go " .. cmd .. " was success runned", "info") 38 + end, 39 + }):start() 40 + end
+1 -17
lua/gopher/_utils/init.lua
··· 1 + ---@diagnostic disable: param-type-mismatch 1 2 return { 2 3 ---@param t table 3 4 ---@return boolean ··· 18 19 end 19 20 20 21 return s:sub(1, n) 21 - end, 22 - 23 - ---@param lib string 24 - ---@return boolean 25 - lualib_is_found = function(lib) 26 - local is_found, _ = pcall(require, lib) 27 - return is_found 28 - end, 29 - 30 - ---@param bin string 31 - ---@return boolean 32 - binary_is_found = function(bin) 33 - if vim.fn.executable(bin) == 1 then 34 - return true 35 - end 36 - 37 - return false 38 22 end, 39 23 40 24 ---@param msg string
+26 -9
lua/gopher/_utils/ts/init.lua
··· 1 + ---@diagnostic disable: param-type-mismatch 1 2 local nodes = require "gopher._utils.ts.nodes" 2 3 local u = require "gopher._utils" 3 4 local M = { ··· 24 25 ---@param row string 25 26 ---@param col string 26 27 ---@param bufnr string|nil 28 + ---@param do_notify boolean|nil 27 29 ---@return table|nil 28 - function M.get_struct_node_at_pos(row, col, bufnr) 30 + function M.get_struct_node_at_pos(row, col, bufnr, do_notify) 31 + local notify = do_notify or true 29 32 local query = M.querys.struct_block .. " " .. M.querys.em_struct_block 30 33 local bufn = bufnr or vim.api.nvim_get_current_buf() 31 34 local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 32 35 if ns == nil then 33 - u.notify("struct not found", "warn") 36 + if notify then 37 + u.notify("struct not found", "warn") 38 + end 34 39 else 35 40 return ns[#ns] 36 41 end ··· 39 44 ---@param row string 40 45 ---@param col string 41 46 ---@param bufnr string|nil 47 + ---@param do_notify boolean|nil 42 48 ---@return table|nil 43 - function M.get_func_method_node_at_pos(row, col, bufnr) 49 + function M.get_func_method_node_at_pos(row, col, bufnr, do_notify) 50 + local notify = do_notify or true 44 51 local query = M.querys.func .. " " .. M.querys.method_name 45 52 local bufn = bufnr or vim.api.nvim_get_current_buf() 46 53 local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 47 54 if ns == nil then 48 - u.notify("function not found", "warn") 55 + if notify then 56 + u.notify("function not found", "warn") 57 + end 49 58 else 50 59 return ns[#ns] 51 60 end ··· 54 63 ---@param row string 55 64 ---@param col string 56 65 ---@param bufnr string|nil 66 + ---@param do_notify boolean|nil 57 67 ---@return table|nil 58 - function M.get_package_node_at_pos(row, col, bufnr) 68 + function M.get_package_node_at_pos(row, col, bufnr, do_notify) 69 + local notify = do_notify or true 59 70 -- stylua: ignore 60 71 if row > 10 then return end 61 72 local query = M.querys.package 62 73 local bufn = bufnr or vim.api.nvim_get_current_buf() 63 74 local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 64 75 if ns == nil then 65 - u.notify("package not found", "warn") 66 - return nil 76 + if notify then 77 + u.notify("package not found", "warn") 78 + return nil 79 + end 67 80 else 68 81 return ns[#ns] 69 82 end ··· 72 85 ---@param row string 73 86 ---@param col string 74 87 ---@param bufnr string|nil 88 + ---@param do_notify boolean|nil 75 89 ---@return table|nil 76 - function M.get_interface_node_at_pos(row, col, bufnr) 90 + function M.get_interface_node_at_pos(row, col, bufnr, do_notify) 91 + local notify = do_notify or true 77 92 local query = M.querys.interface 78 93 local bufn = bufnr or vim.api.nvim_get_current_buf() 79 94 local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 80 95 if ns == nil then 81 - u.notify("interface not found", "warn") 96 + if notify then 97 + u.notify("interface not found", "warn") 98 + end 82 99 else 83 100 return ns[#ns] 84 101 end
+29
lua/gopher/api.lua
··· 1 + local API = {} 2 + local tags = require "gopher.struct_tags" 3 + local tests = require "gopher.gotests" 4 + local cmd = require "gopher._utils.commands" 5 + 6 + API.install_deps = require "gopher.installer" 7 + API.tags_add = tags.add 8 + API.tags_rm = tags.remove 9 + API.impl = require "gopher.impl" 10 + API.iferr = require "gopher.iferr" 11 + API.comment = require "gopher.comment" 12 + API.test_add = tests.func_test 13 + API.test_exported = tests.all_exported_tests 14 + API.tests_all = tests.all_tests 15 + 16 + API.get = function(...) 17 + cmd("get", ...) 18 + end 19 + API.mod = function(...) 20 + cmd("mod", ...) 21 + end 22 + API.generate = function(...) 23 + cmd("generate", ...) 24 + end 25 + API.work = function(...) 26 + cmd("work", ...) 27 + end 28 + 29 + return API
+5 -4
lua/gopher/comment.lua
··· 3 3 local function generate(row, col) 4 4 local comment, ns = nil, nil 5 5 6 - ns = ts_utils.get_package_node_at_pos(row, col) 6 + ns = ts_utils.get_package_node_at_pos(row, col, nil, false) 7 7 if ns ~= nil then 8 8 comment = "// Package " .. ns.name .. " provides " .. ns.name 9 9 return comment, ns 10 10 end 11 11 12 - ns = ts_utils.get_struct_node_at_pos(row, col) 12 + ns = ts_utils.get_struct_node_at_pos(row, col, nil, false) 13 13 if ns ~= nil then 14 14 comment = "// " .. ns.name .. " " .. ns.type .. " " 15 15 return comment, ns 16 16 end 17 17 18 - ns = ts_utils.get_func_method_node_at_pos(row, col) 18 + ns = ts_utils.get_func_method_node_at_pos(row, col, nil, false) 19 19 if ns ~= nil then 20 20 comment = "// " .. ns.name .. " " .. ns.type .. " " 21 21 return comment, ns 22 22 end 23 23 24 - ns = ts_utils.get_interface_node_at_pos(row, col) 24 + ns = ts_utils.get_interface_node_at_pos(row, col, nil, false) 25 25 if ns ~= nil then 26 26 comment = "// " .. ns.name .. " " .. ns.type .. " " 27 27 return comment, ns ··· 39 39 ns.dim.s.c, 40 40 }) 41 41 42 + ---@diagnostic disable-next-line: param-type-mismatch 42 43 vim.fn.append(row - 1, comment) 43 44 44 45 vim.api.nvim_win_set_cursor(0, {
+9 -9
lua/gopher/dap/config.lua
··· 1 + ---@diagnostic disable: param-type-mismatch 1 2 local function get_arguments() 3 + local function get() 4 + vim.ui.input({ prompt = "Args: " }, function(input) 5 + return vim.split(input or "", " ") ---@diagnostic disable-line: missing-parameter 6 + end) 7 + end 8 + 2 9 local co = coroutine.running() 3 10 if co then 4 11 return coroutine.create(function() 5 - local args = {} 6 - vim.ui.input({ prompt = "Args: " }, function(input) 7 - args = vim.split(input or "", " ") 8 - end) 12 + local args = get() 9 13 coroutine.resume(co, args) 10 14 end) 11 15 else 12 - local args = {} 13 - vim.ui.input({ prompt = "Args: " }, function(input) 14 - args = vim.split(input or "", " ") 15 - end) 16 - return args 16 + return get() 17 17 end 18 18 end 19 19
-26
lua/gopher/gogenerate.lua
··· 1 - local Job = require "plenary.job" 2 - local c = require("gopher.config").config.commands 3 - local u = require "gopher._utils" 4 - 5 - ---run "go generate" 6 - return function(...) 7 - local args = { ... } 8 - if #args == 1 and args[1] == "%" then 9 - args[1] = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter 10 - end 11 - 12 - local cmd_args = vim.list_extend({ "generate" }, args) 13 - 14 - Job:new({ 15 - command = c.go, 16 - args = cmd_args, 17 - on_exit = function(_, retval) 18 - if retval ~= 0 then 19 - u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") 20 - return 21 - end 22 - 23 - u.notify("go generate was success runned", "info") 24 - end, 25 - }):start() 26 - end
-33
lua/gopher/goget.lua
··· 1 - local Job = require "plenary.job" 2 - local c = require("gopher.config").config.commands 3 - local u = require "gopher._utils" 4 - 5 - ---run "go get" 6 - return function(...) 7 - local args = { ... } 8 - if #args == 0 then 9 - u.notify("please provide a package url to get", "error") 10 - return 11 - end 12 - 13 - for i, arg in ipairs(args) do 14 - local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg 15 - table.remove(args, i) 16 - table.insert(args, i, m) 17 - end 18 - 19 - local cmd_args = vim.list_extend({ "get" }, args) 20 - 21 - Job:new({ 22 - command = c.go, 23 - args = cmd_args, 24 - on_exit = function(_, retval) 25 - if retval ~= 0 then 26 - u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") 27 - return 28 - end 29 - 30 - u.notify("go get was success runned", "info") 31 - end, 32 - }):start() 33 - end
-27
lua/gopher/gomod.lua
··· 1 - local Job = require "plenary.job" 2 - local c = require("gopher.config").config.commands 3 - local u = require "gopher._utils" 4 - 5 - ---run "go mod" 6 - return function(...) 7 - local args = { ... } 8 - if #args == 0 then 9 - u.notify("please provide any mod command", "error") 10 - return 11 - end 12 - 13 - local cmd_args = vim.list_extend({ "mod" }, args) 14 - 15 - Job:new({ 16 - command = c.go, 17 - args = cmd_args, 18 - on_exit = function(_, retval) 19 - if retval ~= 0 then 20 - u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error") 21 - return 22 - end 23 - 24 - u.notify("go mod was success runned", "info") 25 - end, 26 - }):start() 27 - end
+1 -1
lua/gopher/health.lua
··· 1 1 local health = vim.health or require "health" 2 - local utils = require "gopher._utils" 2 + local utils = require "gopher._utils._health" 3 3 local c = require("gopher.config").config.commands 4 4 5 5 local requried = "Gopher.nvim will not work without it!"
+3 -17
lua/gopher/init.lua
··· 1 - local tags = require "gopher.struct_tags" 2 - local gotests = require "gopher.gotests" 3 - local gopher = {} 1 + local GOPHER = {} 4 2 5 - gopher.install_deps = require "gopher.installer" 6 - gopher.tags_add = tags.add 7 - gopher.tags_rm = tags.remove 8 - gopher.mod = require "gopher.gomod" 9 - gopher.get = require "gopher.goget" 10 - gopher.impl = require "gopher.impl" 11 - gopher.generate = require "gopher.gogenerate" 12 - gopher.iferr = require "gopher.iferr" 13 - gopher.comment = require "gopher.comment" 14 - gopher.test_add = gotests.func_test 15 - gopher.test_exported = gotests.all_exported_tests 16 - gopher.tests_all = gotests.all_tests 17 - gopher.setup = require("gopher.config").setup 3 + GOPHER.setup = require("gopher.config").setup 18 4 19 - return gopher 5 + return GOPHER
+13 -12
plugin/gopher.vim
··· 1 - command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>) 2 - command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>) 3 - command! -nargs=* GoTestAdd :lua require"gopher".test_add(<f-args>) 4 - command! -nargs=* GoTestsAll :lua require"gopher".tests_all(<f-args>) 5 - command! -nargs=* GoTestsExp :lua require"gopher".test_exported(<f-args>) 6 - command! -nargs=* GoMod :lua require"gopher".mod(<f-args>) 7 - command! -nargs=* GoGet :lua require"gopher".get(<f-args>) 8 - command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>) 9 - command! -nargs=* GoGenerate :lua require"gopher".generate(<f-args>) 10 - command! GoCmt :lua require"gopher".comment() 11 - command! GoIfErr :lua require"gopher".iferr() 12 - command! GoInstallDeps :lua require"gopher".install_deps() 1 + command! -nargs=* GoTagAdd :lua require"gopher.api".tags_add(<f-args>) 2 + command! -nargs=* GoTagRm :lua require"gopher.api".tags_rm(<f-args>) 3 + command! -nargs=* GoTestAdd :lua require"gopher.api".test_add(<f-args>) 4 + command! -nargs=* GoTestsAll :lua require"gopher.api".tests_all(<f-args>) 5 + command! -nargs=* GoTestsExp :lua require"gopher.api".test_exported(<f-args>) 6 + command! -nargs=* GoMod :lua require"gopher.api".mod(<f-args>) 7 + command! -nargs=* GoGet :lua require"gopher.api".get(<f-args>) 8 + command! -nargs=* GoWork :lua require"gopher.api".work(<f-args>) 9 + command! -nargs=* GoImpl :lua require"gopher.api".impl(<f-args>) 10 + command! -nargs=* GoGenerate :lua require"gopher.api".generate(<f-args>) 11 + command! GoCmt :lua require"gopher.api".comment() 12 + command! GoIfErr :lua require"gopher.api".iferr() 13 + command! GoInstallDeps :lua require"gopher.api".install_deps()