Enhance your golang experience
Find a file
2024-02-28 18:47:29 +02:00
.github/workflows chore(ci): add more nvim versions to run on 2024-02-28 18:41:57 +02:00
autoload/health healthcheck: refactoring, remove deprecation wanings (#35) 2023-07-17 18:31:21 +03:00
doc ignore tags that mini.doc gens, but why? 2024-02-28 15:23:45 +02:00
lua/gopher refactor(api)!: mave tags and gotests api into their sub tables 2024-02-25 01:18:09 +02:00
plugin refactor(api)!: mave tags and gotests api into their sub tables 2024-02-25 01:18:09 +02:00
scripts this could be working but i still cant figure out how to run it 2024-02-28 15:22:48 +02:00
spec idk how good this idea is 2024-02-28 15:22:20 +02:00
.editorconfig reformat .editorconfig config 2024-02-11 17:49:13 +02:00
.gitignore feat: run tests independent from user's nvim config 2023-07-20 00:18:13 +03:00
.luarc.json run tests independent of user nvim setup (#39) 2023-07-21 02:57:46 +03:00
CONTRIBUTING.md docs: add contributing guide 2022-07-11 23:35:54 +03:00
nvim.toml Initial commit 2022-05-30 15:44:12 +03:00
README.md Add support for named tests (#50) 2024-02-11 16:35:39 +02:00
selene.toml chore: update taskfile, and linter config 2023-07-20 19:51:02 +03:00
stylua.toml Initial commit 2022-05-30 15:44:12 +03:00
Taskfile.yml chore: update taskfile 2024-02-28 18:47:29 +02:00

gopher.nvim

Stand With Ukraine

Minimalistic plugin for Go development in Neovim written in Lua.

It's not an LSP tool, the main goal of this plugin is add go tooling support in Neovim.

Install

Pre-dependency: go (tested on 1.17 and 1.18)

use {
  "olexsmir/gopher.nvim",
  requires = { -- dependencies
    "nvim-lua/plenary.nvim",
    "nvim-treesitter/nvim-treesitter",
  },
}

Also, run TSInstall go if go parser if isn't installed yet.

Config

By .setup function you can configure the plugin.

Note:

  • installer does not install the tool in user set path
require("gopher").setup {
  commands = {
    go = "go",
    gomodifytags = "gomodifytags",
    gotests = "~/go/bin/gotests", -- also you can set custom command path
    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)
    -- works only with gotests installed from develop branch
    named = false,
  },
}

Named tests with testify (using map instead of slice for test cases)

require("gopher").setup({
  gotests = {
    template = "testify",
    named = true
  }
})

For named tests to work you have to install gotests from develop branch, for example using mason-tool-installer:

 require('mason-tool-installer').setup({
  ensure_installed = {
    { "gotests", version = "develop" },
  }
})

Or by calling vim.fn.jobstart:

vim.fn.jobstart("go install github.com/cweill/gotests/...@develop")

If you're using lazy.nvim you can put in build function inside setup()

Features

  1. Installation requires this go tool:
:GoInstallDeps

It will install next tools:

  1. Modify struct tags: By default json tag will be added/removed, if not set:
:GoTagAdd json " For add json tag
:GoTagRm yaml " For remove yaml tag
  1. Run go mod command:
:GoMod tidy " Runs `go mod tidy`
:GoMod init asdf " Runs `go mod init asdf`
  1. Run go get command

Link can have a http or https prefix.

You can provide more than one package url:

:GoGet github.com/gorilla/mux
  1. Interface implementation

Command syntax:

:GoImpl [receiver] [interface]

" Also you can put cursor on the struct and run:
:GoImpl [interface]

Example of usage:

" Example
:GoImpl r Read io.Reader
" or simply put your cursor in the struct and run:
:GoImpl io.Reader
  1. Generate tests with gotests

Generate one test for a specific function/method:

:GoTestAdd

Generate all tests for all functions/methods in current file:

:GoTestsAll

Generate tests only for exported functions/methods in current file:

:GoTestsExp
  1. Run go generate command;
" Run `go generate` in cwd path
:GoGenerate

" Run `go generate` for current file
:GoGenerate %
  1. Generate doc comment

First set a cursor on public package/function/interface/struct and execute:

:GoCmt
  1. Generate if err

Set cursor on the line with err and execute:

:GoIfErr
  1. Setup nvim-dap for go in one line.

Notice: nvim-dap is required

require"gopher.dap".setup()

Contributing

PRs are always welcome. See CONTRIBUTING.md

Thanks