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

refactor: minimize amount of vimscript (#96)

* refactor: remove autoload

* since nvim 0.9 health.lua files are threaded as checkhealth provider

* prof of concept

* fix(runner.gocmd)!: i forgot to update it when i was working on #85

* fix(plugin): now commands register properly

* fix(plugin): fix command name for :GoIfErr

* fix(plugin): respect `setup_commands` option

* docs: update

* refactor(plugin): use vim.schedule

* docs: update CONTRIBUTING

authored by olexsmir.xyz and committed by GitHub 9aa00381 c5cc5080

Changed files
+108 -50
autoload
health
doc
lua
gopher
_utils
runner
plugin
+4 -5
CONTRIBUTING.md
··· 22 22 23 23 For formatting use this following commands, or setup your editor to integrate with selene/stylua: 24 24 ```bash 25 - task format 26 - task format:check # will check if your code formatted 27 - task lint 25 + task stylua 26 + task lint # lintering and format chewing 28 27 ``` 29 28 30 29 ### Documentation ··· 43 42 44 43 ### Testing 45 44 46 - For testing this plugins uses [plenary.nvim](https://github.com/nvim-lua/plenary.nvim). 47 - All tests live in [/spec](https://github.com/olexsmir/gopher.nvim/tree/main/spec) dir. 45 + For testing this plugins uses [mini.test](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-test.md). 46 + All tests live in [/spec](./spec) dir. 48 47 49 48 You can run tests with: 50 49 ```bash
+1 -11
Taskfile.yml
··· 1 1 version: "3" 2 2 tasks: 3 - format: 4 - desc: formats all lua files in repo 5 - cmds: 6 - - stylua . 7 - 8 3 lint: 9 4 desc: runs all linters 10 5 cmds: 11 6 - task: selene 12 - - task: stylua:check 7 + - stylua --check . 13 8 14 9 selene: 15 10 desc: runs lua linter(selene) 16 11 cmds: 17 12 - selene . 18 - 19 - stylua:check: 20 - desc: runs stylua in check mode 21 - cmds: 22 - - stylua --check . 23 13 24 14 stylua: 25 15 desc: runs lua formatter
-3
autoload/health/gopher.vim
··· 1 - function! health#gopher#check() 2 - lua require("gopher.health").check() 3 - endfunction
+4
doc/gopher.nvim.txt
··· 61 61 ---@type number 62 62 timeout = 2000, 63 63 64 + --- whether to setup plugin commands or not 65 + ---@type boolean 66 + setup_commands = true, 67 + 64 68 -- user specified paths to binaries 65 69 ---@class gopher.ConfigCommand 66 70 commands = {
+9 -11
lua/gopher/_utils/runner/gocmd.lua
··· 25 25 26 26 ---@param subcmd string 27 27 ---@param args string[] 28 - ---@return string[]|nil 28 + ---@return string 29 29 function gocmd.run(subcmd, args) 30 - if #args == 0 then 30 + if #args == 0 and subcmd ~= "generate" then 31 31 error "please provide any arguments" 32 32 end 33 33 ··· 39 39 args = if_generate(args) 40 40 end 41 41 42 - return r.sync(c.go, { 43 - args = { subcmd, unpack(args) }, 44 - on_exit = function(data, status) 45 - if status ~= 0 then 46 - error("gocmd failed: " .. data) 47 - end 48 - u.notify(c.go .. " " .. subcmd .. " ran successful") 49 - end, 50 - }) 42 + local rs = r.sync { c.go, subcmd, unpack(args) } 43 + if rs.code ~= 0 then 44 + error("go " .. subcmd .. " failed: " .. rs.stderr) 45 + end 46 + 47 + u.notify(c.go .. " " .. subcmd .. " ran successful") 48 + return rs.stdout 51 49 end 52 50 53 51 return gocmd
+12
lua/gopher/config.lua
··· 37 37 ---@type number 38 38 timeout = 2000, 39 39 40 + --- whether to setup plugin commands or not 41 + ---@type boolean 42 + setup_commands = true, 43 + 40 44 -- user specified paths to binaries 41 45 ---@class gopher.ConfigCommand 42 46 commands = { ··· 90 94 _config = vim.tbl_deep_extend("force", default_config, user_config or {}) 91 95 end 92 96 97 + ---@return boolean 98 + ---@private 99 + function config.should_setup_commands() 100 + return config.setup_commands 101 + end 102 + 93 103 setmetatable(config, { 94 104 __index = function(_, key) 95 105 return _config[key] 96 106 end, 97 107 }) 98 108 109 + ---@return gopher.Config 110 + ---@private 99 111 return config
+4 -4
lua/gopher/init.lua
··· 55 55 } 56 56 57 57 gopher.get = function(...) 58 - gocmd("get", { ... }) 58 + gocmd("get", ...) 59 59 end 60 60 61 61 gopher.mod = function(...) 62 - gocmd("mod", { ... }) 62 + gocmd("mod", ...) 63 63 end 64 64 65 65 gopher.generate = function(...) 66 - gocmd("generate", { ... }) 66 + gocmd("generate", ...) 67 67 end 68 68 69 69 gopher.work = function(...) 70 - gocmd("work", { ... }) 70 + gocmd("work", ...) 71 71 end 72 72 73 73 return gopher
+74
plugin/gopher.lua
··· 1 + --- NOTE: runs in defer since this file before gopher.config 2 + --- I'm not sure if this is the best to do this 3 + vim.schedule(function() 4 + if require("gopher.config").should_setup_commands() then 5 + vim.api.nvim_create_user_command("GopherLog", function() 6 + vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile()) 7 + end, { nargs = 0 }) 8 + 9 + vim.api.nvim_create_user_command("GoIfErr", function() 10 + require("gopher").iferr() 11 + end, { nargs = 0 }) 12 + 13 + vim.api.nvim_create_user_command("GoCmt", function() 14 + require("gopher").comment() 15 + end, { nargs = 0 }) 16 + 17 + vim.api.nvim_create_user_command("GoImpl", function(args) 18 + require("gopher").impl(unpack(args.fargs)) 19 + end, { nargs = "*" }) 20 + 21 + -- :GoInstall 22 + vim.api.nvim_create_user_command("GoInstallDeps", function() 23 + require("gopher").install_deps() 24 + end, { nargs = 0 }) 25 + 26 + vim.api.nvim_create_user_command("GoInstallDepsSync", function() 27 + require("gopher").install_deps { sync = true } 28 + end, { nargs = 0 }) 29 + 30 + --- :GoTag 31 + vim.api.nvim_create_user_command("GoTagAdd", function(opts) 32 + require("gopher").tags.add(unpack(opts.fargs)) 33 + end, { nargs = "*" }) 34 + 35 + vim.api.nvim_create_user_command("GoTagRm", function(opts) 36 + require("gopher").tags.rm(unpack(opts.fargs)) 37 + end, { nargs = "*" }) 38 + 39 + vim.api.nvim_create_user_command("GoTagClear", function() 40 + require("gopher").tags.clear() 41 + end, { nargs = 0 }) 42 + 43 + --- :GoTest 44 + vim.api.nvim_create_user_command("GoTestAdd", function() 45 + require("gopher").test.add() 46 + end, { nargs = 0 }) 47 + 48 + vim.api.nvim_create_user_command("GoTestsAll", function() 49 + require("gopher").test.all() 50 + end, { nargs = 0 }) 51 + 52 + vim.api.nvim_create_user_command("GoTestsExp", function() 53 + require("gopher").test.exported() 54 + end, { nargs = 0 }) 55 + 56 + -- :Go 57 + vim.api.nvim_create_user_command("GoMod", function(opts) 58 + require("gopher").mod(opts.fargs) 59 + end, { nargs = "*" }) 60 + 61 + vim.api.nvim_create_user_command("GoGet", function(opts) 62 + vim.print(opts) 63 + require("gopher").get(opts.fargs) 64 + end, { nargs = "*" }) 65 + 66 + vim.api.nvim_create_user_command("GoWork", function(opts) 67 + require("gopher").get(opts.fargs) 68 + end, { nargs = "*" }) 69 + 70 + vim.api.nvim_create_user_command("GoGenerate", function(opts) 71 + require("gopher").generate(opts.fargs or "") 72 + end, { nargs = "?" }) 73 + end 74 + end)
-16
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! GoTagClear :lua require"gopher".tags.clear() 4 - command! GoTestAdd :lua require"gopher".test.add() 5 - command! GoTestsAll :lua require"gopher".test.all() 6 - command! GoTestsExp :lua require"gopher".test.exported() 7 - command! -nargs=* GoMod :lua require"gopher".mod(<f-args>) 8 - command! -nargs=* GoGet :lua require"gopher".get(<f-args>) 9 - command! -nargs=* GoWork :lua require"gopher".work(<f-args>) 10 - command! -nargs=* GoImpl :lua require"gopher".impl(<f-args>) 11 - command! -nargs=* GoGenerate :lua require"gopher".generate(<f-args>) 12 - command! GoCmt :lua require"gopher".comment() 13 - command! GoIfErr :lua require"gopher".iferr() 14 - command! GoInstallDeps :lua require"gopher".install_deps() 15 - command! GoInstallDepsSync :lua require"gopher".install_deps({ sync = true }) 16 - command! GopherLog :lua vim.cmd("tabnew " .. require("gopher._utils.log").get_outfile())