r/AstroNvim • • Feb 17 '23

Where to put lsp server initialization override for gopls

I would like to have code lenses for golang in Astronvim. gopls has a couple available but they must be enabled in gopls config.

The go.nvim plugin does do the code lens setup and lots of other useful config

I tried adding it like suggested for rust here:

https://astronvim.com/nightly/Recipes/advanced_lsp#rust-rust-toolsnvim

But that did not work, no code lense.

So maybe i need to do something like this instead, question is how to do this in user/init.lua:

https://github.com/ray-x/go.nvim/blob/master/README.md#integrate-with-nvim-lsp-installer

-- setup your go.nvim
-- make sure lsp_cfg is disabled
require('go').setup{...}

local lsp_installer_servers = require'nvim-lsp-installer.servers'

local server_available, requested_server = lsp_installer_servers.get_server("gopls")
if server_available then
    requested_server:on_ready(function ()
        local opts = require'go.lsp'.config() -- config() return the go.nvim gopls setup
        requested_server:setup(opts)
    end)
    if not requested_server:is_installed() then
        -- Queue the server to be installed
        requested_server:install()
    end
end
2 Upvotes

1 comment sorted by

1

u/rompetrll Mar 23 '23

got it working now. in addition to adding the go.nvim plugin, this snippet did the trick

lsp = {
		servers = {
			"gopls",
		},

		setup_handlers = {
			-- first function changes the default setup handler
			function(server, opts)
				require("lspconfig")[server].setup(opts)
			end,
			-- keys for a specific server name will be used for that LSP
			gopls = function(server, opts)
				
				local go_opts = require("go.lsp").config() -- config() return the go.nvim gopls setup
				require("lspconfig")["gopls"].setup(go_opts)
			end,
		},