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

fix(lspinfo): improve lspinfo behavior #2648

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 22 additions & 20 deletions lua/lspconfig/ui/lspinfo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,37 +121,39 @@ local function make_config_info(config, bufnr)
end

---@param client table
---@param fname string
local function make_client_info(client, fname)
---@param bufnr integer
local function make_client_info(client, bufnr)
local client_info = {}

client_info.cmd = cmd_type[type(client.config.cmd)](client.config)
local workspace_folders = fn.has 'nvim-0.9' == 1 and client.workspace_folders or client.workspaceFolders
local uv = vim.loop
local is_windows = uv.os_uname().version:match 'Windows'
local fname = api.nvim_buf_get_name(bufnr)
fname = uv.fs_realpath(fname) or fn.fnamemodify(fn.resolve(fname), ':p')
if is_windows then
fname:gsub('%/', '%\\')
end

if workspace_folders then
for _, schema in ipairs(workspace_folders) do
local matched = true
local root_dir = uv.fs_realpath(schema.name)
if fname:sub(1, root_dir:len()) ~= root_dir then
matched = false
end
for _, schema in ipairs(workspace_folders or {}) do
local matched = true
local root_dir = uv.fs_realpath(schema.name)
if fname:sub(1, root_dir:len()) ~= root_dir then
matched = false
end

if matched then
client_info.root_dir = schema.name
break
end
if matched then
client_info.root_dir = schema.name
break
end
end

if not client_info.root_dir then
if client.single_buffers and client.single_buffers[bufnr] then
client_info.root_dir = 'Running in single file mode.'
elseif client.config.root_dir then
client_info.root_dir = client.config.root_dir
end

client_info.filetypes = table.concat(client.config.filetypes or {}, ', ')
client_info.autostart = (client.config.autostart and 'true') or 'false'
client_info.attached_buffers_list = table.concat(vim.lsp.get_buffers_by_client_id(client.id), ', ')
Expand All @@ -168,10 +170,10 @@ local function make_client_info(client, fname)
}

local info_lines = {
'servercmd: ' .. client_info.cmd,
'filetypes: ' .. client_info.filetypes,
'autostart: ' .. client_info.autostart,
'root directory: ' .. client_info.root_dir,
'cmd: ' .. client_info.cmd,
'root dir: ' .. client_info.root_dir,
}

if client.config.lspinfo then
Expand All @@ -191,7 +193,6 @@ return function()
local buf_clients = lsp.get_active_clients { bufnr = original_bufnr }
local clients = lsp.get_active_clients()
local buffer_filetype = vim.bo.filetype
local fname = api.nvim_buf_get_name(original_bufnr)

windows.default_options.wrap = true
windows.default_options.breakindent = true
Expand Down Expand Up @@ -228,12 +229,12 @@ return function()

local buffer_clients_header = {
'',
tostring(#vim.tbl_keys(buf_clients)) .. ' client(s) attached to this buffer: ',
tostring(#vim.tbl_keys(buf_clients)) .. ' client(s) attached to this buffer: ' .. original_bufnr,
}

vim.list_extend(buf_lines, buffer_clients_header)
for _, client in ipairs(buf_clients) do
local client_info = make_client_info(client, fname)
local client_info = make_client_info(client, original_bufnr)
vim.list_extend(buf_lines, client_info)
end

Expand All @@ -245,7 +246,7 @@ return function()
vim.list_extend(buf_lines, other_active_section_header)
end
for _, client in ipairs(other_active_clients) do
local client_info = make_client_info(client, fname)
local client_info = make_client_info(client, original_bufnr)
vim.list_extend(buf_lines, client_info)
end

Expand Down Expand Up @@ -310,6 +311,7 @@ return function()
vim.cmd [[
syn keyword String true
syn keyword Error false
syn match Number /buffer:\s*\zs\d\+\ze/
syn match LspInfoFiletypeList /\<filetypes\?:\s*\zs.*\ze/ contains=LspInfoFiletype
syn match LspInfoFiletype /\k\+/ contained
syn match LspInfoTitle /^\s*\%(Client\|Config\):\s*\zs\S\+\ze/
Expand Down
20 changes: 19 additions & 1 deletion lua/lspconfig/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ function M.server_per_root_dir_manager(make_config)
end
end

local function register_client_single(client)
if not client.single_buffers then
client.single_buffers = {}
end
client.single_buffers[bufnr] = true
api.nvim_buf_attach(bufnr, false, {
on_detach = function()
client.single_buffers[bufnr] = nil
end,
})
end

local function attach_and_cache(root, client_id)
lsp.buf_attach_client(bufnr, client_id)
if not clients[root] then
Expand Down Expand Up @@ -347,6 +359,9 @@ function M.server_per_root_dir_manager(make_config)
return
end
attach_and_cache(root_dir, client_id)
if not new_config.root_dir then
register_client_single(vim.lsp.get_client_by_id(client_id))
end
end

local function attach_or_spawn(client)
Expand Down Expand Up @@ -384,7 +399,10 @@ function M.server_per_root_dir_manager(make_config)

if clients[root_dir] or single_file then
lsp.buf_attach_client(bufnr, client.id)
return
if clients[root_dir] then
return
end
register_client_single(client)
end

-- make sure neovim had exchanged capabilities from language server
Expand Down