Merge pull request #129 (v0.4.0)
This commit is contained in:
commit
4a2384ade8
51 changed files with 793 additions and 239 deletions
1
.github/workflows/tests.yml
vendored
1
.github/workflows/tests.yml
vendored
|
|
@ -21,6 +21,7 @@ jobs:
|
||||||
- v0.11.1
|
- v0.11.1
|
||||||
- v0.11.2
|
- v0.11.2
|
||||||
- v0.11.3
|
- v0.11.3
|
||||||
|
- v0.11.4
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
steps:
|
steps:
|
||||||
- name: Install Task
|
- name: Install Task
|
||||||
|
|
|
||||||
47
README.md
47
README.md
|
|
@ -8,6 +8,13 @@ It's **NOT** an LSP tool, the goal of this plugin is to add go tooling support i
|
||||||
|
|
||||||
> All development of new and maybe undocumented, and unstable features is happening on [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.
|
||||||
|
|
||||||
|
## Table of content
|
||||||
|
* [How to install](#install-using-lazynvim)
|
||||||
|
* [Features](#features)
|
||||||
|
* [Configuration](#configuration)
|
||||||
|
* [Troubleshooting](#troubleshooting)
|
||||||
|
* [Contributing](#contributing)
|
||||||
|
|
||||||
## Install (using [lazy.nvim](https://github.com/folke/lazy.nvim))
|
## Install (using [lazy.nvim](https://github.com/folke/lazy.nvim))
|
||||||
|
|
||||||
Requirements:
|
Requirements:
|
||||||
|
|
@ -16,14 +23,19 @@ Requirements:
|
||||||
- Treesitter parser for `go`(`:TSInstall go` if you use [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter))
|
- 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
|
- [Go](https://github.com/golang/go) installed
|
||||||
|
|
||||||
|
> [!IMPORTANT]
|
||||||
|
> If you prefer using other forges, this repository is also mirrored at:
|
||||||
|
> - [tangled.org](https://tangled.org): [`https://tangled.org/olexsmir.xyz/gopher.nvim`](https://tangled.org/olexsmir.xyz/gopher.nvim)
|
||||||
|
> - [codeberg.org](https://codeberg.org): [`https://codeberg.org/olexsmir/gopher.nvim`](https://codeberg.org/olexsmir/gopher.nvim)
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
-- NOTE: this plugin is already lazy-loaded, it adds only about 1ms of load
|
-- NOTE: this plugin is already lazy-loaded and adds only about 1ms
|
||||||
-- time to your config
|
-- of load time to your config
|
||||||
{
|
{
|
||||||
"olexsmir/gopher.nvim",
|
"olexsmir/gopher.nvim",
|
||||||
ft = "go",
|
ft = "go",
|
||||||
-- branch = "develop"
|
-- branch = "develop"
|
||||||
-- (optional) will update plugin's deps on every update
|
-- (optional) updates the plugin's dependencies on each update
|
||||||
build = function()
|
build = function()
|
||||||
vim.cmd.GoInstallDeps()
|
vim.cmd.GoInstallDeps()
|
||||||
end,
|
end,
|
||||||
|
|
@ -65,6 +77,9 @@ Requirements:
|
||||||
" add json tag
|
" add json tag
|
||||||
:GoTagAdd json
|
:GoTagAdd json
|
||||||
|
|
||||||
|
" add json tag with omitempty option
|
||||||
|
:GoTagAdd json=omitempty
|
||||||
|
|
||||||
" remove yaml tag
|
" remove yaml tag
|
||||||
:GoTagRm yaml
|
:GoTagRm yaml
|
||||||
```
|
```
|
||||||
|
|
@ -201,6 +216,10 @@ require("gopher").setup {
|
||||||
-- timeout for running internal commands
|
-- timeout for running internal commands
|
||||||
timeout = 2000,
|
timeout = 2000,
|
||||||
|
|
||||||
|
-- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
|
||||||
|
installer_timeout = 999999,
|
||||||
|
|
||||||
|
-- user specified paths to binaries
|
||||||
commands = {
|
commands = {
|
||||||
go = "go",
|
go = "go",
|
||||||
gomodifytags = "gomodifytags",
|
gomodifytags = "gomodifytags",
|
||||||
|
|
@ -209,25 +228,41 @@ require("gopher").setup {
|
||||||
iferr = "iferr",
|
iferr = "iferr",
|
||||||
},
|
},
|
||||||
gotests = {
|
gotests = {
|
||||||
-- gotests doesn't have template named "default" so this plugin uses "default" to set the default template
|
-- a default template that gotess will use.
|
||||||
|
-- gotets doesn't have template named `default`, we use it to represent absence of the provided template.
|
||||||
template = "default",
|
template = "default",
|
||||||
|
|
||||||
-- path to a directory containing custom test code templates
|
-- path to a directory containing custom test code templates
|
||||||
template_dir = nil,
|
template_dir = nil,
|
||||||
-- switch table tests from using slice to map (with test name for the key)
|
|
||||||
|
-- use named tests(map with test name as key) in table tests(slice of structs by default)
|
||||||
named = false,
|
named = false,
|
||||||
},
|
},
|
||||||
gotag = {
|
gotag = {
|
||||||
transform = "snakecase",
|
transform = "snakecase",
|
||||||
|
|
||||||
-- default tags to add to struct fields
|
-- default tags to add to struct fields
|
||||||
default_tag = "json",
|
default_tag = "json",
|
||||||
|
|
||||||
|
-- default tag option added struct fields, set to nil to disable
|
||||||
|
-- e.g: `option = "json=omitempty,xml=omitempty`
|
||||||
|
option = nil,
|
||||||
},
|
},
|
||||||
iferr = {
|
iferr = {
|
||||||
-- choose a custom error message
|
-- choose a custom error message, nil to use default
|
||||||
|
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
|
||||||
message = nil,
|
message = nil,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
The most common issue with the plugin is missing dependencies.
|
||||||
|
Run `:checkhealth gopher` to verify that the plugin is installed correctly.
|
||||||
|
If any binaries are missing, install them using `:GoInstallDeps`.
|
||||||
|
|
||||||
|
If the issue persists, feel free to [open a new issue](https://github.com/olexsmir/gopher.nvim/issues/new).
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md)
|
PRs are always welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md)
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ tasks:
|
||||||
-u ./scripts/minimal_init.lua \
|
-u ./scripts/minimal_init.lua \
|
||||||
-c "lua MiniTest.run()" \
|
-c "lua MiniTest.run()" \
|
||||||
-c ":qa!"
|
-c ":qa!"
|
||||||
|
nvim:
|
||||||
|
cmd: nvim --clean -u "./scripts/minimal_init.lua" {{ .CLI_ARGS }}
|
||||||
|
|
||||||
docgen:
|
docgen:
|
||||||
desc: generate vimhelp
|
desc: generate vimhelp
|
||||||
|
|
|
||||||
|
|
@ -36,11 +36,11 @@ Parameters ~
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
*gopher.nvim-dependencies*
|
*gopher.nvim-dependencies*
|
||||||
`gopher.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
|
Gopher.nvim implements most of its features using third-party tools. To
|
||||||
or call `require("gopher").install_deps()` if you want to use lua api.
|
install plugin's dependencies, you can run:
|
||||||
By default dependencies will be installed asynchronously,
|
`:GoInstallDeps` or `:GoInstallDepsSync`
|
||||||
to install them synchronously pass `{sync = true}` as an argument.
|
or use `require("gopher").install_deps()` if you prefer lua api.
|
||||||
|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
@ -57,7 +57,8 @@ to install them synchronously pass `{sync = true}` as an argument.
|
||||||
---@type number
|
---@type number
|
||||||
timeout = 2000,
|
timeout = 2000,
|
||||||
|
|
||||||
--- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
|
-- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
|
||||||
|
---@type number
|
||||||
installer_timeout = 999999,
|
installer_timeout = 999999,
|
||||||
|
|
||||||
-- user specified paths to binaries
|
-- user specified paths to binaries
|
||||||
|
|
@ -71,12 +72,15 @@ to install them synchronously pass `{sync = true}` as an argument.
|
||||||
},
|
},
|
||||||
---@class gopher.ConfigGotests
|
---@class gopher.ConfigGotests
|
||||||
gotests = {
|
gotests = {
|
||||||
-- gotests doesn't have template named "default" so this plugin uses "default" to set the default template
|
-- a default template that gotess will use.
|
||||||
|
-- gotets doesn't have template named `default`, we use it to represent absence of the provided template.
|
||||||
template = "default",
|
template = "default",
|
||||||
|
|
||||||
-- path to a directory containing custom test code templates
|
-- path to a directory containing custom test code templates
|
||||||
---@type string|nil
|
---@type string|nil
|
||||||
template_dir = nil,
|
template_dir = nil,
|
||||||
-- switch table tests from using slice to map (with test name for the key)
|
|
||||||
|
-- use named tests(map with test name as key) in table tests(slice of structs by default)
|
||||||
named = false,
|
named = false,
|
||||||
},
|
},
|
||||||
---@class gopher.ConfigGoTag
|
---@class gopher.ConfigGoTag
|
||||||
|
|
@ -86,9 +90,15 @@ to install them synchronously pass `{sync = true}` as an argument.
|
||||||
|
|
||||||
-- default tags to add to struct fields
|
-- default tags to add to struct fields
|
||||||
default_tag = "json",
|
default_tag = "json",
|
||||||
|
|
||||||
|
-- default tag option added struct fields, set to nil to disable
|
||||||
|
-- e.g: `option = "json=omitempty,xml=omitempty`
|
||||||
|
---@type string|nil
|
||||||
|
option = nil,
|
||||||
},
|
},
|
||||||
iferr = {
|
iferr = {
|
||||||
-- choose a custom error message
|
-- choose a custom error message, nil to use default
|
||||||
|
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
|
||||||
---@type string|nil
|
---@type string|nil
|
||||||
message = nil,
|
message = nil,
|
||||||
},
|
},
|
||||||
|
|
@ -96,6 +106,8 @@ to install them synchronously pass `{sync = true}` as an argument.
|
||||||
<
|
<
|
||||||
Class ~
|
Class ~
|
||||||
{gopher.Config}
|
{gopher.Config}
|
||||||
|
Fields ~
|
||||||
|
{setup} `(fun(user_config?: gopher.Config))`
|
||||||
|
|
||||||
|
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
@ -110,16 +122,21 @@ you can set `vim.g.gopher_register_commands` to `false`, before loading the plug
|
||||||
------------------------------------------------------------------------------
|
------------------------------------------------------------------------------
|
||||||
*gopher.nvim-struct-tags*
|
*gopher.nvim-struct-tags*
|
||||||
|
|
||||||
`struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to struct fields.
|
`struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to
|
||||||
|
struct fields.
|
||||||
|
|
||||||
Usage ~
|
Usage ~
|
||||||
|
|
||||||
How to add/remove tags to struct fields:
|
How to add/remove/clear tags to struct fields:
|
||||||
1. Place cursor on the struct
|
1. Place cursor on the struct
|
||||||
2. Run `:GoTagAdd json` to add json tags to struct fields
|
2. Run `:GoTagAdd json` to add json tags to struct fields
|
||||||
3. Run `:GoTagRm json` to remove json tags to struct fields
|
3. Run `:GoTagRm json` to remove json tags to struct fields
|
||||||
|
4. Run `:GoTagClear` to clear all tags from struct fields
|
||||||
|
|
||||||
|
If you want to add/remove tag with options, you can use `json=omitempty`
|
||||||
|
(where json is tag, and omitempty is its option).
|
||||||
|
Example: `:GoTagAdd xml json=omitempty`
|
||||||
|
|
||||||
To clear all tags from struct run: `:GoTagClear`
|
|
||||||
|
|
||||||
NOTE: if you dont specify the tag it will use `json` as default
|
NOTE: if you dont specify the tag it will use `json` as default
|
||||||
|
|
||||||
|
|
@ -147,6 +164,7 @@ Example:
|
||||||
Integration of `impl` tool to generate method stubs for interfaces.
|
Integration of `impl` tool to generate method stubs for interfaces.
|
||||||
|
|
||||||
Usage ~
|
Usage ~
|
||||||
|
|
||||||
1. Automatically implement an interface for a struct:
|
1. Automatically implement an interface for a struct:
|
||||||
- Place your cursor on the struct where you want to implement the interface.
|
- Place your cursor on the struct where you want to implement the interface.
|
||||||
- Run `:GoImpl io.Reader`
|
- Run `:GoImpl io.Reader`
|
||||||
|
|
@ -193,8 +211,9 @@ Usage ~
|
||||||
- Generate unit tests *only* for *exported(public)* functions/methods:
|
- Generate unit tests *only* for *exported(public)* functions/methods:
|
||||||
- run `:GoTestsExp`
|
- run `:GoTestsExp`
|
||||||
|
|
||||||
You can also specify the template to use for generating the tests. See |gopher.nvim-config|
|
You can also specify the template to use for generating the tests.
|
||||||
More details about templates can be found at: https://github.com/cweill/gotests
|
See |gopher.nvim-config|.
|
||||||
|
More details about templates: https://github.com/cweill/gotests
|
||||||
|
|
||||||
If you prefer named tests, you can enable them in |gopher.nvim-config|.
|
If you prefer named tests, you can enable them in |gopher.nvim-config|.
|
||||||
|
|
||||||
|
|
@ -217,7 +236,9 @@ Execute `:GoIfErr` near any `err` variable to insert the check
|
||||||
This module provides a way to generate comments for Go code.
|
This module provides a way to generate comments for Go code.
|
||||||
|
|
||||||
Usage ~
|
Usage ~
|
||||||
Set cursor on line with function/method/struct/etc and run `:GoCmt` to generate a comment.
|
|
||||||
|
Set cursor on line with function/method/struct/etc and
|
||||||
|
run `:GoCmt` to generate a comment.
|
||||||
|
|
||||||
|
|
||||||
vim:tw=78:ts=8:noet:ft=help:norl:
|
vim:tw=78:ts=8:noet:ft=help:norl:
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
local r = require "gopher._utils.runner"
|
|
||||||
local c = require("gopher.config").commands
|
|
||||||
local u = require "gopher._utils"
|
|
||||||
local gocmd = {}
|
|
||||||
|
|
||||||
---@param args string[]
|
|
||||||
---@return string[]
|
|
||||||
local function if_get(args)
|
|
||||||
for i, arg in ipairs(args) do
|
|
||||||
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
|
|
||||||
table.remove(args, i)
|
|
||||||
table.insert(args, i, m)
|
|
||||||
end
|
|
||||||
return args
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param args unknown[]
|
|
||||||
---@return string[]
|
|
||||||
local function if_generate(args)
|
|
||||||
if #args == 1 and args[1] == "%" then
|
|
||||||
args[1] = vim.fn.expand "%"
|
|
||||||
end
|
|
||||||
return args
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param subcmd string
|
|
||||||
---@param args string[]
|
|
||||||
---@return string
|
|
||||||
function gocmd.run(subcmd, args)
|
|
||||||
if #args == 0 and subcmd ~= "generate" then
|
|
||||||
error "please provide any arguments"
|
|
||||||
end
|
|
||||||
|
|
||||||
if subcmd == "get" then
|
|
||||||
args = if_get(args)
|
|
||||||
end
|
|
||||||
|
|
||||||
if subcmd == "generate" then
|
|
||||||
args = if_generate(args)
|
|
||||||
end
|
|
||||||
|
|
||||||
local rs = r.sync { c.go, subcmd, unpack(args) }
|
|
||||||
if rs.code ~= 0 then
|
|
||||||
error("go " .. subcmd .. " failed: " .. rs.stderr)
|
|
||||||
end
|
|
||||||
|
|
||||||
u.notify(c.go .. " " .. subcmd .. " ran successful")
|
|
||||||
return rs.stdout
|
|
||||||
end
|
|
||||||
|
|
||||||
return gocmd
|
|
||||||
|
|
@ -3,7 +3,7 @@ local log = require "gopher._utils.log"
|
||||||
local utils = {}
|
local utils = {}
|
||||||
|
|
||||||
---@param msg string
|
---@param msg string
|
||||||
---@param lvl? number by default `vim.log.levels.INFO`
|
---@param lvl? integer by default `vim.log.levels.INFO`
|
||||||
function utils.notify(msg, lvl)
|
function utils.notify(msg, lvl)
|
||||||
lvl = lvl or vim.log.levels.INFO
|
lvl = lvl or vim.log.levels.INFO
|
||||||
vim.notify(msg, lvl, {
|
vim.notify(msg, lvl, {
|
||||||
|
|
@ -38,4 +38,35 @@ function utils.trimend(s)
|
||||||
return r
|
return r
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Since indentation can be spaces or tabs, that's my hack around it
|
||||||
|
---@param line string
|
||||||
|
---@param indent integer
|
||||||
|
---@return string
|
||||||
|
function utils.indent(line, indent)
|
||||||
|
local char = string.sub(line, 1, 1)
|
||||||
|
if char ~= " " and char ~= "\t" then
|
||||||
|
char = " "
|
||||||
|
end
|
||||||
|
return string.rep(char, indent)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@generic T
|
||||||
|
---@param tbl T[]
|
||||||
|
---@return T[]
|
||||||
|
function utils.list_unique(tbl)
|
||||||
|
if vim.fn.has "nvim-0.12" == 1 then
|
||||||
|
return vim.list.unique(tbl)
|
||||||
|
end
|
||||||
|
|
||||||
|
for i = #tbl, 1, -1 do
|
||||||
|
for j = 1, i - 1 do
|
||||||
|
if tbl[i] == tbl[j] then
|
||||||
|
table.remove(tbl, i)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return tbl
|
||||||
|
end
|
||||||
|
|
||||||
return utils
|
return utils
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
-- for the code i have stolen(or have inspected by idk)
|
-- for the code i have stolen(or have inspected by idk)
|
||||||
local c = require "gopher.config"
|
local c = require "gopher.config"
|
||||||
|
|
||||||
---@class Gopher.Logger
|
---@class gopher.Logger
|
||||||
---@field get_outfile fun():string
|
---@field get_outfile fun():string
|
||||||
---@field trace fun(...)
|
---@field trace fun(...)
|
||||||
---@field fmt_trace fun(...)
|
---@field fmt_trace fun(...)
|
||||||
|
|
@ -44,7 +44,7 @@ local config = {
|
||||||
float_precision = 0.01,
|
float_precision = 0.01,
|
||||||
}
|
}
|
||||||
|
|
||||||
---@type Gopher.Logger
|
---@type gopher.Logger
|
||||||
---@diagnostic disable-next-line: missing-fields
|
---@diagnostic disable-next-line: missing-fields
|
||||||
local log = {}
|
local log = {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,13 @@ local queries = {
|
||||||
right: (expression_list (composite_literal
|
right: (expression_list (composite_literal
|
||||||
type: (struct_type))))]
|
type: (struct_type))))]
|
||||||
]],
|
]],
|
||||||
|
struct_field = [[
|
||||||
|
(field_declaration name: (field_identifier) @_name)
|
||||||
|
]],
|
||||||
func = [[
|
func = [[
|
||||||
[(function_declaration name: (identifier) @_name)
|
[(function_declaration name: (identifier) @_name)
|
||||||
(method_declaration name: (field_identifier) @_name)]
|
(method_declaration name: (field_identifier) @_name)
|
||||||
|
(method_elem name: (field_identifier) @_name)]
|
||||||
]],
|
]],
|
||||||
package = [[
|
package = [[
|
||||||
(package_identifier) @_name
|
(package_identifier) @_name
|
||||||
|
|
@ -23,12 +27,17 @@ local queries = {
|
||||||
name: (type_identifier) @_name
|
name: (type_identifier) @_name
|
||||||
type: (interface_type))
|
type: (interface_type))
|
||||||
]],
|
]],
|
||||||
|
var = [[
|
||||||
|
[(var_declaration (var_spec name: (identifier) @_name))
|
||||||
|
(short_var_declaration
|
||||||
|
left: (expression_list (identifier) @_name @_var))]
|
||||||
|
]],
|
||||||
}
|
}
|
||||||
|
|
||||||
---@param parent_type string[]
|
---@param parent_type string[]
|
||||||
---@param node TSNode
|
---@param node TSNode
|
||||||
---@return TSNode?
|
---@return TSNode?
|
||||||
local function get_parrent_node(parent_type, node)
|
local function get_parent_node(parent_type, node)
|
||||||
---@type TSNode?
|
---@type TSNode?
|
||||||
local current = node
|
local current = node
|
||||||
while current do
|
while current do
|
||||||
|
|
@ -64,10 +73,11 @@ local function get_captures(query, node, bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class gopher.TsResult
|
---@class gopher.TsResult
|
||||||
---@field name string
|
---@field name string Name of the struct, function, etc
|
||||||
---@field start integer
|
---@field start integer Line number where the declaration starts
|
||||||
---@field end_ integer
|
---@field end_ integer Line number where the declaration ends
|
||||||
---@field is_varstruct boolean
|
---@field indent integer Number of spaces/tabs in the current cursor line
|
||||||
|
---@field is_varstruct boolean Is struct declared as `var S struct{}` or `s := struct{}{}`
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
---@param parent_type string[]
|
---@param parent_type string[]
|
||||||
|
|
@ -78,23 +88,22 @@ local function do_stuff(bufnr, parent_type, query)
|
||||||
error "No treesitter parser found for go"
|
error "No treesitter parser found for go"
|
||||||
end
|
end
|
||||||
|
|
||||||
local node = vim.treesitter.get_node {
|
local node = vim.treesitter.get_node { bufnr = bufnr }
|
||||||
bufnr = bufnr,
|
|
||||||
}
|
|
||||||
if not node then
|
if not node then
|
||||||
error "No nodes found under cursor"
|
error "No nodes found under the cursor"
|
||||||
end
|
end
|
||||||
|
|
||||||
local parent_node = get_parrent_node(parent_type, node)
|
local parent_node = get_parent_node(parent_type, node)
|
||||||
if not parent_node then
|
if not parent_node then
|
||||||
error "No parent node found under cursor"
|
error "No parent node found under the cursor"
|
||||||
end
|
end
|
||||||
|
|
||||||
local q = vim.treesitter.query.parse("go", query)
|
local q = vim.treesitter.query.parse("go", query)
|
||||||
local res = get_captures(q, parent_node, bufnr)
|
local res = get_captures(q, parent_node, bufnr)
|
||||||
assert(res.name ~= nil, "No capture name found")
|
assert(res.name ~= nil, "No capture name found")
|
||||||
|
|
||||||
local start_row, _, end_row, _ = parent_node:range()
|
local start_row, start_col, end_row, _ = parent_node:range()
|
||||||
|
res["indent"] = start_col
|
||||||
res["start"] = start_row + 1
|
res["start"] = start_row + 1
|
||||||
res["end_"] = end_row + 1
|
res["end_"] = end_row + 1
|
||||||
|
|
||||||
|
|
@ -104,11 +113,12 @@ end
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
function ts.get_struct_under_cursor(bufnr)
|
function ts.get_struct_under_cursor(bufnr)
|
||||||
--- should be both type_spec and type_declaration
|
--- should be both type_spec and type_declaration
|
||||||
--- because in cases like `type ( T struct{}, U strict{} )`
|
--- because in cases like `type ( T struct{}, U struct{} )`
|
||||||
--- i will be choosing always last struct in the list
|
|
||||||
---
|
---
|
||||||
--- var_declaration is for cases like `var x struct{}`
|
--- var_declaration is for cases like `var x struct{}`
|
||||||
--- short_var_declaration is for cases like `x := struct{}{}`
|
--- short_var_declaration is for cases like `x := struct{}{}`
|
||||||
|
---
|
||||||
|
--- it always chooses last struct type in the list
|
||||||
return do_stuff(bufnr, {
|
return do_stuff(bufnr, {
|
||||||
"type_spec",
|
"type_spec",
|
||||||
"type_declaration",
|
"type_declaration",
|
||||||
|
|
@ -117,10 +127,19 @@ function ts.get_struct_under_cursor(bufnr)
|
||||||
}, queries.struct)
|
}, queries.struct)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
function ts.get_struct_field_under_cursor(bufnr)
|
||||||
|
return do_stuff(bufnr, { "field_declaration" }, queries.struct_field)
|
||||||
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
function ts.get_func_under_cursor(bufnr)
|
function ts.get_func_under_cursor(bufnr)
|
||||||
--- since this handles both and funcs and methods we should check for both parent nodes
|
--- since this handles both and funcs and methods we should check for both parent nodes
|
||||||
return do_stuff(bufnr, { "function_declaration", "method_declaration" }, queries.func)
|
return do_stuff(bufnr, {
|
||||||
|
"method_elem",
|
||||||
|
"function_declaration",
|
||||||
|
"method_declaration",
|
||||||
|
}, queries.func)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
|
|
@ -133,4 +152,9 @@ function ts.get_interface_under_cursor(bufnr)
|
||||||
return do_stuff(bufnr, { "type_declaration" }, queries.interface)
|
return do_stuff(bufnr, { "type_declaration" }, queries.interface)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param bufnr integer
|
||||||
|
function ts.get_variable_under_cursor(bufnr)
|
||||||
|
return do_stuff(bufnr, { "var_declaration", "short_var_declaration" }, queries.var)
|
||||||
|
end
|
||||||
|
|
||||||
return ts
|
return ts
|
||||||
|
|
|
||||||
|
|
@ -3,36 +3,46 @@
|
||||||
---@text
|
---@text
|
||||||
--- This module provides a way to generate comments for Go code.
|
--- This module provides a way to generate comments for Go code.
|
||||||
---
|
---
|
||||||
---@usage Set cursor on line with function/method/struct/etc and run `:GoCmt` to generate a comment.
|
---@usage
|
||||||
|
--- Set cursor on line with function/method/struct/etc and
|
||||||
|
--- run `:GoCmt` to generate a comment.
|
||||||
|
|
||||||
local ts = require "gopher._utils.ts"
|
local ts = require "gopher._utils.ts"
|
||||||
local log = require "gopher._utils.log"
|
local log = require "gopher._utils.log"
|
||||||
|
local u = require "gopher._utils"
|
||||||
local comment = {}
|
local comment = {}
|
||||||
|
|
||||||
---@param name string
|
--- NOTE: The order of functions executed inside this function is IMPORTANT.
|
||||||
---@return string
|
--- This function is extremely fragile; run tests after making any changes.
|
||||||
---@dochide
|
---
|
||||||
local function template(name)
|
|
||||||
return "// " .. name .. " "
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param bufnr integer
|
---@param bufnr integer
|
||||||
|
---@param line string
|
||||||
---@return string
|
---@return string
|
||||||
---@dochide
|
---@dochide
|
||||||
local function generate(bufnr)
|
local function generate(bufnr, line)
|
||||||
|
local sf_ok, sf_res = pcall(ts.get_struct_field_under_cursor, bufnr)
|
||||||
|
if sf_ok then
|
||||||
|
return u.indent(line, sf_res.indent) .. "// " .. sf_res.name .. " "
|
||||||
|
end
|
||||||
|
|
||||||
local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr)
|
local s_ok, s_res = pcall(ts.get_struct_under_cursor, bufnr)
|
||||||
if s_ok then
|
if s_ok then
|
||||||
return template(s_res.name)
|
return u.indent(line, s_res.indent) .. "// " .. s_res.name .. " "
|
||||||
|
end
|
||||||
|
|
||||||
|
local v_ok, v_res = pcall(ts.get_variable_under_cursor, bufnr)
|
||||||
|
if v_ok then
|
||||||
|
return u.indent(line, v_res.indent) .. "// " .. v_res.name .. " "
|
||||||
end
|
end
|
||||||
|
|
||||||
local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr)
|
local f_ok, f_res = pcall(ts.get_func_under_cursor, bufnr)
|
||||||
if f_ok then
|
if f_ok then
|
||||||
return template(f_res.name)
|
return u.indent(line, f_res.indent) .. "// " .. f_res.name .. " "
|
||||||
end
|
end
|
||||||
|
|
||||||
local i_ok, i_res = pcall(ts.get_interface_under_cursor, bufnr)
|
local i_ok, i_res = pcall(ts.get_interface_under_cursor, bufnr)
|
||||||
if i_ok then
|
if i_ok then
|
||||||
return template(i_res.name)
|
return u.indent(line, i_res.indent) .. "// " .. i_res.name .. " "
|
||||||
end
|
end
|
||||||
|
|
||||||
local p_ok, p_res = pcall(ts.get_package_under_cursor, bufnr)
|
local p_ok, p_res = pcall(ts.get_package_under_cursor, bufnr)
|
||||||
|
|
@ -45,12 +55,16 @@ end
|
||||||
|
|
||||||
function comment.comment()
|
function comment.comment()
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
local cmt = generate(bufnr)
|
local lnum = vim.fn.getcurpos()[2]
|
||||||
log.debug("generated comment: " .. cmt)
|
local line = vim.fn.getline(lnum)
|
||||||
|
local cmt = generate(bufnr, line)
|
||||||
|
log.debug("generated comment:", {
|
||||||
|
comment = cmt,
|
||||||
|
line = line,
|
||||||
|
})
|
||||||
|
|
||||||
local pos = vim.fn.getcurpos()[2]
|
vim.fn.append(lnum - 1, cmt)
|
||||||
vim.fn.append(pos - 1, cmt)
|
vim.fn.setpos(".", { bufnr, lnum, #cmt })
|
||||||
vim.fn.setpos(".", { 0, pos, #cmt })
|
|
||||||
vim.cmd "startinsert!"
|
vim.cmd "startinsert!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
---@type gopher.Config
|
||||||
|
---@dochide
|
||||||
|
---@diagnostic disable-next-line: missing-fields .setup() gets injected later
|
||||||
local config = {}
|
local config = {}
|
||||||
|
|
||||||
---@tag gopher.nvim-config.ConfigGoTagTransform
|
---@tag gopher.nvim-config.ConfigGoTagTransform
|
||||||
|
|
@ -16,6 +19,7 @@ local config = {}
|
||||||
---@tag gopher.nvim-config
|
---@tag gopher.nvim-config
|
||||||
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
|
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
|
||||||
---@class gopher.Config
|
---@class gopher.Config
|
||||||
|
---@field setup fun(user_config?: gopher.Config)
|
||||||
local default_config = {
|
local default_config = {
|
||||||
-- log level, you might consider using DEBUG or TRACE for debugging the plugin
|
-- log level, you might consider using DEBUG or TRACE for debugging the plugin
|
||||||
---@type number
|
---@type number
|
||||||
|
|
@ -25,7 +29,8 @@ local default_config = {
|
||||||
---@type number
|
---@type number
|
||||||
timeout = 2000,
|
timeout = 2000,
|
||||||
|
|
||||||
--- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
|
-- timeout for running installer commands(e.g :GoDepsInstall, :GoDepsInstallSync)
|
||||||
|
---@type number
|
||||||
installer_timeout = 999999,
|
installer_timeout = 999999,
|
||||||
|
|
||||||
-- user specified paths to binaries
|
-- user specified paths to binaries
|
||||||
|
|
@ -39,12 +44,15 @@ local default_config = {
|
||||||
},
|
},
|
||||||
---@class gopher.ConfigGotests
|
---@class gopher.ConfigGotests
|
||||||
gotests = {
|
gotests = {
|
||||||
-- gotests doesn't have template named "default" so this plugin uses "default" to set the default template
|
-- a default template that gotess will use.
|
||||||
|
-- gotets doesn't have template named `default`, we use it to represent absence of the provided template.
|
||||||
template = "default",
|
template = "default",
|
||||||
|
|
||||||
-- path to a directory containing custom test code templates
|
-- path to a directory containing custom test code templates
|
||||||
---@type string|nil
|
---@type string|nil
|
||||||
template_dir = nil,
|
template_dir = nil,
|
||||||
-- switch table tests from using slice to map (with test name for the key)
|
|
||||||
|
-- use named tests(map with test name as key) in table tests(slice of structs by default)
|
||||||
named = false,
|
named = false,
|
||||||
},
|
},
|
||||||
---@class gopher.ConfigGoTag
|
---@class gopher.ConfigGoTag
|
||||||
|
|
@ -54,9 +62,15 @@ local default_config = {
|
||||||
|
|
||||||
-- default tags to add to struct fields
|
-- default tags to add to struct fields
|
||||||
default_tag = "json",
|
default_tag = "json",
|
||||||
|
|
||||||
|
-- default tag option added struct fields, set to nil to disable
|
||||||
|
-- e.g: `option = "json=omitempty,xml=omitempty`
|
||||||
|
---@type string|nil
|
||||||
|
option = nil,
|
||||||
},
|
},
|
||||||
iferr = {
|
iferr = {
|
||||||
-- choose a custom error message
|
-- choose a custom error message, nil to use default
|
||||||
|
-- e.g: `message = 'fmt.Errorf("failed to %w", err)'`
|
||||||
---@type string|nil
|
---@type string|nil
|
||||||
message = nil,
|
message = nil,
|
||||||
},
|
},
|
||||||
|
|
@ -92,13 +106,14 @@ function config.setup(user_config)
|
||||||
vim.validate("commands.iferr", _config.commands.iferr, "string")
|
vim.validate("commands.iferr", _config.commands.iferr, "string")
|
||||||
vim.validate("gotests", _config.gotests, "table")
|
vim.validate("gotests", _config.gotests, "table")
|
||||||
vim.validate("gotests.template", _config.gotests.template, "string")
|
vim.validate("gotests.template", _config.gotests.template, "string")
|
||||||
vim.validate("gotests.template_dir", _config.gotests.template_dir, "string", true)
|
vim.validate("gotests.template_dir", _config.gotests.template_dir, { "string", "nil" })
|
||||||
vim.validate("gotests.named", _config.gotests.named, "boolean")
|
vim.validate("gotests.named", _config.gotests.named, "boolean")
|
||||||
vim.validate("gotag", _config.gotag, "table")
|
vim.validate("gotag", _config.gotag, "table")
|
||||||
vim.validate("gotag.transform", _config.gotag.transform, "string")
|
vim.validate("gotag.transform", _config.gotag.transform, "string")
|
||||||
vim.validate("gotag.default_tag", _config.gotag.default_tag, "string")
|
vim.validate("gotag.default_tag", _config.gotag.default_tag, "string")
|
||||||
|
vim.validate("gotag.option", _config.gotag.option, { "string", "nil" })
|
||||||
vim.validate("iferr", _config.iferr, "table")
|
vim.validate("iferr", _config.iferr, "table")
|
||||||
vim.validate("iferr.message", _config.iferr.message, "string", true)
|
vim.validate("iferr.message", _config.iferr.message, { "string", "nil" })
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(config, {
|
setmetatable(config, {
|
||||||
|
|
@ -108,5 +123,4 @@ setmetatable(config, {
|
||||||
})
|
})
|
||||||
|
|
||||||
---@dochide
|
---@dochide
|
||||||
---@return gopher.Config
|
return config
|
||||||
return config --[[ @as gopher.Config ]]
|
|
||||||
|
|
|
||||||
56
lua/gopher/go.lua
Normal file
56
lua/gopher/go.lua
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
local c = require "gopher.config"
|
||||||
|
local u = require "gopher._utils"
|
||||||
|
local r = require "gopher._utils.runner"
|
||||||
|
local go = {}
|
||||||
|
|
||||||
|
local function run(subcmd, args)
|
||||||
|
local rs = r.sync { c.commands.go, subcmd, unpack(args) }
|
||||||
|
if rs.code ~= 0 then
|
||||||
|
error("go " .. subcmd .. " failed: " .. rs.stderr)
|
||||||
|
end
|
||||||
|
|
||||||
|
u.notify(c.commands.go .. " " .. subcmd .. " ran successful")
|
||||||
|
return rs.stdout
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param args string[]
|
||||||
|
function go.get(args)
|
||||||
|
for i, arg in ipairs(args) do
|
||||||
|
local m = string.match(arg, "^https://(.*)$") or string.match(arg, "^http://(.*)$") or arg
|
||||||
|
table.remove(args, i)
|
||||||
|
table.insert(args, i, m)
|
||||||
|
end
|
||||||
|
|
||||||
|
run("get", args)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param args string[]
|
||||||
|
function go.mod(args)
|
||||||
|
run("mod", args)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param args string[]
|
||||||
|
function go.work(args)
|
||||||
|
-- TODO: use `gopls.tidy`
|
||||||
|
|
||||||
|
run("work", args)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Executes `go generate`
|
||||||
|
---If only argument is `%` it's going to be equivalent to `go generate <path to current file>`
|
||||||
|
---@param args string[]
|
||||||
|
function go.generate(args)
|
||||||
|
-- TODO: use `gopls.generate`
|
||||||
|
|
||||||
|
if #args == 0 then
|
||||||
|
error "please provide arguments"
|
||||||
|
end
|
||||||
|
|
||||||
|
if #args == 1 and args[1] == "%" then
|
||||||
|
args[1] = vim.fn.expand "%"
|
||||||
|
end
|
||||||
|
|
||||||
|
run("generate", args)
|
||||||
|
end
|
||||||
|
|
||||||
|
return go
|
||||||
|
|
@ -12,8 +12,9 @@
|
||||||
--- - Generate unit tests *only* for *exported(public)* functions/methods:
|
--- - Generate unit tests *only* for *exported(public)* functions/methods:
|
||||||
--- - run `:GoTestsExp`
|
--- - run `:GoTestsExp`
|
||||||
---
|
---
|
||||||
--- You can also specify the template to use for generating the tests. See |gopher.nvim-config|
|
--- You can also specify the template to use for generating the tests.
|
||||||
--- More details about templates can be found at: https://github.com/cweill/gotests
|
--- See |gopher.nvim-config|.
|
||||||
|
--- More details about templates: https://github.com/cweill/gotests
|
||||||
---
|
---
|
||||||
--- If you prefer named tests, you can enable them in |gopher.nvim-config|.
|
--- If you prefer named tests, you can enable them in |gopher.nvim-config|.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,62 +1,58 @@
|
||||||
|
local c = require("gopher.config").commands
|
||||||
local health = {}
|
local health = {}
|
||||||
local cmd = require("gopher.config").commands
|
|
||||||
|
|
||||||
local deps = {
|
local deps = {
|
||||||
|
vim_version = "nvim-0.10",
|
||||||
bin = {
|
bin = {
|
||||||
{
|
{
|
||||||
bin = cmd.go,
|
bin = c.go,
|
||||||
msg = "required for `:GoGet`, `:GoMod`, `:GoGenerate`, `:GoWork`, `:GoInstallDeps`, `:GoInstallDepsSync`",
|
msg = "required for `:GoGet`, `:GoMod`, `:GoGenerate`, `:GoWork`, `:GoInstallDeps`, `:GoInstallDepsSync`",
|
||||||
optional = false,
|
|
||||||
},
|
|
||||||
{ bin = cmd.gomodifytags, msg = "required for `:GoTagAdd`, `:GoTagRm`", optional = true },
|
|
||||||
{ bin = cmd.impl, msg = "required for `:GoImpl`", optional = true },
|
|
||||||
{ bin = cmd.iferr, msg = "required for `:GoIfErr`", optional = true },
|
|
||||||
{
|
|
||||||
bin = cmd.gotests,
|
|
||||||
msg = "required for `:GoTestAdd`, `:GoTestsAll`, `:GoTestsExp`",
|
|
||||||
optional = true,
|
|
||||||
},
|
},
|
||||||
|
{ bin = c.gomodifytags, msg = "required for `:GoTagAdd`, `:GoTagRm`" },
|
||||||
|
{ bin = c.impl, msg = "required for `:GoImpl`" },
|
||||||
|
{ bin = c.iferr, msg = "required for `:GoIfErr`" },
|
||||||
|
{ bin = c.gotests, msg = "required for `:GoTestAdd`, `:GoTestsAll`, `:GoTestsExp`" },
|
||||||
},
|
},
|
||||||
treesitter = {
|
treesitter = {
|
||||||
{ parser = "go", msg = "required for most of the parts of `gopher.nvim`" },
|
{ parser = "go", msg = "required for most of the parts of `gopher.nvim`" },
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
---@param bin string
|
---@param bin {bin:string, msg:string, optional:boolean}
|
||||||
---@return boolean
|
local function check_binary(bin)
|
||||||
local function is_binary_found(bin)
|
if vim.fn.executable(bin.bin) == 1 then
|
||||||
return vim.fn.executable(bin) == 1
|
vim.health.ok(bin.bin .. " is found oh PATH: `" .. vim.fn.exepath(bin.bin) .. "`")
|
||||||
|
else
|
||||||
|
vim.health.error(bin.bin .. " not found on PATH, " .. bin.msg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param ft string
|
---@param ts {parser:string, msg:string}
|
||||||
---@return boolean
|
local function check_treesitter(ts)
|
||||||
local function is_treesitter_parser_available(ft)
|
local ok, parser = pcall(vim.treesitter.get_parser, 0, ts.parser)
|
||||||
local ok, parser = pcall(vim.treesitter.get_parser, 0, ft)
|
if ok and parser ~= nil then
|
||||||
return ok and parser ~= nil
|
vim.health.ok("`" .. ts.parser .. "` parser is installed")
|
||||||
|
else
|
||||||
|
vim.health.error("`" .. ts.parser .. "` parser not found")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function health.check()
|
function health.check()
|
||||||
vim.health.start "required binaries"
|
vim.health.start "Neovim version"
|
||||||
vim.health.info "all those binaries can be installed by `:GoInstallDeps`"
|
if vim.fn.has(deps.vim_version) == 1 then
|
||||||
for _, bin in ipairs(deps.bin) do
|
vim.health.ok "Neovim version is compatible"
|
||||||
if is_binary_found(bin.bin) then
|
|
||||||
vim.health.ok(bin.bin .. " installed")
|
|
||||||
else
|
else
|
||||||
if bin.optional then
|
vim.health.error(deps.vim_version .. " or newer is required")
|
||||||
vim.health.warn(bin.bin .. " not found, " .. bin.msg)
|
|
||||||
else
|
|
||||||
vim.health.error(bin.bin .. " not found, " .. bin.msg)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.health.start "required treesitter parsers"
|
vim.health.start "Required binaries (those can be installed with `:GoInstallDeps`)"
|
||||||
for _, parser in ipairs(deps.treesitter) do
|
for _, bin in ipairs(deps.bin) do
|
||||||
if is_treesitter_parser_available(parser.parser) then
|
check_binary(bin)
|
||||||
vim.health.ok(parser.parser .. " parser installed")
|
|
||||||
else
|
|
||||||
vim.health.error(parser.parser .. " parser not found, " .. parser.msg)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
vim.health.start "Treesitter"
|
||||||
|
for _, parser in ipairs(deps.treesitter) do
|
||||||
|
check_treesitter(parser)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ function iferr.iferr()
|
||||||
|
|
||||||
vim.fn.append(pos, u.remove_empty_lines(vim.split(rs.stdout, "\n")))
|
vim.fn.append(pos, u.remove_empty_lines(vim.split(rs.stdout, "\n")))
|
||||||
vim.cmd [[silent normal! j=2j]]
|
vim.cmd [[silent normal! j=2j]]
|
||||||
vim.fn.setpos(".", pos)
|
vim.fn.setpos(".", pos --[[@as integer[] ]])
|
||||||
end
|
end
|
||||||
|
|
||||||
return iferr
|
return iferr
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,8 @@
|
||||||
---@text
|
---@text
|
||||||
--- Integration of `impl` tool to generate method stubs for interfaces.
|
--- Integration of `impl` tool to generate method stubs for interfaces.
|
||||||
---
|
---
|
||||||
---@usage 1. Automatically implement an interface for a struct:
|
---@usage
|
||||||
|
--- 1. Automatically implement an interface for a struct:
|
||||||
--- - Place your cursor on the struct where you want to implement the interface.
|
--- - Place your cursor on the struct where you want to implement the interface.
|
||||||
--- - Run `:GoImpl io.Reader`
|
--- - Run `:GoImpl io.Reader`
|
||||||
--- - This will automatically determine the receiver and implement the `io.Reader` interface.
|
--- - This will automatically determine the receiver and implement the `io.Reader` interface.
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
local log = require "gopher._utils.log"
|
local log = require "gopher._utils.log"
|
||||||
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.gocmd").run
|
local go = require "gopher.go"
|
||||||
local gopher = {}
|
local gopher = {}
|
||||||
|
|
||||||
---@toc_entry Setup
|
---@toc_entry Setup
|
||||||
|
|
@ -35,11 +35,11 @@ end
|
||||||
|
|
||||||
---@toc_entry Install dependencies
|
---@toc_entry Install dependencies
|
||||||
---@tag gopher.nvim-dependencies
|
---@tag gopher.nvim-dependencies
|
||||||
---@text Gopher.nvim implements most of its features using third-party tools.
|
---@text
|
||||||
--- To install these tools, you can run `:GoInstallDeps` command
|
--- Gopher.nvim implements most of its features using third-party tools. To
|
||||||
--- or call `require("gopher").install_deps()` if you want to use lua api.
|
--- install plugin's dependencies, you can run:
|
||||||
--- By default dependencies will be installed asynchronously,
|
--- `:GoInstallDeps` or `:GoInstallDepsSync`
|
||||||
--- to install them synchronously pass `{sync = true}` as an argument.
|
--- or use `require("gopher").install_deps()` if you prefer 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
|
||||||
|
|
@ -58,20 +58,9 @@ gopher.test = {
|
||||||
all = tests.all_tests,
|
all = tests.all_tests,
|
||||||
}
|
}
|
||||||
|
|
||||||
gopher.get = function(...)
|
gopher.get = go.get
|
||||||
gocmd("get", ...)
|
gopher.mod = go.mod
|
||||||
end
|
gopher.work = go.work
|
||||||
|
gopher.generate = go.generate
|
||||||
gopher.mod = function(...)
|
|
||||||
gocmd("mod", ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
gopher.generate = function(...)
|
|
||||||
gocmd("generate", ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
gopher.work = function(...)
|
|
||||||
gocmd("work", ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
return gopher
|
return gopher
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,20 @@
|
||||||
---@toc_entry Modify struct tags
|
---@toc_entry Modify struct tags
|
||||||
---@tag gopher.nvim-struct-tags
|
---@tag gopher.nvim-struct-tags
|
||||||
---@text
|
---@text
|
||||||
--- `struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to struct fields.
|
--- `struct_tags` is utilizing the `gomodifytags` tool to add or remove tags to
|
||||||
|
--- struct fields.
|
||||||
---
|
---
|
||||||
---@usage
|
---@usage
|
||||||
--- How to add/remove tags to struct fields:
|
--- How to add/remove/clear tags to struct fields:
|
||||||
--- 1. Place cursor on the struct
|
--- 1. Place cursor on the struct
|
||||||
--- 2. Run `:GoTagAdd json` to add json tags to struct fields
|
--- 2. Run `:GoTagAdd json` to add json tags to struct fields
|
||||||
--- 3. Run `:GoTagRm json` to remove json tags to struct fields
|
--- 3. Run `:GoTagRm json` to remove json tags to struct fields
|
||||||
|
--- 4. Run `:GoTagClear` to clear all tags from struct fields
|
||||||
|
---
|
||||||
|
--- If you want to add/remove tag with options, you can use `json=omitempty`
|
||||||
|
--- (where json is tag, and omitempty is its option).
|
||||||
|
--- Example: `:GoTagAdd xml json=omitempty`
|
||||||
---
|
---
|
||||||
--- To clear all tags from struct run: `:GoTagClear`
|
|
||||||
---
|
---
|
||||||
--- NOTE: if you dont specify the tag it will use `json` as default
|
--- NOTE: if you dont specify the tag it will use `json` as default
|
||||||
---
|
---
|
||||||
|
|
@ -39,7 +44,7 @@ local struct_tags = {}
|
||||||
|
|
||||||
---@dochide
|
---@dochide
|
||||||
---@class gopher.StructTagInput
|
---@class gopher.StructTagInput
|
||||||
---@field tags string[] User provided tags
|
---@field input string[] User provided tags
|
||||||
---@field range? gopher.StructTagRange (optional)
|
---@field range? gopher.StructTagRange (optional)
|
||||||
|
|
||||||
---@dochide
|
---@dochide
|
||||||
|
|
@ -102,18 +107,52 @@ local function handle_tags(fpath, bufnr, range, user_args)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param args string[]
|
|
||||||
---@return string
|
|
||||||
---@dochide
|
---@dochide
|
||||||
local function handler_user_tags(args)
|
---@param option string
|
||||||
if #args == 0 then
|
local function option_to_tag(option)
|
||||||
return c.gotag.default_tag
|
return option:match "^(.-)="
|
||||||
end
|
|
||||||
return table.concat(args, ",")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Adds tags to a struct under the cursor
|
---@dochide
|
||||||
-- See |gopher.nvim-struct-tags|
|
---@param args string[]
|
||||||
|
local function unwrap_if_needed(args)
|
||||||
|
local out = {}
|
||||||
|
for _, v in pairs(args) do
|
||||||
|
for _, p in pairs(vim.split(v, ",")) do
|
||||||
|
table.insert(out, p)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
---@dochide
|
||||||
|
---@class gopher.StructTagsArgs
|
||||||
|
---@field tags string
|
||||||
|
---@field options string
|
||||||
|
|
||||||
|
---@dochide
|
||||||
|
---@param args string[]
|
||||||
|
---@return gopher.StructTagsArgs
|
||||||
|
function struct_tags.parse_args(args)
|
||||||
|
args = unwrap_if_needed(args)
|
||||||
|
|
||||||
|
local tags, options = {}, {}
|
||||||
|
for _, v in pairs(args) do
|
||||||
|
if string.find(v, "=") then
|
||||||
|
table.insert(options, v)
|
||||||
|
table.insert(tags, option_to_tag(v))
|
||||||
|
else
|
||||||
|
table.insert(tags, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
tags = table.concat(u.list_unique(tags), ","),
|
||||||
|
options = table.concat(u.list_unique(options), ","),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Adds tags to a struct under the cursor. See `:h gopher.nvim-struct-tags`.
|
||||||
---@param opts gopher.StructTagInput
|
---@param opts gopher.StructTagInput
|
||||||
---@dochide
|
---@dochide
|
||||||
function struct_tags.add(opts)
|
function struct_tags.add(opts)
|
||||||
|
|
@ -122,12 +161,16 @@ function struct_tags.add(opts)
|
||||||
local fpath = vim.fn.expand "%"
|
local fpath = vim.fn.expand "%"
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local user_tags = handler_user_tags(opts.tags)
|
local user_args = struct_tags.parse_args(opts.input)
|
||||||
handle_tags(fpath, bufnr, opts.range, { "-add-tags", user_tags })
|
handle_tags(fpath, bufnr, opts.range, {
|
||||||
|
"-add-tags",
|
||||||
|
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||||
|
(user_args.options ~= "" or c.gotag.option) and "-add-options" or nil,
|
||||||
|
(user_args.options ~= "") and user_args.options or c.gotag.option,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Removes tags from a struct under the cursor
|
-- Removes tags from a struct under the cursor. See `:h gopher.nvim-struct-tags`.
|
||||||
-- See `:h gopher.nvim-struct-tags`
|
|
||||||
---@dochide
|
---@dochide
|
||||||
---@param opts gopher.StructTagInput
|
---@param opts gopher.StructTagInput
|
||||||
function struct_tags.remove(opts)
|
function struct_tags.remove(opts)
|
||||||
|
|
@ -136,12 +179,17 @@ function struct_tags.remove(opts)
|
||||||
local fpath = vim.fn.expand "%"
|
local fpath = vim.fn.expand "%"
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
local bufnr = vim.api.nvim_get_current_buf()
|
||||||
|
|
||||||
local user_tags = handler_user_tags(opts.tags)
|
local user_args = struct_tags.parse_args(opts.input)
|
||||||
handle_tags(fpath, bufnr, opts.range, { "-remove-tags", user_tags })
|
handle_tags(fpath, bufnr, opts.range, {
|
||||||
|
"-remove-tags",
|
||||||
|
(user_args.tags ~= "") and user_args.tags or c.gotag.default_tag,
|
||||||
|
(user_args.options ~= "" or c.gotag.option ~= nil) and "-remove-options" or nil,
|
||||||
|
(user_args.options ~= "") and user_args.options or c.gotag.option,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Removes all tags from a struct under the cursor
|
-- Removes all tags from a struct under the cursor.
|
||||||
-- See `:h gopher.nvim-struct-tags`
|
-- See `:h gopher.nvim-struct-tags`.
|
||||||
---@dochide
|
---@dochide
|
||||||
function struct_tags.clear()
|
function struct_tags.clear()
|
||||||
local fpath = vim.fn.expand "%"
|
local fpath = vim.fn.expand "%"
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ end)
|
||||||
-- :GoTag
|
-- :GoTag
|
||||||
cmd("GoTagAdd", function(opts)
|
cmd("GoTagAdd", function(opts)
|
||||||
require("gopher").tags.add {
|
require("gopher").tags.add {
|
||||||
tags = opts.fargs,
|
input = opts.fargs,
|
||||||
range = (opts.count ~= -1) and {
|
range = (opts.count ~= -1) and {
|
||||||
start = opts.line1,
|
start = opts.line1,
|
||||||
end_ = opts.line2,
|
end_ = opts.line2,
|
||||||
|
|
@ -58,7 +58,7 @@ end, "*", true)
|
||||||
|
|
||||||
cmd("GoTagRm", function(opts)
|
cmd("GoTagRm", function(opts)
|
||||||
require("gopher").tags.rm {
|
require("gopher").tags.rm {
|
||||||
tags = opts.fargs,
|
input = opts.fargs,
|
||||||
range = (opts.count ~= -1) and {
|
range = (opts.count ~= -1) and {
|
||||||
start = opts.line1,
|
start = opts.line1,
|
||||||
end_ = opts.line2,
|
end_ = opts.line2,
|
||||||
|
|
@ -98,5 +98,5 @@ cmd("GoWork", function(opts)
|
||||||
end, "*")
|
end, "*")
|
||||||
|
|
||||||
cmd("GoGenerate", function(opts)
|
cmd("GoGenerate", function(opts)
|
||||||
require("gopher").generate(opts.fargs or "")
|
require("gopher").generate(opts.fargs or { "" })
|
||||||
end, "?")
|
end, "?")
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ local function install_plug(plugin)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
install_plug "echasnovski/mini.doc" -- used for docs generation
|
||||||
install_plug "folke/tokyonight.nvim" -- theme for generating demos
|
install_plug "folke/tokyonight.nvim" -- theme for generating demos
|
||||||
|
|
|
||||||
6
spec/fixtures/comment/interface_many_method_input.go
vendored
Normal file
6
spec/fixtures/comment/interface_many_method_input.go
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Testinger interface {
|
||||||
|
Get(id string) int
|
||||||
|
Set(id string, val int)
|
||||||
|
}
|
||||||
7
spec/fixtures/comment/interface_many_method_output.go
vendored
Normal file
7
spec/fixtures/comment/interface_many_method_output.go
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Testinger interface {
|
||||||
|
Get(id string) int
|
||||||
|
// Set
|
||||||
|
Set(id string, val int)
|
||||||
|
}
|
||||||
5
spec/fixtures/comment/interface_method_input.go
vendored
Normal file
5
spec/fixtures/comment/interface_method_input.go
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Testinger interface {
|
||||||
|
Method(input string) error
|
||||||
|
}
|
||||||
6
spec/fixtures/comment/interface_method_output.go
vendored
Normal file
6
spec/fixtures/comment/interface_method_output.go
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Testinger interface {
|
||||||
|
// Method
|
||||||
|
Method(input string) error
|
||||||
|
}
|
||||||
18
spec/fixtures/comment/many_structs_fields_input.go
vendored
Normal file
18
spec/fixtures/comment/many_structs_fields_input.go
vendored
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type (
|
||||||
|
TestOne struct {
|
||||||
|
Asdf string
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
|
TestTwo struct {
|
||||||
|
Fesa int
|
||||||
|
A bool
|
||||||
|
}
|
||||||
|
|
||||||
|
TestThree struct {
|
||||||
|
Asufj int
|
||||||
|
Fs string
|
||||||
|
}
|
||||||
|
)
|
||||||
19
spec/fixtures/comment/many_structs_fields_output.go
vendored
Normal file
19
spec/fixtures/comment/many_structs_fields_output.go
vendored
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type (
|
||||||
|
TestOne struct {
|
||||||
|
Asdf string
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
|
TestTwo struct {
|
||||||
|
// Fesa
|
||||||
|
Fesa int
|
||||||
|
A bool
|
||||||
|
}
|
||||||
|
|
||||||
|
TestThree struct {
|
||||||
|
Asufj int
|
||||||
|
Fs string
|
||||||
|
}
|
||||||
|
)
|
||||||
7
spec/fixtures/comment/struct_fields_input.go
vendored
Normal file
7
spec/fixtures/comment/struct_fields_input.go
vendored
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type CommentStruct struct {
|
||||||
|
Name string
|
||||||
|
Address string
|
||||||
|
Aliases []string
|
||||||
|
}
|
||||||
8
spec/fixtures/comment/struct_fields_output.go
vendored
Normal file
8
spec/fixtures/comment/struct_fields_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type CommentStruct struct {
|
||||||
|
Name string
|
||||||
|
// Address
|
||||||
|
Address string
|
||||||
|
Aliases []string
|
||||||
|
}
|
||||||
5
spec/fixtures/comment/svar_input.go
vendored
Normal file
5
spec/fixtures/comment/svar_input.go
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func varTest() {
|
||||||
|
s := "something"
|
||||||
|
}
|
||||||
6
spec/fixtures/comment/svar_output.go
vendored
Normal file
6
spec/fixtures/comment/svar_output.go
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func varTest() {
|
||||||
|
// s
|
||||||
|
s := "something"
|
||||||
|
}
|
||||||
5
spec/fixtures/comment/var_input.go
vendored
Normal file
5
spec/fixtures/comment/var_input.go
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func test() {
|
||||||
|
var imAVar string
|
||||||
|
}
|
||||||
6
spec/fixtures/comment/var_output.go
vendored
Normal file
6
spec/fixtures/comment/var_output.go
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func test() {
|
||||||
|
// imAVar
|
||||||
|
var imAVar string
|
||||||
|
}
|
||||||
8
spec/fixtures/comment/var_struct_fields_input.go
vendored
Normal file
8
spec/fixtures/comment/var_struct_fields_input.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s struct {
|
||||||
|
API string
|
||||||
|
Key string
|
||||||
|
}
|
||||||
|
}
|
||||||
9
spec/fixtures/comment/var_struct_fields_output.go
vendored
Normal file
9
spec/fixtures/comment/var_struct_fields_output.go
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s struct {
|
||||||
|
API string
|
||||||
|
// Key
|
||||||
|
Key string
|
||||||
|
}
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/overwrite_default_option_input.go
vendored
Normal file
8
spec/fixtures/tags/overwrite_default_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int
|
||||||
|
Another struct {
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/overwrite_default_option_output.go
vendored
Normal file
8
spec/fixtures/tags/overwrite_default_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `xml:"id,otheroption"`
|
||||||
|
Another struct {
|
||||||
|
Second string `xml:"second,otheroption"`
|
||||||
|
} `xml:"another,otheroption"`
|
||||||
|
}
|
||||||
11
spec/fixtures/tags/remove_with_option_input.go
vendored
Normal file
11
spec/fixtures/tags/remove_with_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `json:"id,omitempty" xml:"id,someoption"`
|
||||||
|
Name string `json:"name,omitempty" xml:"name,someoption"`
|
||||||
|
Num int64 `json:"num,omitempty" xml:"num,someoption"`
|
||||||
|
Another struct {
|
||||||
|
First int `json:"first,omitempty" xml:"first,someoption"`
|
||||||
|
Second string `json:"second,omitempty" xml:"second,someoption"`
|
||||||
|
} `json:"another,omitempty" xml:"another,someoption"`
|
||||||
|
}
|
||||||
11
spec/fixtures/tags/remove_with_option_output.go
vendored
Normal file
11
spec/fixtures/tags/remove_with_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `xml:"id,someoption"`
|
||||||
|
Name string `xml:"name,someoption"`
|
||||||
|
Num int64 `xml:"num,someoption"`
|
||||||
|
Another struct {
|
||||||
|
First int `xml:"first,someoption"`
|
||||||
|
Second string `xml:"second,someoption"`
|
||||||
|
} `xml:"another,someoption"`
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/with_default_option_input.go
vendored
Normal file
8
spec/fixtures/tags/with_default_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int
|
||||||
|
Another struct {
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
}
|
||||||
8
spec/fixtures/tags/with_default_option_output.go
vendored
Normal file
8
spec/fixtures/tags/with_default_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `xml:"id,theoption"`
|
||||||
|
Another struct {
|
||||||
|
Second string `xml:"second,theoption"`
|
||||||
|
} `xml:"another,theoption"`
|
||||||
|
}
|
||||||
11
spec/fixtures/tags/with_option_input.go
vendored
Normal file
11
spec/fixtures/tags/with_option_input.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int
|
||||||
|
Name string
|
||||||
|
Num int64
|
||||||
|
Another struct {
|
||||||
|
First int
|
||||||
|
Second string
|
||||||
|
}
|
||||||
|
}
|
||||||
11
spec/fixtures/tags/with_option_output.go
vendored
Normal file
11
spec/fixtures/tags/with_option_output.go
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Test struct {
|
||||||
|
ID int `json:"id,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
|
Num int64 `json:"num,omitempty"`
|
||||||
|
Another struct {
|
||||||
|
First int `json:"first,omitempty"`
|
||||||
|
Second string `json:"second,omitempty"`
|
||||||
|
} `json:"another,omitempty"`
|
||||||
|
}
|
||||||
|
|
@ -18,6 +18,18 @@ comment["should add comment to struct"] = function()
|
||||||
do_the_test("struct", { 4, 1 })
|
do_the_test("struct", { 4, 1 })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on struct field"] = function()
|
||||||
|
do_the_test("struct_fields", { 5, 8 })
|
||||||
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on var struct field"] = function()
|
||||||
|
do_the_test("var_struct_fields", { 6, 4 })
|
||||||
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on one field of many structs"] = function()
|
||||||
|
do_the_test("many_structs_fields", { 10, 4 })
|
||||||
|
end
|
||||||
|
|
||||||
comment["should add comment to function"] = function()
|
comment["should add comment to function"] = function()
|
||||||
do_the_test("func", { 3, 1 })
|
do_the_test("func", { 3, 1 })
|
||||||
end
|
end
|
||||||
|
|
@ -30,6 +42,22 @@ comment["should add comment to interface"] = function()
|
||||||
do_the_test("interface", { 3, 6 })
|
do_the_test("interface", { 3, 6 })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
comment["should add comment on interface method"] = function()
|
||||||
|
do_the_test("interface_method", { 4, 2 })
|
||||||
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on interface with many method"] = function()
|
||||||
|
do_the_test("interface_many_method", { 5, 2 })
|
||||||
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on a var"] = function()
|
||||||
|
do_the_test("var", { 4, 2 })
|
||||||
|
end
|
||||||
|
|
||||||
|
comment["should add a comment on a short declared var"] = function()
|
||||||
|
do_the_test("svar", { 4, 8 })
|
||||||
|
end
|
||||||
|
|
||||||
comment["otherwise should add // above cursor"] = function()
|
comment["otherwise should add // above cursor"] = function()
|
||||||
do_the_test("empty", { 1, 1 })
|
do_the_test("empty", { 1, 1 })
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ iferr["should add if err with custom message"] = function()
|
||||||
child.lua [[
|
child.lua [[
|
||||||
require("gopher").setup {
|
require("gopher").setup {
|
||||||
iferr = { message = 'fmt.Errorf("failed to %w", err)' }
|
iferr = { message = 'fmt.Errorf("failed to %w", err)' }
|
||||||
} ]]
|
}
|
||||||
|
]]
|
||||||
|
|
||||||
local rs = t.setup_test("iferr/message", child, { 6, 2 })
|
local rs = t.setup_test("iferr/message", child, { 6, 2 })
|
||||||
child.cmd "GoIfErr"
|
child.cmd "GoIfErr"
|
||||||
|
|
|
||||||
|
|
@ -96,4 +96,52 @@ struct_tags["should remove tag with range"] = function()
|
||||||
t.cleanup(rs)
|
t.cleanup(rs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
struct_tags["should add tags with option"] = function()
|
||||||
|
local rs = t.setup_test("tags/with_option", child, { 3, 6 })
|
||||||
|
child.cmd "GoTagAdd json=omitempty"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
|
struct_tags["should add tags with default option"] = function()
|
||||||
|
child.lua [[
|
||||||
|
require("gopher").setup {
|
||||||
|
gotag = { option = "xml=theoption" },
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
|
||||||
|
local rs = t.setup_test("tags/with_default_option", child, { 3, 6 })
|
||||||
|
child.cmd "GoTagAdd xml"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
|
struct_tags["should add tags and overwrite default option"] = function()
|
||||||
|
child.lua [[
|
||||||
|
require("gopher").setup {
|
||||||
|
gotag = { option = "xml=theoption" },
|
||||||
|
}
|
||||||
|
]]
|
||||||
|
|
||||||
|
local rs = t.setup_test("tags/overwrite_default_option", child, { 3, 6 })
|
||||||
|
child.cmd "GoTagAdd xml=otheroption"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
|
struct_tags["should remove tag with specified option"] = function()
|
||||||
|
local rs = t.setup_test("tags/remove_with_option", child, { 3, 6 })
|
||||||
|
child.cmd "GoTagRm json=omitempty"
|
||||||
|
child.cmd "write"
|
||||||
|
|
||||||
|
t.eq(t.readfile(rs.tmp), rs.fixtures.output)
|
||||||
|
t.cleanup(rs)
|
||||||
|
end
|
||||||
|
|
||||||
return T
|
return T
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,14 @@ config["can be called without any arguments passed"] = function()
|
||||||
end
|
end
|
||||||
|
|
||||||
config["can be called with empty table"] = function()
|
config["can be called with empty table"] = function()
|
||||||
|
---@diagnostic disable-next-line: missing-fields
|
||||||
require("gopher").setup {}
|
require("gopher").setup {}
|
||||||
end
|
end
|
||||||
|
|
||||||
config["should change option"] = function()
|
config["should change option"] = function()
|
||||||
local log_level = 1234567890
|
local log_level = 1234567890
|
||||||
|
|
||||||
|
---@diagnostic disable-next-line: missing-fields
|
||||||
require("gopher").setup {
|
require("gopher").setup {
|
||||||
log_level = log_level,
|
log_level = log_level,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
68
spec/unit/struct_tag_test.lua
Normal file
68
spec/unit/struct_tag_test.lua
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
local t = require "spec.testutils"
|
||||||
|
local _, T, st = t.setup "struct_tags"
|
||||||
|
|
||||||
|
st["should parse tags"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args { "json", "yaml", "etc" }
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,yaml,etc")
|
||||||
|
t.eq(out.options, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
st["should parse tags separated by commas"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args { "json,yaml,etc" }
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,yaml,etc")
|
||||||
|
t.eq(out.options, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
st["should parse tags separated by command and spaces"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args {
|
||||||
|
"json,yaml",
|
||||||
|
"json=omitempty",
|
||||||
|
"xml=something",
|
||||||
|
}
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,yaml,xml")
|
||||||
|
t.eq(out.options, "json=omitempty,xml=something")
|
||||||
|
end
|
||||||
|
|
||||||
|
st["should parse tag with an option"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args {
|
||||||
|
"json=omitempty",
|
||||||
|
"xml",
|
||||||
|
"xml=theoption",
|
||||||
|
}
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,xml")
|
||||||
|
t.eq(out.options, "json=omitempty,xml=theoption")
|
||||||
|
end
|
||||||
|
|
||||||
|
st["should parse tags with an option"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args { "json=omitempty", "yaml" }
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,yaml")
|
||||||
|
t.eq(out.options, "json=omitempty")
|
||||||
|
end
|
||||||
|
|
||||||
|
st["should parse tags with an option separated with comma"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args { "json=omitempty,yaml" }
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,yaml")
|
||||||
|
t.eq(out.options, "json=omitempty")
|
||||||
|
end
|
||||||
|
|
||||||
|
st["should parse tags with options specified separately"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args { "json", "yaml", "json=omitempty" }
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,yaml")
|
||||||
|
t.eq(out.options, "json=omitempty")
|
||||||
|
end
|
||||||
|
|
||||||
|
st["should parse tags with options specified separately and separated by comma"] = function()
|
||||||
|
local out = require("gopher.struct_tags").parse_args { "json,yaml,json=omitempty" }
|
||||||
|
|
||||||
|
t.eq(out.tags, "json,yaml")
|
||||||
|
t.eq(out.options, "json=omitempty")
|
||||||
|
end
|
||||||
|
|
||||||
|
return T
|
||||||
|
|
@ -22,4 +22,38 @@ utils["should .trimend()"] = function()
|
||||||
t.eq(u.trimend " hi ", " hi")
|
t.eq(u.trimend " hi ", " hi")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
utils["should add .indent() spaces"] = function()
|
||||||
|
local u = require "gopher._utils"
|
||||||
|
local line = " func Test() error {"
|
||||||
|
local indent = 4
|
||||||
|
|
||||||
|
t.eq(" ", u.indent(line, indent))
|
||||||
|
end
|
||||||
|
|
||||||
|
utils["should add .indent() a tab"] = function()
|
||||||
|
local u = require "gopher._utils"
|
||||||
|
local line = "\tfunc Test() error {"
|
||||||
|
local indent = 1
|
||||||
|
|
||||||
|
t.eq("\t", u.indent(line, indent))
|
||||||
|
end
|
||||||
|
|
||||||
|
utils["should add .indent() 2 tabs"] = function()
|
||||||
|
local u = require "gopher._utils"
|
||||||
|
local line = "\t\tfunc Test() error {"
|
||||||
|
local indent = 2
|
||||||
|
|
||||||
|
t.eq("\t\t", u.indent(line, indent))
|
||||||
|
end
|
||||||
|
|
||||||
|
utils["should .list_unique on list with duplicates"] = function()
|
||||||
|
local u = require "gopher._utils"
|
||||||
|
t.eq({ "json", "xml" }, u.list_unique { "json", "xml", "json" })
|
||||||
|
end
|
||||||
|
|
||||||
|
utils["should .list_unique on list with no duplicates"] = function()
|
||||||
|
local u = require "gopher._utils"
|
||||||
|
t.eq({ "json", "xml" }, u.list_unique { "json", "xml" })
|
||||||
|
end
|
||||||
|
|
||||||
return T
|
return T
|
||||||
|
|
|
||||||
BIN
vhs/iferr.gif
BIN
vhs/iferr.gif
Binary file not shown.
|
Before Width: | Height: | Size: 89 KiB After Width: | Height: | Size: 93 KiB |
|
|
@ -1,6 +1,6 @@
|
||||||
package demos
|
package demos
|
||||||
|
|
||||||
func ifErr() {
|
func ifErr() error {
|
||||||
out, err := doSomething()
|
out, err := doSomething()
|
||||||
|
|
||||||
_ = out
|
_ = out
|
||||||
|
|
|
||||||
BIN
vhs/tags.gif
BIN
vhs/tags.gif
Binary file not shown.
|
Before Width: | Height: | Size: 252 KiB After Width: | Height: | Size: 280 KiB |
|
|
@ -26,6 +26,6 @@ Type ":w" Enter
|
||||||
Sleep 1s
|
Sleep 1s
|
||||||
|
|
||||||
Type@400ms "jVjj"
|
Type@400ms "jVjj"
|
||||||
Type ":GoTagAdd xml" Sleep 500ms Enter
|
Type ":GoTagAdd json=omitempty" Sleep 500ms Enter
|
||||||
|
|
||||||
Sleep 5s
|
Sleep 5s
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue