Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go Comment can be colorful #505

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# go.nvim

A modern go neovim plugin based on treesitter, nvim-lsp and dap debugger. It is written in Lua and async as much as
possible. PR & Suggestions are welcome.
A modern go neovim plugin based on treesitter, nvim-lsp and dap debugger. Written in Lua and designed to be as asynchronous as possible, the repo welcomes PR & Suggestions are welcome.

The plugin covers most features required for a gopher.

Expand All @@ -13,6 +12,7 @@ The plugin covers most features required for a gopher.
- All the GoToXxx (E.g reference, implementation, definition, goto doc, peek code/doc etc) You need lspconfig setup.
There are lots of posts on how to set it up. You can also check my [navigator](https://github.com/ray-x/navigator.lua)
gopls setup [lspconfig.lua](https://github.com/ray-x/navigator.lua/blob/master/lua/navigator/lspclient/clients.lua)
- Better `go doc` with GoPkgOutline
- gopls commands: e.g. fillstruct, organize imports, list modules, list packages, gc_details, generate, change
signature, etc.
- Runtime lint/vet/compile: Supported by LSP (once you set up your LSP client), GoLint with golangci-lint also supported
Expand Down Expand Up @@ -49,7 +49,8 @@ The plugin covers most features required for a gopher.
Use your favorite package manager to install. The dependency `treesitter` (and optionally, treesitter-objects) should be
installed the first time you use it. Also Run `TSInstall go` to install the go parser if not installed yet. `sed` is
recommended to run this plugin.

<details>
<summary> plug </summary>
### [vim-plug](https://github.com/junegunn/vim-plug)

```vim
Expand All @@ -58,7 +59,10 @@ Plug 'neovim/nvim-lspconfig'
Plug 'ray-x/go.nvim'
Plug 'ray-x/guihua.lua' ; recommended if need floating window support
```
</details>

<details>
<summary> Packer </summary>
### [packer.nvim](https://github.com/wbthomason/packer.nvim)

```lua
Expand All @@ -67,7 +71,10 @@ use 'ray-x/guihua.lua' -- recommended if need floating window support
use 'neovim/nvim-lspconfig'
use 'nvim-treesitter/nvim-treesitter'
```
<details>

<details>
<summary> Lazy </summary>
### [lazy.nvim](https://github.com/folke/lazy.nvim)

```lua
Expand All @@ -86,6 +93,7 @@ use 'nvim-treesitter/nvim-treesitter'
build = ':lua require("go.install").update_all_sync()' -- if you need to install/update all binaries
}
```
</details>

The go.nvim load speed is fast and you can enable it by default
<img width="479" alt="image" src="https://user-images.githubusercontent.com/1681295/218074895-5182c791-8649-46ad-b18e-8eb1af8c0ffa.png">
Expand Down Expand Up @@ -746,6 +754,8 @@ some of them are not exposed to user, but you can still use it in your lua setup
## configuration

Configure from lua suggested, The default setup:
<details>
<summary> Default setup </summary>

```lua
require('go').setup({
Expand All @@ -766,7 +776,9 @@ require('go').setup({
comment_placeholder = '' , -- comment_placeholder your cool placeholder e.g. 󰟓    
icons = {breakpoint = '🧘', currentpos = '🏃'}, -- setup to `false` to disable icons setup
verbose = false, -- output loginf in messages
lsp_semantic_highlights = true, -- use highlights from gopls
lsp_semantic_highlights = true, -- use highlights from gopls; false: use treesitter semantic highlights
lsp_semantic_highlights_priority = nil, -- set priority < 100 to use treesitter semantic highlights; set to > 100 to
-- use gopls; nil: use default priority (125 for nvim 0.10)
lsp_cfg = false, -- true: use non-default gopls setup specified in go/lsp.lua
-- false: do nothing
-- if lsp_cfg is a table, merge table with with non-default gopls setup in go/lsp.lua, e.g.
Expand Down Expand Up @@ -876,8 +888,22 @@ require('go').setup({
on_stderr = function(err, data) _, _ = err, data end, -- callback for stderr
on_exit = function(code, signal, output) _, _, _ = code, signal, output end, -- callback for jobexit, output : string
iferr_vertical_shift = 4 -- defines where the cursor will end up vertically from the begining of if err statement
comment = {
placeholder = '' , -- comment_placeholder your cool placeholder e.g. 󰟓    
highlight = true, -- set to false to disable comment highlight
queries = nil -- set to a table of queries to use for comment highlight see comment.lua
highlight_groups = { -- default comment highlight groups, see comment.lua
-- redefine or set back to Comment to disable
-- types = 'GoCommentType',
-- functions = 'GoCommentFunction',
-- variables = 'GoCommentVariable',
-- constants = 'GoCommentConstant',
-- parameters = 'GoCommentParameter',
}
}
})
```
</details>

You will need to add keybind yourself: e.g

Expand Down Expand Up @@ -909,6 +935,9 @@ This will override your global `go.nvim` setup

I did not provide textobject support in the plugin. Please use treesitter textobject plugin. My treesitter config:

<details>
<summary> textobject setup with treesitter </summary>

```lua
require "nvim-treesitter.configs".setup {
incremental_selection = {
Expand Down
7 changes: 6 additions & 1 deletion doc/go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ You can setup go.nvim with following options:
max_line_len = 120,
tag_transform = false,
test_dir = "",
comment_placeholder = "  ",
icons = { breakpoint = "🧘", currentpos = "🏃" }, -- set to false to disable
-- this option
verbose = false,
Expand Down Expand Up @@ -458,6 +457,12 @@ You can setup go.nvim with following options:
run_in_floaterm = false, -- set to true to run in float window.
luasnip = false, -- set true to enable included luasnip
iferr_vertical_shift = 4 -- defines where the cursor will end up vertically from the begining of if err statement after GoIfErr command
comment = {
placeholder = '  ',
highlight = true, -- set to false to disable
queries = nil, -- set to a table of queries to use for comment highlight see comment.lua
highlight_groups = nil
}
}

vim:tw=78:ts=8:sts=8:sw=8:ft=help:norl:expandtab
35 changes: 24 additions & 11 deletions lua/go.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ _GO_NVIM_CFG = {
gotests_template_dir = '', -- sets gotests -template_dir parameter (check gotests for details)
gotest_case_exact_match = true, -- default to true, if set to false will match any part of the test name

comment_placeholder = '  ',
icons = { breakpoint = '🧘', currentpos = '🏃' }, -- set to false to disable icons setup
sign_priority = 7, -- set priority of signs used by go.nevim
verbose = false,
Expand All @@ -27,7 +26,9 @@ _GO_NVIM_CFG = {
-- true: apply non-default gopls setup defined in go/gopls.lua
-- if lsp_cfg is a table, merge table with with non-default gopls setup in go/gopls.lua, e.g.
lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt
lsp_semantic_highlights = true, -- use highlights from gopls
lsp_semantic_highlights = true, -- use highlights from gopls, false: use treesitter highlights
lsp_semantic_highlights_priority = nil, -- set priority of semantic highlights to < 100 to use treesitter highlights;
-- > 100 to use gopls semantics token
lsp_on_attach = nil, -- nil: use on_attach function defined in go/lsp.lua for gopls,
-- when lsp_cfg is true
-- if lsp_on_attach is a function: use this function as on_attach function for gopls,
Expand Down Expand Up @@ -83,9 +84,11 @@ _GO_NVIM_CFG = {
-- so you will run `watchexe --restart -v -e go go run `
end,
},
-- deprecated setups for nvim version < 0.10
lsp_inlay_hints = {
enable = true,

-- deprecated setups for nvim version < 0.10

style = 'inlay', -- 'default: inlay', 'eol': show at end of line, 'inlay': show in the middle of the line

-- Note: following setup only for for style == 'eol'
Expand Down Expand Up @@ -177,6 +180,13 @@ _GO_NVIM_CFG = {
_, _, _ = code, signal, output
end, -- callback for jobexit, output : string
iferr_vertical_shift = 4, -- defines where the cursor will end up vertically from the begining of if err statement after GoIfErr command
comment = {
placeholder = '  ',
highlight = false, -- set to true to disable
queries = nil, -- set to a table of queries to use for comment highlight see comment.lua
highlight_groups = nil,
highlight_debounce = 1000, -- 1000ms
},
}

-- TODO: nvim_{add,del}_user_command https://github.com/neovim/neovim/pull/16752
Expand Down Expand Up @@ -218,18 +228,16 @@ function go.setup(cfg)
vim.log.levels.WARN
)
end
if cfg.goimport ~= nil then
vim.notify('go.nvim goimport deprecated, use goimports', vim.log.levels.WARN)
cfg.goimports = cfg.goimport
end
if cfg.lsp_diag_virtual_text ~= nil then
if cfg.comment_placeholder ~= nil then
vim.notify(
'go.nvim lsp_diag_virtual_text deprecated, use diagnostic.virtual_text',
'go.nvim comment_placeholder deprecated, use comment.placeholder',
vim.log.levels.WARN
)
cfg.comment.placeholder = cfg.comment_placeholder
end
if cfg.lsp_diag_signs ~= nil then
vim.notify('go.nvim lsp_diag_signs deprecated, use diagnostic.signs', vim.log.levels.WARN)
if cfg.goimport ~= nil then
vim.notify('go.nvim goimport deprecated, use goimports', vim.log.levels.WARN)
cfg.goimports = cfg.goimport
end
if cfg.disable_defaults then
reset_tbl(_GO_NVIM_CFG)
Expand All @@ -242,6 +250,10 @@ function go.setup(cfg)
vim.notify('go.nvim go binary is not setup', vim.log.levels.ERROR)
end

if _GO_NVIM_CFG.lsp_semantic_highlights_priority then
vim.highlight.priorities.semantic_tokens = _GO_NVIM_CFG.lsp_semantic_highlights_priority
end

if _GO_NVIM_CFG.max_line_len > 0 and _GO_NVIM_CFG.gofmt ~= 'golines' then
vim.notify('go.nvim max_line_len only effective when gofmt is golines', vim.log.levels.WARN)
end
Expand All @@ -250,6 +262,7 @@ function go.setup(cfg)
vim.defer_fn(function()
require('go.project').load_project()
require('go.utils').set_nulls()
require('go.comment')
end, 1)

if _GO_NVIM_CFG.run_in_floaterm then
Expand Down
Loading
Loading