Skip to content

Commit

Permalink
fix(lspinfo): improve lspinfo behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed Jun 4, 2023
1 parent 90f5b0c commit 91ba814
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
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
18 changes: 17 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,7 @@ function M.server_per_root_dir_manager(make_config)
return
end
attach_and_cache(root_dir, client_id)
register_client_single(vim.lsp.get_client_by_id(client_id))
end

local function attach_or_spawn(client)
Expand Down Expand Up @@ -384,7 +397,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

0 comments on commit 91ba814

Please sign in to comment.