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

feat(gotests): add generate one test

docs(gotests): add generate one test

Changed files
+74 -2
lua
gopher
plugin
+10 -2
README.md
··· 3 3 Minimalistic plugin for Go development in Neovim written in Lua. 4 4 5 5 It's not an LSP tool, the main goal of this plugin add go tooling support in neovim. 6 - 7 6 ## Install 8 7 9 8 Pre-dependency: [go](https://github.com/golang/go) (tested on 1.17 and 1.18) ··· 32 31 33 32 - [gomodifytags](https://github.com/fatih/gomodifytags) 34 33 - [impl](https://github.com/josharian/impl) 34 + - [gotests](https://github.com/cweill/gotests) 35 35 36 36 2. Modify struct tags: 37 37 By default be added/removed `json` tag, if not set. ··· 78 78 :GoImpl io.Reader 79 79 ``` 80 80 81 - 5. Run `go generate` command 81 + 6. Generate tests with [gotests](https://github.com/cweill/gotests) 82 + 83 + Generate one test for spesific function/method 84 + 85 + ```vim 86 + :GoTestAdd 87 + ``` 88 + 89 + 7. Run `go generate` command 82 90 83 91 ```vim 84 92 " Run `go generate` in cwd path
+13
lua/gopher/_utils/ts/init.lua
··· 3 3 querys = { 4 4 struct_block = [[((type_declaration (type_spec name:(type_identifier) @struct.name type: (struct_type)))@struct.declaration)]], 5 5 em_struct_block = [[(field_declaration name:(field_identifier)@struct.name type: (struct_type)) @struct.declaration]], 6 + method_name = [[((method_declaration receiver: (parameter_list)@method.receiver name: (field_identifier)@method.name body:(block))@method.declaration)]], 7 + func = [[((function_declaration name: (identifier)@function.name) @function.declaration)]], 6 8 }, 7 9 } 8 10 ··· 26 28 local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 27 29 if ns == nil then 28 30 print "struct not found" 31 + else 32 + return ns[#ns] 33 + end 34 + end 35 + 36 + function M.get_func_method_node_at_pos(row, col, bufnr) 37 + local query = M.querys.func .. " " .. M.querys.method_name 38 + local bufn = bufnr or vim.api.nvim_get_current_buf() 39 + local ns = nodes.nodes_at_cursor(query, get_name_defaults(), bufn, row, col) 40 + if ns == nil then 41 + print "func not found" 29 42 else 30 43 return ns[#ns] 31 44 end
+48
lua/gopher/gotests.lua
··· 1 + local Job = require "plenary.job" 2 + local ts_utils = require "gopher._utils.ts" 3 + local M = {} 4 + 5 + ---@param cmd_args table 6 + local function run(cmd_args) 7 + Job 8 + :new({ 9 + command = "gotests", 10 + args = cmd_args, 11 + on_exit = function(_, retval) 12 + if retval ~= 0 then 13 + print("command exited with code " .. retval) 14 + return 15 + end 16 + 17 + print "unit test(s) generated" 18 + end, 19 + }) 20 + :start() 21 + end 22 + 23 + ---@param args table 24 + local function add_test(args) 25 + local fpath = vim.fn.expand "%" ---@diagnostic disable-line: missing-parameter 26 + table.insert(args, "-w") 27 + table.insert(args, fpath) 28 + run(args) 29 + end 30 + 31 + ---generate unit test for one function 32 + ---@param parallel boolean 33 + function M.func_test(parallel) 34 + local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) 35 + if ns == nil or ns.name == nil then 36 + print "cursor on func/method and execute the command again" 37 + return 38 + end 39 + 40 + local cmd_args = { "-only", ns.name } 41 + if parallel then 42 + table.insert(cmd_args, "-parallel") 43 + end 44 + 45 + add_test(cmd_args) 46 + end 47 + 48 + return M
+2
lua/gopher/init.lua
··· 1 1 local tags = require "gopher.struct_tags" 2 + local gotests = require "gopher.gotests" 2 3 local gopher = {} 3 4 4 5 gopher.install_deps = require "gopher.installer" ··· 8 9 gopher.get = require "gopher.goget" 9 10 gopher.impl = require "gopher.impl" 10 11 gopher.generate = require "gopher.gogenerate" 12 + gopher.test_add = gotests.one_test 11 13 12 14 return gopher
+1
plugin/gopher.vim
··· 1 1 command! -nargs=* GoTagAdd :lua require"gopher".tags_add(<f-args>) 2 2 command! -nargs=* GoTagRm :lua require"gopher".tags_rm(<f-args>) 3 + command! -nargs=* GoTestAdd :lua require"gopher".test_add(<f-args>) 3 4 command! -nargs=* GoMod :lua require"gopher".mod(<f-args>) 4 5 command! -nargs=* GoGet :lua require"gopher".get(<f-args>) 5 6 command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>)