diff options
| author | Raphael <glepnir@neovim.pro> | 2022-12-11 19:25:44 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-11 19:25:44 +0800 |
| commit | 6eb24ef9175d1fa3c7a23e115854b1a2d923d386 (patch) | |
| tree | d62431dbb0e480ef8fa0caac0d57f1cdacf321cf /lua | |
| parent | fix: check client is available and get client id from cache (#2303) (diff) | |
| download | nvim-lspconfig-6eb24ef9175d1fa3c7a23e115854b1a2d923d386.tar nvim-lspconfig-6eb24ef9175d1fa3c7a23e115854b1a2d923d386.tar.gz nvim-lspconfig-6eb24ef9175d1fa3c7a23e115854b1a2d923d386.tar.bz2 nvim-lspconfig-6eb24ef9175d1fa3c7a23e115854b1a2d923d386.tar.lz nvim-lspconfig-6eb24ef9175d1fa3c7a23e115854b1a2d923d386.tar.xz nvim-lspconfig-6eb24ef9175d1fa3c7a23e115854b1a2d923d386.tar.zst nvim-lspconfig-6eb24ef9175d1fa3c7a23e115854b1a2d923d386.zip | |
feat: support multiple workspaceFolders and show correct root in Lspinfo (#2304)
* feat: support multiple workspaceFolders and show correct root in Lspinfo
* fix: add return branch
* fix: convert path before compare
* fix: improve find root dir in LspInfo
* fix: check logic
* fix: remove unnecessary code
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/lspconfig/ui/lspinfo.lua | 44 | ||||
| -rw-r--r-- | lua/lspconfig/util.lua | 36 |
2 files changed, 59 insertions, 21 deletions
diff --git a/lua/lspconfig/ui/lspinfo.lua b/lua/lspconfig/ui/lspinfo.lua index d7f37b7b..40481221 100644 --- a/lua/lspconfig/ui/lspinfo.lua +++ b/lua/lspconfig/ui/lspinfo.lua @@ -1,4 +1,4 @@ -local api, fn = vim.api, vim.fn +local api, fn, lsp = vim.api, vim.fn, vim.lsp local windows = require 'lspconfig.ui.windows' local util = require 'lspconfig.util' @@ -103,14 +103,37 @@ local function make_config_info(config, bufnr) return lines end -local function make_client_info(client) +local function make_client_info(client, fname) 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' + fname = uv.fs_realpath(fname) + local sep = is_windows and '\\' or '/' + local fname_parts = vim.split(fname, sep, { trimempty = true }) if workspace_folders then - client_info.root_dir = workspace_folders[1].name - else + for _, schema in pairs(workspace_folders) do + local matched = true + local root = uv.fs_realpath(schema.name) + local root_parts = vim.split(root, sep, { trimempty = true }) + + for i = 1, #root_parts do + if root_parts[i] ~= fname_parts[i] then + matched = false + break + end + end + + if matched then + client_info.root_dir = schema.name + break + end + end + end + + if not client_info.root_dir then client_info.root_dir = 'Running in single file mode.' end client_info.filetypes = table.concat(client.config.filetypes or {}, ', ') @@ -123,8 +146,6 @@ local function make_client_info(client) .. client.name .. ' (id: ' .. tostring(client.id) - .. ', pid: ' - .. tostring(client.rpc.pid) .. ', bufnr: [' .. client_info.attached_buffers_list .. '])', @@ -150,10 +171,11 @@ end return function() -- These options need to be cached before switching to the floating -- buffer. - local buf_clients = vim.lsp.buf_get_clients() - local clients = vim.lsp.get_active_clients() - local buffer_filetype = vim.bo.filetype local original_bufnr = api.nvim_get_current_buf() + 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 @@ -194,7 +216,7 @@ return function() vim.list_extend(buf_lines, buffer_clients_header) for _, client in pairs(buf_clients) do - local client_info = make_client_info(client) + local client_info = make_client_info(client, fname) vim.list_extend(buf_lines, client_info) end @@ -206,7 +228,7 @@ return function() vim.list_extend(buf_lines, other_active_section_header) end for _, client in pairs(other_active_clients) do - local client_info = make_client_info(client) + local client_info = make_client_info(client, fname) vim.list_extend(buf_lines, client_info) end diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index d4390e40..f7614924 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -247,20 +247,36 @@ function M.server_per_root_dir_manager(make_config) return end + local get_client_from_cache = function(conf) + local id + if vim.tbl_count(clients) == 1 then + id = vim.tbl_values(clients)[1] + elseif vim.tbl_count(single_file_clients) == 1 then + id = vim.tbl_values(single_file_clients)[1] + else + return + end + local client = lsp.get_client_by_id(id) + if client and client.name == conf.name then + return client + end + return nil + end + -- Check if we have a client already or start and store it. if not client_id then local new_config = make_config(root_dir) - if vim.tbl_count(clients) == 1 then - local id = vim.tbl_values(clients)[1] - local client = lsp.get_client_by_id(id) - if client and client.name == new_config.name then - local params = lsp.util.make_workspace_params( - { { uri = vim.uri_from_fname(root_dir), name = root_dir } }, - { {} } - ) - table.insert(client.workspace_folders, params.event.added[1]) - return id + local client = get_client_from_cache(new_config) + if client then + local params = lsp.util.make_workspace_params( + { { uri = vim.uri_from_fname(root_dir), name = root_dir } }, + { {} } + ) + if not client.workspace_folders then + client.workspace_folders = {} end + table.insert(client.workspace_folders, params.event.added[1]) + return client.id end -- do nothing if the client is not enabled if new_config.enabled == false then |
