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

docs: update (#103)

* chore: add @dochide annotation

- it's easier to distinguish @private and something i dont want to see in docs

* docs: update

* refactor: move thing out to utils

* fix: lua-ls error

* fixup! refactor: move thing out to utils

* docs: update

authored by olexsmir.xyz and committed by olexsmir.xyz c0b28346 592fe827

verified
+3 -2
README.md
··· 29 29 vim.cmd.GoInstallDeps() 30 30 end, 31 31 ---@type gopher.Config 32 - opts = {}, -- required 32 + opts = {}, 33 33 } 34 34 ``` 35 35 ··· 39 39 > 40 40 > If you need more info look `:h gopher.nvim` 41 41 42 - **Take a look at default options (might be a bit outdated, look `:h gopher.nvim-config-defaults`)** 42 + **Take a look at default options (might be a bit outdated, look `:h gopher.nvim-config`)** 43 43 44 44 ```lua 45 45 require("gopher").setup { 46 + -- log level, you might consider using DEBUG or TRACE for debugging the plugin 46 47 log_level = vim.log.levels.INFO, 47 48 48 49 -- timeout for running internal commands
+31 -51
doc/gopher.nvim.txt
··· 1 - *gopher.nvim* 1 + *gopher.nvim* Enhance your golang experience 2 + 3 + MIT License Copyright (c) 2025 Oleksandr Smirnov 2 4 3 5 ============================================================================== 4 6 5 7 gopher.nvim is a minimalistic plugin for Go development in Neovim written in Lua. 6 8 It's not an LSP tool, the main goal of this plugin is add go tooling support in Neovim. 7 9 8 - ------------------------------------------------------------------------------ 9 - *gopher.nvim-table-of-contents* 10 10 Table of Contents 11 - Setup....................................................|gopher.nvim-setup| 12 - Install dependencies..............................|gopher.nvim-install-deps| 13 - Configuration...........................................|gopher.nvim-config| 11 + Setup..................................................|gopher.nvim-setup()| 12 + Install dependencies..............................|gopher.nvim-dependencies| 13 + Config..................................................|gopher.nvim-config| 14 14 Commands..............................................|gopher.nvim-commands| 15 15 Modify struct tags.................................|gopher.nvim-struct-tags| 16 16 Auto implementation of interface methods..................|gopher.nvim-impl| ··· 19 19 Generate comments.....................................|gopher.nvim-comments| 20 20 21 21 ------------------------------------------------------------------------------ 22 - *gopher.nvim-setup* 22 + *gopher.nvim-setup()* 23 23 `gopher.setup`({user_config}) 24 24 Setup function. This method simply merges default config with opts table. 25 25 You can read more about configuration at |gopher.nvim-config| 26 26 Calling this function is optional, if you ok with default settings. 27 - See |gopher.nvim.config-defaults| 27 + See |gopher.nvim.config| 28 28 29 29 Usage ~ 30 - `require("gopher").setup {}` (replace `{}` with your `config` table) 30 + >lua 31 + require("gopher").setup {} -- use default config or replace {} with your own 32 + < 31 33 Parameters ~ 32 - {user_config} `(gopher.Config)` 34 + {user_config} `(gopher.Config)` See |gopher.nvim-config| 33 35 34 36 ------------------------------------------------------------------------------ 35 - *gopher.nvim-install-deps* 37 + *gopher.nvim-dependencies* 36 38 `gopher.install_deps` 37 39 Gopher.nvim implements most of its features using third-party tools. 38 40 To install these tools, you can run `:GoInstallDeps` command ··· 44 46 ============================================================================== 45 47 ------------------------------------------------------------------------------ 46 48 *gopher.nvim-config* 47 - config it is the place where you can configure the plugin. 48 - also this is optional is you're ok with default settings. 49 - You can look at default options |gopher.nvim-config-defaults| 50 - 51 - ------------------------------------------------------------------------------ 52 - *gopher.nvim-config-defaults* 53 49 `default_config` 54 50 >lua 55 51 local default_config = { 56 - --minidoc_replace_end 57 - 58 52 -- log level, you might consider using DEBUG or TRACE for debugging the plugin 59 53 ---@type number 60 54 log_level = vim.log.levels.INFO, ··· 112 106 ============================================================================== 113 107 ------------------------------------------------------------------------------ 114 108 *gopher.nvim-struct-tags* 115 - struct-tags is utilizing the `gomodifytags` tool to add or remove tags to struct fields. 109 + 110 + `struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to struct fields. 111 + 116 112 Usage ~ 117 113 118 114 How to add/remove tags to struct fields: 119 - 120 - ------------------------------------------------------------------------------ 115 + 1. Place cursor on the struct 121 116 2. Run `:GoTagAdd json` to add json tags to struct fields 122 117 3. Run `:GoTagRm json` to remove json tags to struct fields 118 + 119 + To clear all tags from struct run: `:GoTagClear` 123 120 124 121 NOTE: if you dont specify the tag it will use `json` as default 125 122 ··· 139 136 Name string `yaml:name` 140 137 } 141 138 < 142 - ------------------------------------------------------------------------------ 143 - *struct_tags.add()* 144 - `struct_tags.add`({...}) 145 - tags to a struct under the cursor 146 - Parameters ~ 147 - {...} `(string)` Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag] 148 - 149 - ------------------------------------------------------------------------------ 150 - *struct_tags.remove()* 151 - `struct_tags.remove`({...}) 152 - tags from a struct under the cursor 153 - Parameters ~ 154 - {...} `(string)` Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag] 155 - 156 - ------------------------------------------------------------------------------ 157 - *struct_tags.clear()* 158 - `struct_tags.clear`() 159 - all tags from a struct under the cursor 160 - 161 139 162 140 ============================================================================== 163 141 ------------------------------------------------------------------------------ 164 142 *gopher.nvim-impl* 165 - impl is utilizing the `impl` tool to generate method stubs for interfaces. 166 - Usage ~ 167 143 144 + Integration of `impl` tool to generate method stubs for interfaces. 145 + 146 + Usage ~ 168 147 1. Automatically implement an interface for a struct: 169 148 - Place your cursor on the struct where you want to implement the interface. 170 149 - Run `:GoImpl io.Reader` ··· 214 193 You can also specify the template to use for generating the tests. See |gopher.nvim-config| 215 194 More details about templates can be found at: https://github.com/cweill/gotests 216 195 217 - ------------------------------------------------------------------------------ 218 - *gopher.nvim-gotests-named* 219 - 220 - You can enable named tests in the config if you prefer using named tests. 221 - See |gopher.nvim-config|. 196 + If you prefer named tests, you can enable them in |gopher.nvim-config|. 222 197 223 198 224 199 ============================================================================== 225 200 ------------------------------------------------------------------------------ 226 201 *gopher.nvim-iferr* 227 - If you're using `iferr` tool, this module provides a way to automatically insert `if err != nil` check. 202 + 203 + `iferr` provides a way to way to automatically insert `if err != nil` check. 204 + If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| 205 + 228 206 Usage ~ 229 207 Execute `:GoIfErr` near any `err` variable to insert the check 230 208 ··· 232 210 ============================================================================== 233 211 ------------------------------------------------------------------------------ 234 212 *gopher.nvim-comments* 235 - Usage ~ 236 - Execute `:GoCmt` to generate a comment for the current function/method/struct/etc on this line. 213 + 237 214 This module provides a way to generate comments for Go code. 215 + 216 + Usage ~ 217 + Set cursor on line with function/method/struct/etc and run `:GoCmt` to generate a comment. 238 218 239 219 240 220 vim:tw=78:ts=8:noet:ft=help:norl:
+8
lua/gopher/_utils/init.lua
··· 7 7 function utils.notify(msg, lvl) 8 8 lvl = lvl or vim.log.levels.INFO 9 9 vim.notify(msg, lvl, { 10 + ---@diagnostic disable-next-line:undefined-field 10 11 title = c.___plugin_name, 11 12 }) 12 13 log.debug(msg) ··· 28 29 end 29 30 end 30 31 return res 32 + end 33 + 34 + ---@param s string 35 + ---@return string 36 + function utils.trimend(s) 37 + local r, _ = string.gsub(s, "%s+$", "") 38 + return r 31 39 end 32 40 33 41 return utils
+1
lua/gopher/_utils/log.lua
··· 18 18 19 19 local config = { 20 20 -- Name of the plugin. Prepended to log messages 21 + ---@diagnostic disable-next-line:undefined-field 21 22 name = c.___plugin_name, 22 23 23 24 -- Should print the output to neovim while running
+6 -4
lua/gopher/comment.lua
··· 1 1 ---@toc_entry Generate comments 2 2 ---@tag gopher.nvim-comments 3 - ---@usage Execute `:GoCmt` to generate a comment for the current function/method/struct/etc on this line. 4 - ---@text This module provides a way to generate comments for Go code. 3 + ---@text 4 + --- This module provides a way to generate comments for Go code. 5 + --- 6 + ---@usage Set cursor on line with function/method/struct/etc and run `:GoCmt` to generate a comment. 5 7 6 8 local ts = require "gopher._utils.ts" 7 9 local log = require "gopher._utils.log" ··· 9 11 10 12 ---@param name string 11 13 ---@return string 12 - ---@private 14 + ---@dochide 13 15 local function template(name) 14 16 return "// " .. name .. " " 15 17 end 16 18 17 19 ---@param bufnr integer 18 20 ---@return string 19 - ---@private 21 + ---@dochide 20 22 local function generate(bufnr) 21 23 local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr) 22 24 if s_ok then
+6 -18
lua/gopher/config.lua
··· 1 - ---@toc_entry Configuration 2 - ---@tag gopher.nvim-config 3 - ---@text config it is the place where you can configure the plugin. 4 - --- also this is optional is you're ok with default settings. 5 - --- You can look at default options |gopher.nvim-config-defaults| 6 - 7 - ---@type gopher.Config 8 - ---@private 9 1 local config = {} 10 2 11 3 ---@tag gopher.nvim-config.ConfigGoTagTransform 12 4 ---@text Possible values for |gopher.Config|.gotag.transform: 13 5 --- 14 - ---@private 6 + ---@dochide 15 7 ---@alias gopher.ConfigGoTagTransform 16 8 ---| "snakecase" "GopherUser" -> "gopher_user" 17 9 ---| "camelcase" "GopherUser" -> "gopherUser" ··· 20 12 ---| "titlecase" "GopherUser" -> "Gopher User" 21 13 ---| "keep" keeps the original field name 22 14 23 - --minidoc_replace_start { 24 - 25 - ---@tag gopher.nvim-config-defaults 15 + ---@toc_entry Config 16 + ---@tag gopher.nvim-config 26 17 ---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section) 27 - --- 28 18 ---@class gopher.Config 29 19 local default_config = { 30 - --minidoc_replace_end 31 - 32 20 -- log level, you might consider using DEBUG or TRACE for debugging the plugin 33 21 ---@type number 34 22 log_level = vim.log.levels.INFO, ··· 73 61 --minidoc_afterlines_end 74 62 75 63 ---@type gopher.Config 76 - ---@private 64 + ---@dochide 77 65 local _config = default_config 78 66 79 67 -- I am kinda secret so don't tell anyone about me even dont use me ··· 84 72 _config.___plugin_name = "gopher.nvim" ---@diagnostic disable-line: inject-field 85 73 86 74 ---@param user_config? gopher.Config 87 - ---@private 75 + ---@dochide 88 76 function config.setup(user_config) 89 77 vim.validate { user_config = { user_config, "table", true } } 90 78 ··· 117 105 end, 118 106 }) 119 107 108 + ---@dochide 120 109 ---@return gopher.Config 121 - ---@private 122 110 return config
+2 -6
lua/gopher/gotests.lua
··· 15 15 --- You can also specify the template to use for generating the tests. See |gopher.nvim-config| 16 16 --- More details about templates can be found at: https://github.com/cweill/gotests 17 17 --- 18 - 19 - ---@tag gopher.nvim-gotests-named 20 - ---@text 21 - --- You can enable named tests in the config if you prefer using named tests. 22 - --- See |gopher.nvim-config|. 18 + --- If you prefer named tests, you can enable them in |gopher.nvim-config|. 23 19 24 20 local c = require "gopher.config" 25 21 local ts_utils = require "gopher._utils.ts" ··· 29 25 local gotests = {} 30 26 31 27 ---@param args table 32 - ---@private 28 + ---@dochide 33 29 local function add_test(args) 34 30 if c.gotests.named then 35 31 table.insert(args, "-named")
+6 -2
lua/gopher/iferr.lua
··· 1 + -- Thanks https://github.com/koron/iferr for vim implementation 2 + 1 3 ---@toc_entry Iferr 2 4 ---@tag gopher.nvim-iferr 3 - ---@text If you're using `iferr` tool, this module provides a way to automatically insert `if err != nil` check. 5 + ---@text 6 + --- `iferr` provides a way to way to automatically insert `if err != nil` check. 7 + --- If you want to change `-message` option of `iferr` tool, see |gopher.nvim-config| 8 + --- 4 9 ---@usage Execute `:GoIfErr` near any `err` variable to insert the check 5 10 6 11 local c = require "gopher.config" ··· 9 14 local log = require "gopher._utils.log" 10 15 local iferr = {} 11 16 12 - -- That's Lua implementation: https://github.com/koron/iferr 13 17 function iferr.iferr() 14 18 local curb = vim.fn.wordcount().cursor_bytes 15 19 local pos = vim.fn.getcurpos()[2]
+4 -3
lua/gopher/impl.lua
··· 1 1 ---@toc_entry Auto implementation of interface methods 2 2 ---@tag gopher.nvim-impl 3 - ---@text impl is utilizing the `impl` tool to generate method stubs for interfaces. 4 - ---@usage 5 - --- 1. Automatically implement an interface for a struct: 3 + ---@text 4 + --- Integration of `impl` tool to generate method stubs for interfaces. 5 + --- 6 + ---@usage 1. Automatically implement an interface for a struct: 6 7 --- - Place your cursor on the struct where you want to implement the interface. 7 8 --- - Run `:GoImpl io.Reader` 8 9 --- - This will automatically determine the receiver and implement the `io.Reader` interface.
+11 -8
lua/gopher/init.lua
··· 1 - --- *gopher.nvim* 1 + --- *gopher.nvim* Enhance your golang experience 2 + --- 3 + --- MIT License Copyright (c) 2025 Oleksandr Smirnov 2 4 --- 3 5 --- ============================================================================== 4 6 --- 5 7 --- gopher.nvim is a minimalistic plugin for Go development in Neovim written in Lua. 6 8 --- It's not an LSP tool, the main goal of this plugin is add go tooling support in Neovim. 7 - 9 + --- 8 10 --- Table of Contents 9 - ---@tag gopher.nvim-table-of-contents 10 11 ---@toc 11 12 12 13 local log = require "gopher._utils.log" ··· 16 17 local gopher = {} 17 18 18 19 ---@toc_entry Setup 19 - ---@tag gopher.nvim-setup 20 + ---@tag gopher.nvim-setup() 20 21 ---@text Setup function. This method simply merges default config with opts table. 21 22 --- You can read more about configuration at |gopher.nvim-config| 22 23 --- Calling this function is optional, if you ok with default settings. 23 - --- See |gopher.nvim.config-defaults| 24 + --- See |gopher.nvim.config| 24 25 --- 25 - ---@usage `require("gopher").setup {}` (replace `{}` with your `config` table) 26 - ---@param user_config gopher.Config 26 + ---@usage >lua 27 + --- require("gopher").setup {} -- use default config or replace {} with your own 28 + --- < 29 + ---@param user_config gopher.Config See |gopher.nvim-config| 27 30 gopher.setup = function(user_config) 28 31 log.debug "setting up config" 29 32 require("gopher.config").setup(user_config) ··· 31 34 end 32 35 33 36 ---@toc_entry Install dependencies 34 - ---@tag gopher.nvim-install-deps 37 + ---@tag gopher.nvim-dependencies 35 38 ---@text Gopher.nvim implements most of its features using third-party tools. 36 39 --- To install these tools, you can run `:GoInstallDeps` command 37 40 --- or call `require("gopher").install_deps()` if you want to use lua api.
+19 -8
lua/gopher/struct_tags.lua
··· 1 1 ---@toc_entry Modify struct tags 2 2 ---@tag gopher.nvim-struct-tags 3 - ---@text struct-tags is utilizing the `gomodifytags` tool to add or remove tags to struct fields. 3 + ---@text 4 + --- `struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to struct fields. 5 + --- 4 6 ---@usage 5 7 --- How to add/remove tags to struct fields: 6 - -- 1. Place cursor on the struct 8 + --- 1. Place cursor on the struct 7 9 --- 2. Run `:GoTagAdd json` to add json tags to struct fields 8 10 --- 3. Run `:GoTagRm json` to remove json tags to struct fields 9 11 --- 12 + --- To clear all tags from struct run: `:GoTagClear` 13 + --- 10 14 --- NOTE: if you dont specify the tag it will use `json` as default 11 15 --- 12 16 --- Example: ··· 29 33 local ts = require "gopher._utils.ts" 30 34 local r = require "gopher._utils.runner" 31 35 local c = require "gopher.config" 36 + local u = require "gopher._utils" 32 37 local log = require "gopher._utils.log" 33 38 local struct_tags = {} 34 39 35 40 ---@param fpath string 36 41 ---@param bufnr integer 37 42 ---@param user_args string[] 38 - ---@private 43 + ---@dochide 39 44 local function handle_tags(fpath, bufnr, user_args) 40 45 local st = ts.get_struct_under_cursor(bufnr) 41 46 ··· 73 78 end 74 79 75 80 for i, v in ipairs(res["lines"]) do 76 - res["lines"][i] = string.gsub(v, "%s+$", "") 81 + res["lines"][i] = u.trimend(v) 77 82 end 78 83 79 84 vim.api.nvim_buf_set_lines( ··· 87 92 88 93 ---@param args string[] 89 94 ---@return string 90 - ---@private 95 + ---@dochide 91 96 local function handler_user_args(args) 92 97 if #args == 0 then 93 98 return c.gotag.default_tag ··· 95 100 return table.concat(args, ",") 96 101 end 97 102 98 - ---Adds tags to a struct under the cursor 103 + -- Adds tags to a struct under the cursor 104 + -- See |gopher.nvim-struct-tags| 99 105 ---@param ... string Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag] 106 + ---@dochide 100 107 function struct_tags.add(...) 101 108 local args = { ... } 102 109 local fpath = vim.fn.expand "%" ··· 106 113 handle_tags(fpath, bufnr, { "-add-tags", user_tags }) 107 114 end 108 115 109 - ---Removes tags from a struct under the cursor 116 + -- Removes tags from a struct under the cursor 117 + -- See `:h gopher.nvim-struct-tags` 118 + ---@dochide 110 119 ---@param ... string Tags to add to the struct fields. If not provided, it will use [config.gotag.default_tag] 111 120 function struct_tags.remove(...) 112 121 local args = { ... } ··· 117 126 handle_tags(fpath, bufnr, { "-remove-tags", user_tags }) 118 127 end 119 128 120 - ---Removes all tags from a struct under the cursor 129 + -- Removes all tags from a struct under the cursor 130 + -- See `:h gopher.nvim-struct-tags` 131 + ---@dochide 121 132 function struct_tags.clear() 122 133 local fpath = vim.fn.expand "%" 123 134 local bufnr = vim.api.nvim_get_current_buf()
+4
scripts/docgen.lua
··· 30 30 return lines 31 31 end 32 32 33 + hooks.sections["@dochide"] = function(s) 34 + s.parent:clear_lines() 35 + end 36 + 33 37 MiniDoc.generate(files, "doc/gopher.nvim.txt", { hooks = hooks })
+6 -10
spec/unit/utils_test.lua
··· 1 1 local t = require "spec.testutils" 2 - local child = MiniTest.new_child_neovim() 3 - local T = MiniTest.new_set { 4 - hooks = { 5 - post_once = child.stop, 6 - pre_case = function() 7 - child.restart { "-u", t.mininit_path } 8 - end, 9 - }, 10 - } 2 + local _, T = t.setup "utils" 11 3 12 - T["utils"] = MiniTest.new_set() 13 4 T["utils"]["should .remove_empty_lines()"] = function() 14 5 local u = require "gopher._utils" 15 6 local inp = { "hi", "", "a", "", "", "asdf" } ··· 24 15 25 16 t.writefile(tmp, data) 26 17 t.eq(u.readfile_joined(tmp), data) 18 + end 19 + 20 + T["utils"]["should .trimend()"] = function() 21 + local u = require "gopher._utils" 22 + t.eq(u.trimend " hi ", " hi") 27 23 end 28 24 29 25 return T