diff --git a/README.md b/README.md index b2cdb2a..4519fc4 100644 --- a/README.md +++ b/README.md @@ -234,7 +234,8 @@ LSP supported by nvim-lsp is good enough for a gopher. If you looking for a bett [navigator](https://github.com/ray-x/navigator.lua), or lspsaga, and lsp-utils etc. ## LSP CodeLens -Gopls support code lens. To run gopls code lens action `GoCodeLenAct` +Gopls supports code lens. To run gopls code lens action `GoCodeLenAct` +Note: codelens need to be enabled in gopls, check default config in ## Lint @@ -284,7 +285,7 @@ The plugin will setup debugger. But you need to install Also you can check telescope dap extension : nvim-telescope/telescope-dap.nvim -Sample vimrc +Sample vimrc for DAP ```viml Plug 'mfussenegger/nvim-dap' Plug 'rcarriga/nvim-dap-ui' @@ -302,7 +303,7 @@ Check [go.lua](https://github.com/ray-x/go.nvim/blob/master/lua/go.lua) on all t Configure from lua suggested, The default setup: ```lua -require('go').setup(cfg = { +require('go').setup({ goimport='gofumports', -- goimport command gofmt = 'gofumpt', --gofmt cmd, max_line_len = 120, -- max line length in goline format @@ -316,10 +317,12 @@ require('go').setup(cfg = { lsp_on_attach = true, -- if a on_attach function provided: attach on_attach function to gopls -- true: will use go.nvim on_attach if true -- nil/false do nothing + lsp_codelens = true, -- set to false to disable codelens, true by default + gopls_remote_auto = true, -- add -remote=auto to gopls gopls_cmd = nil, -- if you need to specify gopls path and cmd, e.g {"/home/user/lsp/gopls", "-logfile", "/var/log/gopls.log" } lsp_diag_hdlr = true, -- hook lsp diag handler - dap_debug = false, -- set to true to enable dap + dap_debug = true, -- set to false to disable dap dap_debug_keymap = true, -- set keymaps for debugger dap_debug_gui = true, -- set to true to enable dap gui, highly recommand dap_debug_vt = true, -- set to true to enable dap virtual text diff --git a/lua/go.lua b/lua/go.lua index b5ca1fd..e74be80 100644 --- a/lua/go.lua +++ b/lua/go.lua @@ -13,8 +13,10 @@ _GO_NVIM_CFG = { lsp_gofumpt = false, -- true: set default gofmt in gopls format to gofumpt lsp_on_attach = nil, -- provides a on_attach function to gopls, will use go.nvim on_attach if nil lsp_diag_hdlr = true, -- hook lsp diag handler - dap_debug = false, - dap_debug_gui = false, + lsp_codelens = true, + gopls_remote_auto = true, + dap_debug = true, + dap_debug_gui = true, dap_vt = true, -- false, true and 'all frames' gopls_cmd = nil --- you can provide gopls path and cmd if it not in PATH, e.g. cmd = { "/home/ray/.local/nvim/data/lspinstall/go/gopls" } } @@ -99,12 +101,14 @@ function go.setup(cfg) end if _GO_NVIM_CFG.lsp_cfg then - require 'go.lsp' + require 'go.lsp'.setup() if _GO_NVIM_CFG.lsp_diag_hdlr then require 'go.lsp_diag' end end - require'go.codelens'.setup() + if _GO_NVIM_CFG.lsp_codelens then + require'go.codelens'.setup() + end end return go diff --git a/lua/go/codelens.lua b/lua/go/codelens.lua index 7978938..229461e 100644 --- a/lua/go/codelens.lua +++ b/lua/go/codelens.lua @@ -1,4 +1,5 @@ -local log = require"go.utils".log +local utils = require "go.utils" +local log = utils.log local codelens = require "vim.lsp.codelens" local api = vim.api @@ -35,7 +36,7 @@ function M.setup() vim.cmd('augroup go.codelenses') vim.cmd(' autocmd!') - vim.cmd('autocmd BufEnter,CursorHold,InsertLeave lua vim.lsp.codelens.refresh()') + vim.cmd('autocmd BufEnter,CursorHold,InsertLeave lua require("go.codelens").refresh()') vim.cmd('augroup end') end @@ -62,4 +63,16 @@ function M.run_action() end end +function M.refresh() + if _GO_NVIM_CFG.lsp_codelens == false then + return + end + if not utils.check_capabilities("code_lens") then + -- _GO_NVIM_CFG.lsp_codelens = false + log("code lens not supported by your gopls") + return + end + vim.lsp.codelens.refresh() +end + return M diff --git a/lua/go/lsp.lua b/lua/go/lsp.lua index 112390c..1dec518 100644 --- a/lua/go/lsp.lua +++ b/lua/go/lsp.lua @@ -59,7 +59,6 @@ local gopls = { message_level = vim.lsp.protocol.MessageType.Error, cmd = { "gopls", -- share the gopls instance if there is one already - "-remote=auto", --[[ debug options ]] -- "-remote.debug=:0" }, @@ -72,7 +71,9 @@ local gopls = { analyses = {unusedparams = true, unreachable = false}, codelenses = { generate = true, -- show the `go generate` lens. - gc_details = true -- // Show a code lens toggling the display of gc's choices. + gc_details = true, -- // Show a code lens toggling the display of gc's choices. + test = true, + tidy = true }, usePlaceholders = true, completeUnimported = true, @@ -89,21 +90,30 @@ local gopls = { } } -if _GO_NVIM_CFG.lsp_on_attach then - if _GO_NVIM_CFG.lsp_on_attach == true then - gopls.on_attach = on_attach - else - gopls.on_attach = _GO_NVIM_CFG.lsp_on_attach +local M = {} + +function M.setup() + if _GO_NVIM_CFG.lsp_on_attach then + if _GO_NVIM_CFG.lsp_on_attach == true then + gopls.on_attach = on_attach + else + gopls.on_attach = _GO_NVIM_CFG.lsp_on_attach + end end -else - print("gopls on_attach not set") -end -if _GO_NVIM_CFG.gopls_cmd then - gopls.cmd = _GO_NVIM_CFG.gopls_cmd -end + if _GO_NVIM_CFG.gopls_cmd then + gopls.cmd = _GO_NVIM_CFG.gopls_cmd + end -if _GO_NVIM_CFG.lsp_gofumpt then - gopls.settings.gopls.gofumpt = true + if _GO_NVIM_CFG.lsp_gofumpt then + gopls.settings.gopls.gofumpt = true + end + + if _GO_NVIM_CFG.gopls_remote_auto then + table.insert(gopls.cmd, "-remote=auto") + end + + require'lspconfig'.gopls.setup(gopls) end -require'lspconfig'.gopls.setup(gopls) + +return M diff --git a/lua/go/utils.lua b/lua/go/utils.lua index ea823e5..3db7ee0 100644 --- a/lua/go/utils.lua +++ b/lua/go/utils.lua @@ -75,7 +75,10 @@ end util.log = function(...) local arg = {...} - local log_path = _GO_NVIM_CFG.log_path or "/tmp/gonvim.log" + local log_default = string.format("%s/%s.log", vim.api.nvim_call_function("stdpath", {"data"}), + "gonvim") + + local log_path = _GO_NVIM_CFG.log_path or log_default if _GO_NVIM_CFG.verbose == true then local str = "  " for i, v in ipairs(arg) do @@ -208,4 +211,29 @@ function util.load_plugin(name, modulename) return plugin end +function util.check_capabilities(feature, client_id) + local clients = vim.lsp.buf_get_clients(client_id or 0) + + local supported_client = false + for _, client in pairs(clients) do + util.log(client.resolved_capabilities) + supported_client = client.resolved_capabilities[feature] + if supported_client then + goto continue + end + end + + ::continue:: + if supported_client then + return true + else + if #clients == 0 then + print("LSP: no client attached") + else + log("LSP: server does not support " .. feature) + end + return false + end +end + return util diff --git a/samplevimrc.vim b/samplevimrc.vim new file mode 100644 index 0000000..b3f5e8a --- /dev/null +++ b/samplevimrc.vim @@ -0,0 +1,42 @@ +call plug#begin('~/.vim/plugged') + +Plug 'neovim/nvim-lspconfig' +" Plug 'ray-x/go.nvim' +Plug '~/github/go.nvim' +Plug 'mfussenegger/nvim-dap' +Plug 'rcarriga/nvim-dap-ui' +Plug 'theHamsta/nvim-dap-virtual-text' + +" Plug 'hrsh7th/nvim-compe' and other plugins you commenly use... + +" optional, if you need treesitter symbol support +Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'} + +call plug#end() + +" No need for rquire('lspconfig'), navigator will configure it for you +lua <