@@ -15,6 +15,12 @@ os: [ubuntu-latest]
version: - stable - nightly + - v0.10.0 + - v0.10.4 + - v0.11.0 + - v0.11.1 + - v0.11.2 + - v0.11.3 runs-on: ${{ matrix.os }} steps: - name: Install Task@@ -47,14 +53,9 @@ ~/go/pkg/mod
key: ${{ runner.os }}-tests-${{ hashFiles('${{ github.workspace }}/.tests') }} - name: Install Go bins - run: | - # TODO: install with :GoInstallDeps - go install github.com/fatih/gomodifytags@latest - go install github.com/josharian/impl@latest - go install github.com/cweill/gotests/...@latest - go install github.com/koron/iferr@latest + run: task install-deps - name: Run Tests run: | nvim --version - task tests + task test
@@ -1,17 +1,14 @@
# Contributing to `gopher.nvim` - Thank you for taking the time to submit some code to gopher.nvim. It means a lot! ### Task running - In this codebase for running tasks is used [Taskfile](https://taskfile.dev). You can install it with: ```bash go install github.com/go-task/task/v3/cmd/task@latest ``` -### Styling and formatting - +### Formatting and linting Code is formatted by [stylua](https://github.com/JohnnyMorganz/StyLua) and linted using [selene](https://github.com/Kampfkarren/selene). You can install these with:@@ -22,30 +19,31 @@ ```
For formatting use this following commands, or setup your editor to integrate with selene/stylua: ```bash -task stylua -task lint # lintering and format chewing +task format +task lint ``` ### Documentation +Here we're using [mini.doc](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-doc.md) +for generating vimhelp files based on [LuaCats](https://luals.github.io/wiki/annotations/) annotations in comments. -Here we are using [mini.doc](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-doc.md) -for generating help files based on EmmyLua-like annotations in comments +For demo gifs in [readme](./README.md) we're using [vhs](https://github.com/charmbracelet/vhs). +All files related to demos live in [/vhs](./vhs) dir. You can generate docs with: ```bash -task docgen +task docgen # generates vimhelp +task vhs:generate # generates demo gifs ``` ### Commit messages - We use [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/), please follow it. ### Testing - For testing this plugins uses [mini.test](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-test.md). All tests live in [/spec](./spec) dir. You can run tests with: ```bash -task tests +task test ```
@@ -4,19 +4,21 @@ [](https://stand-with-ukraine.pp.ua)
Minimalistic plugin for Go development in Neovim written in Lua. -It's **NOT** an LSP tool, the main goal of this plugin is to add go tooling support in Neovim. +It's **NOT** an LSP tool, the goal of this plugin is to add go tooling support in Neovim. -> If you want to use new and maybe undocumented, and unstable features you might use [develop](https://github.com/olexsmir/gopher.nvim/tree/develop) branch. +> All development of new and maybe undocumented, and unstable features is happening on [develop](https://github.com/olexsmir/gopher.nvim/tree/develop) branch. ## Install (using [lazy.nvim](https://github.com/folke/lazy.nvim)) Requirements: - **Neovim 0.10** or later -- Treesitter `go` parser(`:TSInstall go` if you use [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter)) -- [Go](https://github.com/golang/go) installed (tested on 1.23) +- Treesitter parser for `go`(`:TSInstall go` if you use [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter)) +- [Go](https://github.com/golang/go) installed ```lua +-- NOTE: this plugin is already lazy-loaded, it adds only about 1ms of load +-- time to your config { "olexsmir/gopher.nvim", ft = "go",@@ -25,57 +27,13 @@ -- (optional) will update plugin's deps on every update
build = function() vim.cmd.GoInstallDeps() end, + ---@module "gopher" ---@type gopher.Config opts = {}, } ``` -## Configuration - -> [!IMPORTANT] -> -> If you need more info look `:h gopher.nvim` - -**Take a look at default options (might be a bit outdated, look `:h gopher.nvim-config`)** - -```lua -require("gopher").setup { - -- log level, you might consider using DEBUG or TRACE for debugging the plugin - log_level = vim.log.levels.INFO, - - -- timeout for running internal commands - timeout = 2000, - - commands = { - go = "go", - gomodifytags = "gomodifytags", - gotests = "gotests", - impl = "impl", - iferr = "iferr", - }, - gotests = { - -- gotests doesn't have template named "default" so this plugin uses "default" to set the default template - template = "default", - -- path to a directory containing custom test code templates - template_dir = nil, - -- switch table tests from using slice to map (with test name for the key) - named = false, - }, - gotag = { - transform = "snakecase", - -- default tags to add to struct fields - default_tag = "json", - }, - iferr = { - -- choose a custom error message - message = nil, - }, -} -``` - ## Features - -<!-- markdownlint-disable --> <details> <summary>@@ -99,6 +57,8 @@ <summary>
<b>Add and remove tags for structs via <a href="https://github.com/fatih/gomodifytags">gomodifytags</a></b> </summary> +  + By default `json` tag will be added/removed, if not set: ```vim@@ -176,6 +136,8 @@ <summary>
<b>Interface implementation via <a href="https://github.com/josharian/impl">impl<a></b> </summary> +  + Syntax of the command: ```vim :GoImpl [receiver] [interface]@@ -198,6 +160,8 @@ <details>
<summary> <b>Generate boilerplate for doc comments</b> </summary> + +  First set a cursor on **public** package/function/interface/struct and execute:@@ -212,6 +176,8 @@ <summary>
<b>Generate <code>if err != nil {</code> via <a href="https://github.com/koron/iferr">iferr</a></b> </summary> +  + Set the cursor on the line with `err` and execute ```vim@@ -219,11 +185,49 @@ :GoIfErr
``` </details> -## Contributing +## Configuration -PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md) +> [!IMPORTANT] +> +> If you need more info look `:h gopher.nvim` -## Thanks +**Take a look at default options (might be a bit outdated, look `:h gopher.nvim-config`)** -- [go.nvim](https://github.com/ray-x/go.nvim) -- [iferr](https://github.com/koron/iferr) +```lua +require("gopher").setup { + -- log level, you might consider using DEBUG or TRACE for debugging the plugin + log_level = vim.log.levels.INFO, + + -- timeout for running internal commands + timeout = 2000, + + commands = { + go = "go", + gomodifytags = "gomodifytags", + gotests = "gotests", + impl = "impl", + iferr = "iferr", + }, + gotests = { + -- gotests doesn't have template named "default" so this plugin uses "default" to set the default template + template = "default", + -- path to a directory containing custom test code templates + template_dir = nil, + -- switch table tests from using slice to map (with test name for the key) + named = false, + }, + gotag = { + transform = "snakecase", + -- default tags to add to struct fields + default_tag = "json", + }, + iferr = { + -- choose a custom error message + message = nil, + }, +} +``` + +## Contributing + +PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md)
@@ -1,28 +1,26 @@
version: "3" + +includes: + vhs: + taskfile: ./vhs/Taskfile.yml + dir: ./vhs + tasks: lint: - desc: runs all linters cmds: - - task: selene + - selene . - stylua --check . - selene: - desc: runs lua linter(selene) - cmds: - - selene . - - stylua: - desc: runs lua formatter - cmds: - - stylua . + format: + cmd: stylua . - tests: - desc: run all tests + test: cmds: - | nvim --clean --headless \ -u ./scripts/minimal_init.lua \ - -c "lua MiniTest.run()" + -c "lua MiniTest.run()" \ + -c ":qa!" docgen: desc: generate vimhelp@@ -32,3 +30,11 @@ nvim --clean --headless \
-u "./scripts/minimal_init.lua" \ -c "luafile ./scripts/docgen.lua" \ -c ":qa!" + + install-deps: + desc: installs go bin (used in CI) + cmds: + - | + nvim --clean --headless \ + -u "./scripts/minimal_init.lua" \ + +GoInstallDepsSync +qa
@@ -65,8 +65,8 @@ end
---@class gopher.TsResult ---@field name string ----@field start_line integer ----@field end_line integer +---@field start integer +---@field end_ integer ---@field is_varstruct boolean ---@param bufnr integer@@ -95,8 +95,8 @@ local res = get_captures(q, parent_node, bufnr)
assert(res.name ~= nil, "No capture name found") local start_row, _, end_row, _ = parent_node:range() - res["start_line"] = start_row + 1 - res["end_line"] = end_row + 1 + res["start"] = start_row + 1 + res["end_"] = end_row + 1 return res end
@@ -109,4 +109,4 @@ })
---@dochide ---@return gopher.Config -return config +return config --[[ @as gopher.Config ]]
@@ -3,43 +3,3 @@ any = true
[MiniTest] any = true - -[describe] -any = true -[[describe.args]] -type = "string" -[[describe.args]] -type = "function" - -[it] -any = true -[[it.args]] -type = "string" -[[it.args]] -type = "function" - -[before_each] -any = true -[[before_each.args]] -type = "function" -[[after_each.args]] -type = "function" - -[assert] -any = true - -[assert.is_not] -any = true - -[[assert.equals.args]] -type = "any" -[[assert.equals.args]] -type = "any" -[[assert.equals.args]] -type = "any" -required = false - -[[assert.same.args]] -type = "any" -[[assert.same.args]] -type = "any"
@@ -11,10 +11,13 @@
---@param name string ---@param fn fun(args: table) ---@param nargs? number|"*"|"?" +---@param range? boolean ---@private -local function cmd(name, fn, nargs) - nargs = nargs or 0 - vim.api.nvim_create_user_command(name, fn, { nargs = nargs }) +local function cmd(name, fn, nargs, range) + vim.api.nvim_create_user_command(name, fn, { + nargs = nargs or 0, + range = range or false, + }) end cmd("GopherLog", function()@@ -44,12 +47,24 @@ end)
-- :GoTag cmd("GoTagAdd", function(opts) - require("gopher").tags.add(unpack(opts.fargs)) -end, "*") + require("gopher").tags.add { + tags = opts.fargs, + range = (opts.count ~= -1) and { + start = opts.line1, + end_ = opts.line2, + } or nil, + } +end, "*", true) cmd("GoTagRm", function(opts) - require("gopher").tags.rm(unpack(opts.fargs)) -end, "*") + require("gopher").tags.rm { + tags = opts.fargs, + range = (opts.count ~= -1) and { + start = opts.line1, + end_ = opts.line2, + } or nil, + } +end, "*", true) cmd("GoTagClear", function() require("gopher").tags.clear()
@@ -23,6 +23,7 @@
install_plug "nvim-lua/plenary.nvim" install_plug "nvim-treesitter/nvim-treesitter" install_plug "echasnovski/mini.doc" -- used for docs generation +install_plug "folke/tokyonight.nvim" -- theme for generating demos install_plug "echasnovski/mini.test" vim.env.XDG_CONFIG_HOME = root ".tests/config"@@ -32,6 +33,8 @@ vim.env.XDG_CACHE_HOME = root ".tests/cache"
vim.opt.runtimepath:append(root()) vim.opt.packpath:append(root ".tests/site") +vim.o.swapfile = false +vim.o.writebackup = false vim.notify = vim.print -- install go treesitter parse@@ -51,6 +54,13 @@ return vim.fn.globpath("spec", "**/*_test.lua", true, true)
end, }, } +end + +-- set colorscheme only when running ui +if #vim.api.nvim_list_uis() == 1 then + vim.cmd.colorscheme "tokyonight-night" + vim.o.cursorline = true + vim.o.number = true end -- needed for tests, i dont know the reason why, but on start
@@ -1,5 +1,5 @@
local t = require "spec.testutils" -local child, T = t.setup "comment" +local child, T, comment = t.setup "comment" local function do_the_test(fixture, pos) local rs = t.setup_test("comment/" .. fixture, child, pos)@@ -10,27 +10,27 @@ t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs) end -T["comment"]["should add comment to package"] = function() +comment["should add comment to package"] = function() do_the_test("package", { 1, 1 }) end -T["comment"]["should add comment to struct"] = function() +comment["should add comment to struct"] = function() do_the_test("struct", { 4, 1 }) end -T["comment"]["should add comment to function"] = function() +comment["should add comment to function"] = function() do_the_test("func", { 3, 1 }) end -T["comment"]["should add comment to method"] = function() +comment["should add comment to method"] = function() do_the_test("method", { 5, 1 }) end -T["comment"]["should add comment to interface"] = function() +comment["should add comment to interface"] = function() do_the_test("interface", { 3, 6 }) end -T["comment"]["otherwise should add // above cursor"] = function() +comment["otherwise should add // above cursor"] = function() do_the_test("empty", { 1, 1 }) end
@@ -1,5 +1,5 @@
local t = require "spec.testutils" -local child, T = t.setup "gotests" +local child, T, gotests = t.setup "gotests" --- NOTE: :GoTestAdd is the only place that has actual logic --- All other parts are handled `gotests` itself.@@ -10,7 +10,7 @@ local function read_testfile(fpath)
return t.readfile(fpath:gsub(".go", "_test.go")) end -T["gotests"]["should add test for function under cursor"] = function() +gotests["should add test for function under cursor"] = function() local rs = t.setup_test("tests/function", child, { 3, 5 }) child.cmd "GoTestAdd"@@ -18,7 +18,7 @@ t.eq(rs.fixtures.output, read_testfile(rs.tmp))
t.cleanup(rs) end -T["gotests"]["should add test for method under cursor"] = function() +gotests["should add test for method under cursor"] = function() local rs = t.setup_test("tests/method", child, { 5, 19 }) child.cmd "GoTestAdd"
@@ -1,7 +1,7 @@
local t = require "spec.testutils" -local child, T = t.setup "iferr" +local child, T, iferr = t.setup "iferr" -T["iferr"]["should add if != nil {"] = function() +iferr["should add if != nil {"] = function() local rs = t.setup_test("iferr/iferr", child, { 8, 2 }) child.cmd "GoIfErr" child.cmd "write"@@ -10,7 +10,7 @@ t.eq(t.readfile(rs.tmp), rs.fixtures.output)
t.cleanup(rs) end -T["iferr"]["should add if err with custom message"] = function() +iferr["should add if err with custom message"] = function() child.lua [[ require("gopher").setup { iferr = { message = 'fmt.Errorf("failed to %w", err)' }
@@ -1,7 +1,7 @@
local t = require "spec.testutils" -local child, T = t.setup "impl" +local child, T, impl = t.setup "impl" -T["impl"]["should do impl with 'w io.Writer'"] = function() +impl["should do impl with 'w io.Writer'"] = function() local rs = t.setup_test("impl/writer", child, { 3, 0 }) child.cmd "GoImpl w io.Writer" child.cmd "write"@@ -12,7 +12,7 @@ t.eq(t.readfile(rs.tmp), rhs)
t.cleanup(rs) end -T["impl"]["should work with full input, 'r Read io.Reader'"] = function() +impl["should work with full input, 'r Read io.Reader'"] = function() local rs = t.setup_test("impl/reader", child) child.cmd "GoImpl r Read io.Reader" child.cmd "write"@@ -22,7 +22,7 @@ t.eq(t.readfile(rs.tmp), rhs)
t.cleanup(rs) end -T["impl"]["should work with minimal input 'io.Closer'"] = function() +impl["should work with minimal input 'io.Closer'"] = function() local rs = t.setup_test("impl/closer", child, { 3, 6 }) child.cmd "GoImpl io.Closer" child.cmd "write"
@@ -6,9 +6,11 @@
testutils.mininit_path = vim.fs.joinpath(base_dir, "scripts", "minimal_init.lua") testutils.fixtures_dir = vim.fs.joinpath(base_dir, "spec/fixtures") ----@param name string ----@return MiniTest.child, table -function testutils.setup(name) +---@param mod string Module name for which to create a nested test set. +---@return MiniTest.child child nvim client. +---@return table T root test set created by `MiniTest.new_set()`. +---@return table mod_name nested set of tests in `T[mod]`. +function testutils.setup(mod) local child = MiniTest.new_child_neovim() local T = MiniTest.new_set { hooks = {@@ -19,8 +21,8 @@ end,
}, } - T[name] = MiniTest.new_set {} - return child, T + T[mod] = MiniTest.new_set {} + return child, T, T[mod] end ---@generic T@@ -76,6 +78,8 @@ ---@param child MiniTest.child
---@param pos? number[] ---@return gopher.TestUtilsSetup function testutils.setup_test(fixture, child, pos) + vim.validate("pos", pos, "table", true) + local tmp = testutils.tmpfile() local fixtures = testutils.get_fixtures(fixture)@@ -84,6 +88,8 @@ child.cmd("silent edit " .. tmp)
local bufnr = child.fn.bufnr(tmp) if pos then + assert(#pos == 2, "invalid cursor position") + child.fn.setpos(".", { bufnr, unpack(pos) }) end
@@ -0,0 +1,22 @@
+local t = require "spec.testutils" +local _, T, config = t.setup "config" + +config["can be called without any arguments passed"] = function() + ---@diagnostic disable-next-line: missing-parameter + require("gopher").setup() +end + +config["can be called with empty table"] = function() + require("gopher").setup {} +end + +config["should change option"] = function() + local log_level = 1234567890 + require("gopher").setup { + log_level = log_level, + } + + t.eq(log_level, require("gopher.config").log_level) +end + +return T
@@ -1,14 +1,14 @@
local t = require "spec.testutils" -local _, T = t.setup "utils" +local _, T, utils = t.setup "utils" -T["utils"]["should .remove_empty_lines()"] = function() +utils["should .remove_empty_lines()"] = function() local u = require "gopher._utils" local inp = { "hi", "", "a", "", "", "asdf" } t.eq(u.remove_empty_lines(inp), { "hi", "a", "asdf" }) end -T["utils"]["should .readfile_joined()"] = function() +utils["should .readfile_joined()"] = function() local data = "line1\nline2\nline3" local tmp = t.tmpfile() local u = require "gopher._utils"@@ -17,7 +17,7 @@ t.writefile(tmp, data)
t.eq(u.readfile_joined(tmp), data) end -T["utils"]["should .trimend()"] = function() +utils["should .trimend()"] = function() local u = require "gopher._utils" t.eq(u.trimend " hi ", " hi") end
@@ -0,0 +1,20 @@
+version: "3" +tasks: + generate: + deps: + - comment + - iferr + - tags + - impl + + comment: + cmd: vhs comment.tape + + iferr: + cmd: vhs iferr.tape + + tags: + cmd: vhs tags.tape + + impl: + cmd: vhs impl.tape
@@ -0,0 +1,7 @@
+package demos + +func doSomethingImportant() {} + +type User struct{} + +type DataProvider interface{}
@@ -0,0 +1,34 @@
+Output comment.gif +Require nvim + +Set FontFamily "JetBrains Mono" +Set Height 800 +Set Width 1500 +Set Padding 20 +Set Shell "bash" +Set Theme "tokyonight" +Set TypingSpeed 250ms + +Hide +Type@0ms "alias nvim='./nvim.sh'" Enter +Type@0ms "clear" Enter +Show + +Type "nvim comment.go" Sleep 150ms Enter + +# package +Type ":GoCmt" Enter Sleep 500ms Escape Sleep 700ms + +# func +Type "3j" +Type ":GoCmt" Enter Sleep 500ms Escape Sleep 700ms + +# struct +Type "3j" +Type ":GoCmt" Enter Sleep 500ms Escape Sleep 700ms + +# interface +Type "3j" +Type ":GoCmt" Enter Sleep 500ms Escape Sleep 700ms + +Sleep 5s
@@ -0,0 +1,11 @@
+package demos + +func ifErr() { + out, err := doSomething() + + _ = out +} + +func doSomething() (string, error) { + return "", nil +}
@@ -0,0 +1,23 @@
+Output iferr.gif +Require nvim +Require iferr + +Set FontFamily "JetBrains Mono" +Set Height 800 +Set Width 1500 +Set Padding 20 +Set Shell "bash" +Set Theme "tokyonight" +Set TypingSpeed 250ms + +Hide +Type@0ms "alias nvim='./nvim.sh'" Enter +Type@0ms "clear" Enter +Show + +Type "nvim iferr.go" Sleep 150ms Enter + +Type "3j" +Type ":GoIfErr" Sleep 500ms Enter + +Sleep 5s
@@ -0,0 +1,23 @@
+Output impl.gif +Require nvim +Require iferr + +Set FontFamily "JetBrains Mono" +Set Height 800 +Set Width 1500 +Set Padding 20 +Set Shell "bash" +Set Theme "tokyonight" +Set TypingSpeed 250ms + +Hide +Type@0ms "alias nvim='./nvim.sh'" Enter +Type@0ms "clear" Enter +Show + +Type "nvim impl.go" Sleep 150ms Enter + +Type "2j" +Type ":GoImpl io.Reader" Sleep 500ms Enter + +Sleep 5s