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

Should re-use the same server for multiple workspaces only the server supports it #2326

Closed
xiantang opened this issue Dec 19, 2022 · 1 comment · Fixed by #2330
Closed

Should re-use the same server for multiple workspaces only the server supports it #2326

xiantang opened this issue Dec 19, 2022 · 1 comment · Fixed by #2330
Labels
bug Something isn't working

Comments

@xiantang
Copy link
Contributor

Description

when I use the master branch of nvim-lspconfig seems something is broken.
image
I suppose that is because it reused the LSP but in fact the gopls do not support the multiple workspaces
because when I checkout the version before #2287 it's work well, but when I use master branch it will encounter this issue.

Neovim version

NVIM v0.8.0
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by brew@HMBRW-A-001-M1-004.local

Features: +acl +iconv +tui
See ":help feature-compile"

system vimrc file: "$VIM/sysinit.vim"
fall-back for $VIM: "/opt/homebrew/Cellar/neovim/0.8.0/share/nvim"

Run :checkhealth for more info

Nvim-lspconfig version

No response

Operating system and version

macOS 12.5

Affected language servers

gopls

Steps to reproduce

  1. nvim open project A
  2. :pwd should be you project A path
  3. then use telescope to open the file in project B but not change your :pwd
  4. will show an error which is mod has not been indexed

image

although the root path became, but still has error.

image

Actual behavior

show an error which is mod has not been indexed

Expected behavior

the file of B project should index good.

Minimal config

local nvim_lsp = require("lspconfig")

local golang_capabilities = vim.lsp.protocol.make_client_capabilities()
golang_capabilities.textDocument.completion.completionItem.snippetSupport = true
local on_attach = function(client, bufnr)
	require("lsp_signature").on_attach(signature_setup, bufnr)
	if client.supports_method("textDocument/formatting") then
		vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
		vim.api.nvim_create_autocmd("BufWritePre", {
			group = augroup,
			buffer = bufnr,
			callback = function()
				lsp_formatting(bufnr)
			end,
		})
	end
	local function buf_set_keymap(...)
		vim.api.nvim_buf_set_keymap(bufnr, ...)
	end

	local function buf_set_option(...)
		vim.api.nvim_buf_set_option(bufnr, ...)
	end

	buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")

	-- change hold time
	vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
		border = "single",
		focusable = false,
		max_width = 80,
		max_height = 20,
	})
	-- cousor hold for 3 seconds, show signature helper
	-- silent
	-- vim.api.nvim_command [[autocmd CursorHold  <buffer> lua vim.lsp.buf.hover() ]]
	-- Mappings.
	local opts = { noremap = true, silent = true }
	buf_set_keymap("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
	buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
	buf_set_keymap("n", "gv", "<cmd>Lspsaga peek_definition<CR>", opts)
	buf_set_keymap("n", "ga", "<cmd>Lspsaga code_action<CR>", opts)
	-- -- coode action for extract function or variable
	-- buf_set_keymap("v", "ga", "cmd>lua vim.lsp.bug.code_action()<CR>", opts)
	buf_set_keymap("v", "ga", "<cmd>lua vim.lsp.buf.range_code_action()<CR>", opts)
	buf_set_keymap("n", "K", "<cmd>Lspsaga hover_doc<CR>", opts)
	buf_set_keymap("n", "<space>gi", "<cmd>Telescope lsp_implementations<CR>", opts)
	buf_set_keymap("n", "<space>dt", "<cmd>lua require('dap-go').debug_test()<CR>", opts)
	buf_set_keymap("n", "<space>wa", "<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>", opts)
	buf_set_keymap("n", "<space>wr", "<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>", opts)
	buf_set_keymap("n", "<space>wl", "<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>", opts)
	buf_set_keymap("n", "<space>D", "<cmd>lua vim.lsp.buf.type_definition()<CR>", opts)
	buf_set_keymap("n", "<space>rn", "<cmd>Lspsaga rename<CR>", opts)
	buf_set_keymap("n", "<space>gr", "<cmd>Lspsaga lsp_finder<CR>", opts)
	buf_set_keymap("n", "<space>o", "<cmd>SymbolsOutline<CR>", opts)
	-- buf_set_keymap('n', '<space>f', '<cmd>lua vim.diagnostic.set_loclist()<CR>', opts)
	-- if current buff end with _test.go, then set keymap for error
	local buf_name = vim.api.nvim_buf_get_name(bufnr)
	buf_set_keymap("n", "ge", "<cmd>Lspsaga diagnostic_jump_next<CR>", opts)

	-- buf_set_keymap('n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)

	-- Set some keybinds conditional on server capabilities
	if client.server_capabilities.document_formatting then
		buf_set_keymap("n", "ff", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
	elseif client.server_capabilities.document_range_formatting then
		buf_set_keymap("n", "ff", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
	end

	-- Set autocommands conditional on server_capabilities
	if client.server_capabilities.document_highlight then
		vim.api.nvim_exec(
			[[
      hi LspReferenceRead cterm=bold ctermbg=DarkMagenta guibg=LightYellow
      hi LspReferenceText cterm=bold ctermbg=DarkMagenta guibg=LightYellow
      hi LspReferenceWrite cterm=bold ctermbg=DarkMagenta guibg=LightYellow
      augroup lsp_document_highlight
        autocmd! * <buffer>
        autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
        autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
      augroup END
    ]],
			false
		)
	end
end


nvim_lsp.gopls.setup({
	cmd = { "gopls" },
	-- for postfix snippets and analyzers
	flags = {
		allow_incremental_sync = false,
		debounce_text_changes = 500,
	},
	capabilities = golang_capabilities,
	settings = {
		gopls = {
			experimentalPostfixCompletions = true,
			analyses = {
				unusedparams = true,
				shadow = true,
			},
			staticcheck = true,
		},
	},
	on_attach = on_attach,
})

LSP log

I have debug the code, this issue is specify

@xiantang xiantang added the bug Something isn't working label Dec 19, 2022
@xiantang
Copy link
Contributor Author

#2324 I use this branch of the pull request, will not encounter the issue now

@xiantang xiantang changed the title Should re-use the same server for multiple workspaces when the server supports it Should re-use the same server for multiple workspaces only the server supports it Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant