add help file, and docs (#59)

* idk how good this idea is

* this could be working but i still cant figure out how to run it

* ignore tags that mini.doc gens, but why?

* chore(taskfile): force exiting after tests

because i got infinit ci

* chore(ci): add more nvim versions to run on

* chore: update taskfile

* feat: add docs generator

* docs: its only begining

* refactor: update docgen script

* docs: write some more

* docs(config): update

* docs: update readme

* language

* hope it would work

* what about that?

* maybe this would work?

* update md

* upd

* WHY DOESNT IT WORKING

* idk by but 0.9.3 just fails the ci, so i deleted it from suite

* again update, why does markdown not work in embeded html

* maybe it can help?

* upd

* again update

* kinda fix

* fix: formatting

* again some updating

* some readme updating

* fix, this shouldnt be in repo

* i finnaly undertood how to fix this fking skill issue

* fix(struct_tags): typo

* refactor(docs): change the order in generated file

* docs: install deps

* refactor(scripts): rename doc-gen script

* docs(impl): write docs

* docs(dap): add doc

* stylua .

* docs(struct_tags): add doc

* docs(gotests): add docs

* docs(iferr): add docs

* docs(comment): add doc

* update CONTRIBUTING.md

* docs(README): talk about `develop` branch

* docs: update README.md
This commit is contained in:
Smirnov Oleksandr 2024-04-04 17:15:55 +03:00 committed by GitHub
parent 28e1f5689f
commit 10cec9c6b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 657 additions and 142 deletions

View file

@ -16,11 +16,20 @@ jobs:
- v0.8.3 - v0.8.3
- v0.9.0 - v0.9.0
- v0.9.1 - v0.9.1
- v0.9.2
- v0.9.4
- v0.9.5
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:
- name: Install Task
uses: arduino/setup-task@v1
with:
version: 3.x
repo-token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- name: Install Neovim - name: Install Neovim
shell: bash
run: | run: |
mkdir -p /tmp/nvim mkdir -p /tmp/nvim
wget -q https://github.com/neovim/neovim/releases/download/${{ matrix.nvim_version }}/nvim.appimage -O /tmp/nvim/nvim.appimage wget -q https://github.com/neovim/neovim/releases/download/${{ matrix.nvim_version }}/nvim.appimage -O /tmp/nvim/nvim.appimage
@ -28,7 +37,8 @@ jobs:
chmod a+x ./nvim.appimage chmod a+x ./nvim.appimage
./nvim.appimage --appimage-extract ./nvim.appimage --appimage-extract
echo "/tmp/nvim/squashfs-root/usr/bin/" >> $GITHUB_PATH echo "/tmp/nvim/squashfs-root/usr/bin/" >> $GITHUB_PATH
- name: Run Tests - name: Run Tests
run: | run: |
nvim --version nvim --version
nvim --headless -u ./spec/minimal_init.lua -c "PlenaryBustedDirectory spec {minimal_init='./spec/minimal_init.lua', sequential=true}" task test

View file

@ -1,6 +1,14 @@
# Contributing to `gopher.nvim` # Contributing to `gopher.nvim`
Thank you for looking to contributing 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 ### Styling and formatting
@ -8,6 +16,40 @@ Code is formatted by [stylua](https://github.com/JohnnyMorganz/StyLua) and linte
You can install these with: You can install these with:
```bash ```bash
cargo install stylua sudo pacman -S selene stylua
cargo install selene # or whatever is your package manager
# or way of installing pkgs
```
For formatting use this following commands, or setup your editor to integrate with selene/stylua:
```bash
task format
task format:check # will check if your code formatted
task lint
```
### Documentation
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
You can generate docs with:
```bash
task docgen
```
### Commit messages
We use [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/), please follow it.
### Testing
For testing this plugins uses [plenary.nvim](https://github.com/nvim-lua/plenary.nvim).
All tests live in [/spec](https://github.com/olexsmir/gopher.nvim/tree/main/spec) dir.
You can run tests with:
```bash
task test
# also there are some aliases for that
task tests
task spec
``` ```

266
README.md
View file

@ -4,40 +4,54 @@
Minimalistic plugin for Go development in Neovim written in Lua. 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. It's **NOT** an LSP tool, the main goal of this plugin is to add go tooling support in Neovim.
## Install > 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.
Pre-dependency: [go](https://github.com/golang/go) (tested on 1.17 and 1.18) ## Install (using [lazy.nvim](https://github.com/folke/lazy.nvim))
Pre-dependency:
- [Go](https://github.com/golang/go)
- `go` treesitter parser, install by `:TSInstall go`
```lua ```lua
use { {
"olexsmir/gopher.nvim", "olexsmir/gopher.nvim",
requires = { -- dependencies ft = "go",
-- branch = "develop", -- if you want develop branch
-- keep in mind, it might break everything
dependencies = {
"nvim-lua/plenary.nvim", "nvim-lua/plenary.nvim",
"nvim-treesitter/nvim-treesitter", "nvim-treesitter/nvim-treesitter",
"mfussenegger/nvim-dap", -- (optional) only if you use `gopher.dap`
}, },
-- (optional) will update plugin's deps on every update
build = function()
vim.cmd.GoInstallDeps()
end,
---@type gopher.Config
opts = {},
} }
``` ```
Also, run `TSInstall go` if `go` parser if isn't installed yet. ## Configuratoin
## Config > [!IMPORTANT]
>
> If you need more info look `:h gopher.nvim`
By `.setup` function you can configure the plugin. **Take a look at default options**
Note:
- `installer` does not install the tool in user set path
```lua ```lua
require("gopher").setup { require("gopher").setup {
commands = { commands = {
go = "go", go = "go",
gomodifytags = "gomodifytags", gomodifytags = "gomodifytags",
gotests = "~/go/bin/gotests", -- also you can set custom command path gotests = "gotests",
impl = "impl", impl = "impl",
iferr = "iferr", iferr = "iferr",
dlv = "dlv",
}, },
gotests = { gotests = {
-- gotests doesn't have template named "default" so this plugin uses "default" to set the default template -- gotests doesn't have template named "default" so this plugin uses "default" to set the default template
@ -48,151 +62,170 @@ require("gopher").setup {
-- works only with gotests installed from develop branch -- works only with gotests installed from develop branch
named = false, named = false,
}, },
gotag = {
transform = "snakecase",
},
} }
``` ```
### Named tests with testify (using map instead of slice for test cases)
```lua
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](https://github.com/WhoIsSethDaniel/mason-tool-installer.nvim):
```lua
require('mason-tool-installer').setup({
ensure_installed = {
{ "gotests", version = "develop" },
}
})
```
Or by calling `vim.fn.jobstart`:
```lua
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 ## Features
1. Installation requires this go tool: <!-- markdownlint-disable -->
```vim <details>
:GoInstallDeps <summary>
``` <b>Install plugin's go deps</b>
</summary>
It will install next tools: ```vim
:GoInstallDeps
```
- [gomodifytags](https://github.com/fatih/gomodifytags) This will install the following tools:
- [impl](https://github.com/josharian/impl)
- [gotests](https://github.com/cweill/gotests) - [gomodifytags](https://github.com/fatih/gomodifytags)
- [iferr](https://github.com/koron/iferr) - [impl](https://github.com/josharian/impl)
- [gotests](https://github.com/cweill/gotests)
- [iferr](https://github.com/koron/iferr)
- [dlv](github.com/go-delve/delve/cmd/dlv)
</details>
<details>
<summary>
<b>Add and remove tags for structs via <a href="https://github.com/fatih/gomodifytags">gomodifytags</a></b>
</summary>
2. Modify struct tags:
By default `json` tag will be added/removed, if not set: By default `json` tag will be added/removed, if not set:
```vim ```vim
:GoTagAdd json " For add json tag " add json tag
:GoTagRm yaml " For remove yaml tag :GoTagAdd json
```
3. Run `go mod` command: " remove yaml tag
:GoTagRm yaml
```
```vim ```lua
:GoMod tidy " Runs `go mod tidy` -- or you can use lua api
:GoMod init asdf " Runs `go mod init asdf` require("gopher").tags.add "xml"
``` require("gopher").tags.rm "proto"
```
</details>
4. Run `go get` command <details>
<summary>
<b>Generating tests via <a href="https://github.com/cweill/gotests">gotests</a></b>
</summary>
Link can have a `http` or `https` prefix. ```vim
" Generate one test for a specific function/method(one under cursor)
:GoTestAdd
You can provide more than one package url: " Generate all tests for all functions/methods in the current file
:GoTestsAll
```vim " Generate tests for only exported functions/methods in the current file
:GoGet github.com/gorilla/mux :GoTestsExp
``` ```
5. Interface implementation ```lua
-- or you can use lua api
require("gopher").test.add()
require("gopher").test.exported()
require("gopher").test.all()
```
Command syntax: For named tests see `:h gopher.nvim-gotests-named`
</details>
```vim <details>
:GoImpl [receiver] [interface] <summary>
<b>Run commands like <code>go mod/get/etc</code> inside of nvim</b>
</summary>
" Also you can put cursor on the struct and run: ```vim
:GoImpl [interface] :GoGet github.com/gorilla/mux
```
Example of usage: " Link can have an `http` or `https` prefix.
:GoGet https://github.com/lib/pq
```vim " You can provide more than one package url
" Example :GoGet github.com/jackc/pgx/v5 github.com/google/uuid/
:GoImpl r Read io.Reader
" or simply put your cursor in the struct and run:
:GoImpl io.Reader
```
6. Generate tests with [gotests](https://github.com/cweill/gotests) " go mod commands
:GoMod tidy
:GoMod init new-shiny-project
Generate one test for a specific function/method: " go work commands
:GoWork sync
```vim " run go generate in cwd
:GoTestAdd :GoGenerate
```
Generate all tests for all functions/methods in current file: " run go generate for the current file
:GoGenerate %
```
</details>
```vim <details>
:GoTestsAll <summary>
``` <b>Interface implementation via <a href="https://github.com/josharian/impl">impl<a></b>
</summary>
Generate tests only for exported functions/methods in current file: Syntax of the command:
```vim
:GoImpl [receiver] [interface]
```vim " also you can put a cursor on the struct and run
:GoTestsExp :GoImpl [interface]
``` ```
7. Run `go generate` command; Usage examples:
```vim
:GoImpl r Read io.Reader
:GoImpl Write io.Writer
```vim " or you can simply put a cursor on the struct and run
" Run `go generate` in cwd path :GoImpl io.Reader
:GoGenerate ```
</details>
" Run `go generate` for current file <details>
:GoGenerate % <summary>
``` <b>Generate boilerplate for doc comments</b>
</summary>
8. Generate doc comment First set a cursor on **public** package/function/interface/struct and execute:
First set a cursor on **public** package/function/interface/struct and execute: ```vim
:GoCmt
```
</details>
```vim
:GoCmt
```
9. Generate `if err` <details>
<summary>
<b>Generate <code>if err != nil {</code> via <a href="https://github.com/koron/iferr">iferr</a></b>
</summary>
Set cursor on the line with **err** and execute: Set the cursor on the line with `err` and execute
```vim ```vim
:GoIfErr :GoIfErr
``` ```
</details>
10. Setup nvim-dap for go in one line. <details>
<summary>
<b>Setup <a href="https://github.com/mfussenegger/nvim-dap">nvim-dap</a> for go in one line</b>
</summary>
Notice: [nvim-dap](https://github.com/mfussenegger/nvim-dap) is required note [nvim-dap](https://github.com/mfussenegger/nvim-dap) has to be installed
```lua ```lua
require"gopher.dap".setup() require("gopher.dap").setup()
``` ```
</details>
## Contributing ## Contributing
@ -202,3 +235,4 @@ PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md)
- [go.nvim](https://github.com/ray-x/go.nvim) - [go.nvim](https://github.com/ray-x/go.nvim)
- [nvim-dap-go](https://github.com/leoluz/nvim-dap-go) - [nvim-dap-go](https://github.com/leoluz/nvim-dap-go)
- [iferr](https://github.com/koron/iferr)

View file

@ -2,21 +2,29 @@ version: "3"
tasks: tasks:
format: format:
desc: formats all lua files in repo desc: formats all lua files in repo
cmds: [stylua .] cmds:
- stylua .
lint: lint:
desc: runs all linters desc: runs all linters
cmds: cmds:
- task: lint_selene - task: selene
- task: lint_stylua - task: stylua:check
lint_selene: selene:
desc: runs lua linter(selene) desc: runs lua linter(selene)
cmds: [selene .] cmds:
- selene .
lint_stylua: stylua:check:
desc: runs stylua in check mode desc: runs stylua in check mode
cmds: [stylua --check .] cmds:
- stylua --check .
stylua:
desc: runs lua formatter
cmds:
- stylua .
test: test:
desc: runs all tests desc: runs all tests
@ -24,7 +32,18 @@ tasks:
cmds: cmds:
- | - |
nvim --headless \ nvim --headless \
-u ./spec/minimal_init.lua\ -u ./scripts/minimal_init.lua \
-c "PlenaryBustedDirectory spec \ -c "PlenaryBustedDirectory spec \
{minimal_init='./spec/minimal_init.lua'\ {minimal_init='./scripts/minimal_init.lua' \
,sequential=true}" ,sequential=true}" \
-c ":qa!"
docgen:
desc: generate vimhelp
cmds:
- |
nvim --noplugin \
--headless \
-u "./scripts/minimal_init.lua" \
-c "luafile ./scripts/docgen.lua" \
-c ":qa!"

1
doc/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/tags

220
doc/gopher.nvim.txt Normal file
View file

@ -0,0 +1,220 @@
*gopher.nvim*
==============================================================================
gopher.nvim is a 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.
------------------------------------------------------------------------------
*gopher.nvim-table-of-contents*
Table of Contents
Setup....................................................|gopher.nvim-setup|
Install dependencies..............................|gopher.nvim-install-deps|
Configuration...........................................|gopher.nvim-config|
Modifty struct tags................................|gopher.nvim-struct-tags|
Auto implementation of interface methods..................|gopher.nvim-impl|
Generating unit tests boilerplate......................|gopher.nvim-gotests|
Iferr....................................................|gopher.nvim-iferr|
Generate comments.....................................|gopher.nvim-comments|
Setup `nvim-dap` for Go......................................|gopher.nvim-dap|
------------------------------------------------------------------------------
*gopher.nvim-setup*
`gopher.setup`
Setup function. This method simply merges default configs with opts table.
You can read more about configuration at |gopher.nvim-config|
Calling this function is optional, if you ok with default settings. Look |gopher.nvim.config-defaults|
Usage ~
`require("gopher").setup {}` (replace `{}` with your `config` table)
------------------------------------------------------------------------------
*gopher.nvim-install-deps*
`gopher.install_deps`
Gopher.nvim implements most of its features using third-party tools.
To install these tools, you can run `:GoInstallDeps` command
or call `require("gopher").install_deps()` if you want ues lua api.
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-config*
config it is the place where you can configure the plugin.
also this is optional is you're ok with default settings.
You can look at default options |gopher.nvim-config-defaults|
------------------------------------------------------------------------------
*gopher.nvim-config-defaults*
`default_config`
>lua
local default_config = {
--minidoc_replace_end
-- user specified paths to binaries
---@class gopher.ConfigCommand
commands = {
go = "go",
gomodifytags = "gomodifytags",
gotests = "gotests",
impl = "impl",
iferr = "iferr",
dlv = "dlv",
},
---@class gopher.ConfigGotests
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
---@type string|nil
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,
},
---@class gopher.ConfigGoTag
gotag = {
---@type gopher.ConfigGoTagTransform
transform = "snakecase",
},
}
<
Class ~
{gopher.Config}
------------------------------------------------------------------------------
*config.setup()*
`config.setup`({user_config})
Parameters ~
{user_config} `(optional)` gopher.Config
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-struct-tags*
struct-tags is utilizing the `gomodifytags` tool to add or remove tags to struct fields.
Usage ~
- put your coursor on the struct
- run `:GoTagAdd json` to add json tags to struct fields
- run `:GoTagRm json` to remove json tags to struct fields
note: if you dont spesify the tag it will use `json` as default
simple example:
>go
// before
type User struct {
// ^ put your cursor here
// run `:GoTagAdd yaml`
ID int
Name string
}
// after
type User struct {
ID int `yaml:id`
Name string `yaml:name`
}
<
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-impl*
impl is utilizing the `impl` tool to generate method stubs for interfaces.
Usage ~
1. put your coursor on the struct on which you want implement the interface
and run `:GoImpl io.Reader`
which will automatically choose the reciver for the methods and
implement the `io.Reader` interface
2. same as previous but with custom receiver, so put your coursor on the struct
run `:GoImpl w io.Writer`
where `w` is the receiver and `io.Writer` is the interface
3. specift receiver, struct, and interface
there's no need to put your coursor on the struct if you specify all arguments
`:GoImpl r RequestReader io.Reader`
where `r` is the receiver, `RequestReader` is the struct and `io.Reader` is the interface
simple example:
>go
type BytesReader struct{}
// ^ put your cursor here
// run `:GoImpl b io.Reader`
// this is what you will get
func (b *BytesReader) Read(p []byte) (n int, err error) {
panic("not implemented") // TODO: Implement
}
<
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-gotests*
gotests is utilizing the `gotests` tool to generate unit tests boilerplate.
Usage ~
- generate unit test for spesisfic function/method
- to specift the function/method put your cursor on it
- run `:GoTestAdd`
- generate unit tests for all functions/methods in current file
- run `:GoTestsAll`
- generate unit tests only for exported(public) functions/methods
- run `:GoTestsExp`
you can also specify the template to use for generating the tests. see |gopher.nvim-config|
more details about templates can be found at: https://github.com/cweill/gotests
------------------------------------------------------------------------------
*gopher.nvim-gotests-named*
if you prefare using named tests, you can enable it in the config.
but you would need to install `gotests@develop` because stable version doesn't support this feature.
you can do it with:
>lua
-- simply run go get in your shell:
go install github.com/cweill/gotests/...@develop
-- if you want to install it within neovim, you can use one of this:
vim.fn.jobstart("go install github.com/cweill/gotests/...@develop")
-- or if you want to use mason:
require("mason-tool-installer").setup {
ensure_installed = {
{ "gotests", version = "develop" },
}
}
<
if you choose to install `gotests` within neovim, i recommend adding it to your `build` section in your |lazy.nvim|
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-iferr*
if you're using `iferr` tool, this module provides a way to automatically insert `if err != nil` check.
Usage ~
execute `:GoIfErr` near any err variable to insert the check
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-comments*
Usage ~
Execute `:GoCmt` to generate a comment for the current function/method/struct/etc on this line.
This module provides a way to generate comments for Go code.
==============================================================================
------------------------------------------------------------------------------
*gopher.nvim-dap*
This module sets up `nvim-dap` for Go.
Usage ~
just call `require("gopher.dap").setup()`, and you're good to go.
vim:tw=78:ts=8:noet:ft=help:norl:

View file

@ -1,3 +1,8 @@
---@toc_entry Generate comments
---@tag gopher.nvim-comments
---@usage Execute `:GoCmt` to generate a comment for the current function/method/struct/etc on this line.
---@text This module provides a way to generate comments for Go code.
local function generate(row, col) local function generate(row, col)
local ts_utils = require "gopher._utils.ts" local ts_utils = require "gopher._utils.ts"
local comment, ns = nil, nil local comment, ns = nil, nil

View file

@ -1,6 +1,17 @@
---@toc_entry Configuration
---@tag gopher.nvim-config
---@text config it is the place where you can configure the plugin.
--- also this is optional is you're ok with default settings.
--- You can look at default options |gopher.nvim-config-defaults|
---@type gopher.Config ---@type gopher.Config
---@private
local config = {} local config = {}
---@tag gopher.nvim-config.ConfigGoTagTransform
---@text Possible values for |gopher.Config|.gotag.transform:
---
---@private
---@alias gopher.ConfigGoTagTransform ---@alias gopher.ConfigGoTagTransform
---| "snakecase" "GopherUser" -> "gopher_user" ---| "snakecase" "GopherUser" -> "gopher_user"
---| "camelcase" "GopherUser" -> "gopherUser" ---| "camelcase" "GopherUser" -> "gopherUser"
@ -9,8 +20,16 @@ local config = {}
---| "titlecase" "GopherUser" -> "Gopher User" ---| "titlecase" "GopherUser" -> "Gopher User"
---| "keep" keeps the original field name ---| "keep" keeps the original field name
--minidoc_replace_start {
---@tag gopher.nvim-config-defaults
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section):gsub(">", ">lua")
---
---@class gopher.Config ---@class gopher.Config
local default_config = { local default_config = {
--minidoc_replace_end
-- user specified paths to binaries
---@class gopher.ConfigCommand ---@class gopher.ConfigCommand
commands = { commands = {
go = "go", go = "go",
@ -29,7 +48,6 @@ local default_config = {
template_dir = nil, template_dir = nil,
-- switch table tests from using slice to map (with test name for the key) -- switch table tests from using slice to map (with test name for the key)
-- works only with gotests installed from develop branch -- works only with gotests installed from develop branch
---@type boolean
named = false, named = false,
}, },
---@class gopher.ConfigGoTag ---@class gopher.ConfigGoTag
@ -38,8 +56,10 @@ local default_config = {
transform = "snakecase", transform = "snakecase",
}, },
} }
--minidoc_afterlines_end
---@type gopher.Config ---@type gopher.Config
---@private
local _config = default_config local _config = default_config
---@param user_config? gopher.Config ---@param user_config? gopher.Config

View file

@ -1,3 +1,8 @@
---@toc_entry Setup `nvim-dap` for Go
---@tag gopher.nvim-dap
---@text This module sets up `nvim-dap` for Go.
---@usage just call `require("gopher.dap").setup()`, and you're good to go.
local u = require "gopher._utils" local u = require "gopher._utils"
local c = require "gopher.config" local c = require "gopher.config"
local dap = {} local dap = {}
@ -106,7 +111,7 @@ dap.configuration = {
}, },
} }
---setup `nvim-dap` for Go -- sets ups nvim-dap for Go in one function call.
function dap.setup() function dap.setup()
local d = u.sreq "dap" local d = u.sreq "dap"

View file

@ -1,3 +1,44 @@
---@toc_entry Generating unit tests boilerplate
---@tag gopher.nvim-gotests
---@text gotests is utilizing the `gotests` tool to generate unit tests boilerplate.
---@usage
--- - generate unit test for spesisfic function/method
--- - to specift the function/method put your cursor on it
--- - run `:GoTestAdd`
---
--- - generate unit tests for all functions/methods in current file
--- - run `:GoTestsAll`
---
--- - generate unit tests only for exported(public) functions/methods
--- - run `:GoTestsExp`
---
--- you can also specify the template to use for generating the tests. see |gopher.nvim-config|
--- more details about templates can be found at: https://github.com/cweill/gotests
---
---@tag gopher.nvim-gotests-named
---@text
--- if you prefare using named tests, you can enable it in the config.
--- but you would need to install `gotests@develop` because stable version doesn't support this feature.
--- you can do it with:
--- >lua
--- -- simply run go get in your shell:
--- go install github.com/cweill/gotests/...@develop
---
--- -- if you want to install it within neovim, you can use one of this:
---
--- vim.fn.jobstart("go install github.com/cweill/gotests/...@develop")
---
--- -- or if you want to use mason:
--- require("mason-tool-installer").setup {
--- ensure_installed = {
--- { "gotests", version = "develop" },
--- }
--- }
--- <
---
--- if you choose to install `gotests` within neovim, i recommend adding it to your `build` section in your |lazy.nvim|
local c = require "gopher.config" local c = require "gopher.config"
local ts_utils = require "gopher._utils.ts" local ts_utils = require "gopher._utils.ts"
local r = require "gopher._utils.runner" local r = require "gopher._utils.runner"
@ -5,6 +46,7 @@ local u = require "gopher._utils"
local gotests = {} local gotests = {}
---@param args table ---@param args table
---@private
local function add_test(args) local function add_test(args)
if c.gotests.named then if c.gotests.named then
table.insert(args, "-named") table.insert(args, "-named")
@ -35,7 +77,7 @@ local function add_test(args)
}) })
end end
---generate unit test for one function -- generate unit test for one function
function gotests.func_test() function gotests.func_test()
local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_func_method_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil or ns.name == nil then if ns == nil or ns.name == nil then
@ -46,12 +88,12 @@ function gotests.func_test()
add_test { "-only", ns.name } add_test { "-only", ns.name }
end end
---generate unit tests for all functions in current file -- generate unit tests for all functions in current file
function gotests.all_tests() function gotests.all_tests()
add_test { "-all" } add_test { "-all" }
end end
---generate unit tests for all exported functions -- generate unit tests for all exported functions
function gotests.all_exported_tests() function gotests.all_exported_tests()
add_test { "-exported" } add_test { "-exported" }
end end

View file

@ -1,7 +1,12 @@
---@toc_entry Iferr
---@tag gopher.nvim-iferr
---@text if you're using `iferr` tool, this module provides a way to automatically insert `if err != nil` check.
---@usage execute `:GoIfErr` near any err variable to insert the check
local c = require "gopher.config" local c = require "gopher.config"
local iferr = {} local iferr = {}
-- That's Lua of vimscript implementation of: github.com/koron/iferr -- That's Lua implementation: github.com/koron/iferr
function iferr.iferr() function iferr.iferr()
local boff = vim.fn.wordcount().cursor_bytes local boff = vim.fn.wordcount().cursor_bytes
local pos = vim.fn.getcurpos()[2] local pos = vim.fn.getcurpos()[2]

View file

@ -1,3 +1,31 @@
---@toc_entry Auto implementation of interface methods
---@tag gopher.nvim-impl
---@text impl is utilizing the `impl` tool to generate method stubs for interfaces.
---@usage
--- 1. put your coursor on the struct on which you want implement the interface
--- and run `:GoImpl io.Reader`
--- which will automatically choose the reciver for the methods and
--- implement the `io.Reader` interface
--- 2. same as previous but with custom receiver, so put your coursor on the struct
--- run `:GoImpl w io.Writer`
--- where `w` is the receiver and `io.Writer` is the interface
--- 3. specift receiver, struct, and interface
--- there's no need to put your coursor on the struct if you specify all arguments
--- `:GoImpl r RequestReader io.Reader`
--- where `r` is the receiver, `RequestReader` is the struct and `io.Reader` is the interface
---
--- simple example:
--- >go
--- type BytesReader struct{}
--- // ^ put your cursor here
--- // run `:GoImpl b io.Reader`
---
--- // this is what you will get
--- func (b *BytesReader) Read(p []byte) (n int, err error) {
--- panic("not implemented") // TODO: Implement
--- }
--- <
local c = require("gopher.config").commands local c = require("gopher.config").commands
local r = require "gopher._utils.runner" local r = require "gopher._utils.runner"
local ts_utils = require "gopher._utils.ts" local ts_utils = require "gopher._utils.ts"
@ -5,6 +33,7 @@ local u = require "gopher._utils"
local impl = {} local impl = {}
---@return string ---@return string
---@private
local function get_struct() local function get_struct()
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0))) local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then if ns == nil then

View file

@ -1,10 +1,35 @@
--- *gopher.nvim*
---
--- ==============================================================================
---
--- gopher.nvim is a 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.
--- Table of Contents
---@tag gopher.nvim-table-of-contents
---@toc
local tags = require "gopher.struct_tags" local tags = require "gopher.struct_tags"
local tests = require "gopher.gotests" local tests = require "gopher.gotests"
local gocmd = require("gopher._utils.runner.gocmd").run local gocmd = require("gopher._utils.runner.gocmd").run
local gopher = {} local gopher = {}
---@toc_entry Setup
---@tag gopher.nvim-setup
---@text Setup function. This method simply merges default configs with opts table.
--- You can read more about configuration at |gopher.nvim-config|
--- Calling this function is optional, if you ok with default settings. Look |gopher.nvim.config-defaults|
---
---@usage `require("gopher").setup {}` (replace `{}` with your `config` table)
gopher.setup = require("gopher.config").setup gopher.setup = require("gopher.config").setup
---@toc_entry Install dependencies
---@tag gopher.nvim-install-deps
---@text Gopher.nvim implements most of its features using third-party tools.
--- To install these tools, you can run `:GoInstallDeps` command
--- or call `require("gopher").install_deps()` if you want ues lua api.
gopher.install_deps = require("gopher.installer").install_deps gopher.install_deps = require("gopher.installer").install_deps
gopher.impl = require("gopher.impl").impl gopher.impl = require("gopher.impl").impl
gopher.iferr = require("gopher.iferr").iferr gopher.iferr = require("gopher.iferr").iferr
gopher.comment = require "gopher.comment" gopher.comment = require "gopher.comment"

View file

@ -1,3 +1,29 @@
---@toc_entry Modifty struct tags
---@tag gopher.nvim-struct-tags
---@text struct-tags is utilizing the `gomodifytags` tool to add or remove tags to struct fields.
---@usage - put your coursor on the struct
--- - run `:GoTagAdd json` to add json tags to struct fields
--- - run `:GoTagRm json` to remove json tags to struct fields
---
--- note: if you dont spesify the tag it will use `json` as default
---
--- simple example:
--- >go
--- // before
--- type User struct {
--- // ^ put your cursor here
--- // run `:GoTagAdd yaml`
--- ID int
--- Name string
--- }
---
--- // after
--- type User struct {
--- ID int `yaml:id`
--- Name string `yaml:name`
--- }
--- <
local ts_utils = require "gopher._utils.ts" local ts_utils = require "gopher._utils.ts"
local r = require "gopher._utils.runner" local r = require "gopher._utils.runner"
local c = require "gopher.config" local c = require "gopher.config"
@ -56,7 +82,7 @@ local function modify(...)
or tagged["start"] == nil or tagged["start"] == nil
or tagged["start"] == 0 or tagged["start"] == 0
then then
error("failed to set tags " .. vim.inspec(tagged)) error("failed to set tags " .. vim.inspect(tagged))
end end
vim.api.nvim_buf_set_lines( vim.api.nvim_buf_set_lines(
@ -69,8 +95,7 @@ local function modify(...)
vim.cmd "write" vim.cmd "write"
end end
---add tags to struct under cursor -- add tags to struct under cursor
---@param ... unknown
function struct_tags.add(...) function struct_tags.add(...)
local arg = { ... } local arg = { ... }
if #arg == nil or arg == "" then if #arg == nil or arg == "" then
@ -85,8 +110,7 @@ function struct_tags.add(...)
modify(unpack(cmd_args)) modify(unpack(cmd_args))
end end
---remove tags to struct under cursor -- remove tags to struct under cursor
---@param ... unknown
function struct_tags.remove(...) function struct_tags.remove(...)
local arg = { ... } local arg = { ... }
if #arg == nil or arg == "" then if #arg == nil or arg == "" then

33
scripts/docgen.lua Normal file
View file

@ -0,0 +1,33 @@
---@diagnostic disable: undefined-global
--# selene: allow(undefined_variable)
local okay, minidoc = pcall(require, "mini.doc")
if not okay then
error "mini.doc not found, please install it. https://github.com/echasnovski/mini.doc"
return
end
local files = {
"lua/gopher/init.lua",
"lua/gopher/config.lua",
"lua/gopher/struct_tags.lua",
"lua/gopher/impl.lua",
"lua/gopher/gotests.lua",
"lua/gopher/iferr.lua",
"lua/gopher/comment.lua",
"lua/gopher/dap.lua",
}
minidoc.setup()
local hooks = vim.deepcopy(minidoc.default_hooks)
hooks.write_pre = function(lines)
-- Remove first two lines with `======` and `------` delimiters to comply
-- with `:h local-additions` template
table.remove(lines, 1)
table.remove(lines, 1)
return lines
end
MiniDoc.generate(files, "doc/gopher.nvim.txt", { hooks = hooks })

View file

@ -26,6 +26,7 @@ vim.notify = print
install_plug "nvim-lua/plenary.nvim" install_plug "nvim-lua/plenary.nvim"
install_plug "nvim-treesitter/nvim-treesitter" install_plug "nvim-treesitter/nvim-treesitter"
install_plug "echasnovski/mini.doc" -- used for docs generation
vim.env.XDG_CONFIG_HOME = root ".tests/config" vim.env.XDG_CONFIG_HOME = root ".tests/config"
vim.env.XDG_DATA_HOME = root ".tests/data" vim.env.XDG_DATA_HOME = root ".tests/data"