diff options
| author | kylo252 <59826753+kylo252@users.noreply.github.com> | 2021-08-28 16:18:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-28 10:18:21 -0400 |
| commit | afc4571dc8c92948131d97f9a8a1512613c5d533 (patch) | |
| tree | 693cb511939012361566c22a11c2011b9724275b /lua/lspconfig/ui/lspinfo.lua | |
| parent | [docgen] Update CONFIG.md (diff) | |
| download | nvim-lspconfig-afc4571dc8c92948131d97f9a8a1512613c5d533.tar nvim-lspconfig-afc4571dc8c92948131d97f9a8a1512613c5d533.tar.gz nvim-lspconfig-afc4571dc8c92948131d97f9a8a1512613c5d533.tar.bz2 nvim-lspconfig-afc4571dc8c92948131d97f9a8a1512613c5d533.tar.lz nvim-lspconfig-afc4571dc8c92948131d97f9a8a1512613c5d533.tar.xz nvim-lspconfig-afc4571dc8c92948131d97f9a8a1512613c5d533.tar.zst nvim-lspconfig-afc4571dc8c92948131d97f9a8a1512613c5d533.zip | |
feat(lspinfo): improve layout (#1186)
Diffstat (limited to 'lua/lspconfig/ui/lspinfo.lua')
| -rw-r--r-- | lua/lspconfig/ui/lspinfo.lua | 282 |
1 files changed, 173 insertions, 109 deletions
diff --git a/lua/lspconfig/ui/lspinfo.lua b/lua/lspconfig/ui/lspinfo.lua index 90839291..5178939d 100644 --- a/lua/lspconfig/ui/lspinfo.lua +++ b/lua/lspconfig/ui/lspinfo.lua @@ -1,5 +1,112 @@ local configs = require 'lspconfig/configs' local windows = require 'lspconfig/ui/windows' +local util = require 'lspconfig/util' + +local error_messages = { + cmd_not_found = 'Unable to find executable. Please check your path and ensure the server is installed', + no_filetype_defined = 'No filetypes defined, Please define filetypes in setup()', +} + +local function trim_blankspace(cmd) + local trimmed_cmd = {} + for _, str in pairs(cmd) do + table.insert(trimmed_cmd, str:match '^%s*(.*)') + end + return trimmed_cmd +end + +local function indent_lines(lines, offset) + return vim.tbl_map(function(val) + return offset .. val + end, lines) +end + +local function remove_newlines(cmd) + cmd = trim_blankspace(cmd) + cmd = table.concat(cmd, ' ') + cmd = vim.split(cmd, '\n') + cmd = trim_blankspace(cmd) + cmd = table.concat(cmd, ' ') + return cmd +end + +local function make_config_info(config) + local config_info = {} + config_info.name = config.name + if config.cmd then + config_info.cmd = remove_newlines(config.cmd) + if vim.fn.executable(config.cmd[1]) == 1 then + config_info.cmd_is_executable = 'true' + else + config_info.cmd_is_executable = error_messages.cmd_not_found + end + else + config_info.cmd = 'cmd not defined' + config_info.cmd_is_executable = 'NA' + end + + local buffer_dir = vim.fn.expand '%:p:h' + config_info.root_dir = config.get_root_dir(buffer_dir) or 'NA' + config_info.autostart = (config.autostart and 'true') or 'false' + config_info.handlers = table.concat(vim.tbl_keys(config.handlers), ', ') + config_info.filetypes = table.concat(config.filetypes or {}, ', ') + + local lines = { + 'Config: ' .. config_info.name, + } + + local info_lines = { + 'filetypes: ' .. config_info.filetypes, + 'root directory: ' .. config_info.root_dir, + 'cmd: ' .. config_info.cmd, + 'cmd is executable: ' .. config_info.cmd_is_executable, + 'autostart: ' .. config_info.autostart, + 'custom handlers: ' .. config_info.handlers, + } + + vim.list_extend(lines, indent_lines(info_lines, '\t')) + + return lines +end + +local function make_client_info(client) + local client_info = {} + + client_info.cmd = remove_newlines(client.config.cmd) + client_info.root_dir = client.workspaceFolders[1].name + 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), ', ') + + local lines = { + '', + 'Client: ' + .. client.name + .. ' (id: ' + .. tostring(client.id) + .. ', pid: ' + .. tostring(client.rpc.pid) + .. ', bufnr: [' + .. client_info.attached_buffers_list + .. '])', + } + + local info_lines = { + 'filetypes: ' .. client_info.filetypes, + 'autostart: ' .. client_info.autostart, + 'root directory: ' .. client_info.root_dir, + 'cmd: ' .. client_info.cmd, + } + + if client.config.lspinfo then + local server_specific_info = client.config.lspinfo(client.config) + info_lines = vim.list_extend(info_lines, server_specific_info) + end + + vim.list_extend(lines, indent_lines(info_lines, '\t')) + + return lines +end return function() -- These options need to be cached before switching to the floating @@ -7,7 +114,6 @@ return function() local buf_clients = vim.lsp.buf_get_clients() local clients = vim.lsp.get_active_clients() local buffer_filetype = vim.bo.filetype - local buffer_dir = vim.fn.expand '%:p:h' local win_info = windows.percentage_range_window(0.8, 0.7) local bufnr, win_id = win_info.bufnr, win_info.win_id @@ -19,137 +125,95 @@ return function() table.insert(buf_client_names, client.name) end - local header = { - 'Configured servers: ' .. table.concat(vim.tbl_keys(configs), ', '), - 'Neovim logs at: ' .. (vim.lsp.get_log_path()), - '', - tostring(#vim.tbl_keys(buf_clients)) .. ' client(s) attached to this buffer: ' .. table.concat( - buf_client_names, - ', ' - ), - } - vim.list_extend(buf_lines, header) - - local function trim_whitespace(cmd) - local trimmed_cmd = {} - for _, str in pairs(cmd) do - table.insert(trimmed_cmd, str:match '^%s*(.*)') - end - return trimmed_cmd - end - - local function remove_newlines(cmd) - cmd = trim_whitespace(cmd) - cmd = table.concat(cmd, ' ') - cmd = vim.split(cmd, '\n') - cmd = trim_whitespace(cmd) - cmd = table.concat(cmd, ' ') - return cmd + local buf_client_ids = {} + for _, client in pairs(buf_clients) do + table.insert(buf_client_ids, client.id) end - local function autostart_to_str(autostart) - local autostart_status = 'True' - if autostart == false then - autostart_status = 'Disabled' + local other_active_clients = {} + local client_names = {} + for _, client in pairs(clients) do + if not vim.tbl_contains(buf_client_ids, client.id) then + table.insert(other_active_clients, client) end - return autostart_status + table.insert(client_names, client.name) end - local indent = ' ' - local function make_client_info(client) - local lines = { - '', - indent - .. 'Client: ' - .. client.name - .. ' (id: ' - .. tostring(client.id) - .. ' pid: ' - .. tostring(client.rpc.pid) - .. ')', - indent .. '\troot: ' .. client.workspaceFolders[1].name, - indent .. '\tfiletypes: ' .. table.concat(client.config.filetypes or {}, ', '), - indent .. '\tcmd: ' .. remove_newlines(client.config.cmd), - indent .. '\tautostart: ' .. autostart_to_str(client._autostart), - } - if client.config.lspinfo then - local server_specific_info = client.config.lspinfo(client.config) - server_specific_info = vim.tbl_map(function(val) - return indent .. '\t' .. val - end, server_specific_info) - lines = vim.list_extend(lines, server_specific_info) - end - return lines - end + local header = { + '', + 'Language client log: ' .. (vim.lsp.get_log_path()), + 'Detected filetype: ' .. buffer_filetype, + } + vim.list_extend(buf_lines, header) + local buffer_clients_header = { + '', + tostring(#vim.tbl_keys(buf_clients)) .. ' client(s) attached to this buffer: ', + } + + vim.list_extend(buf_lines, buffer_clients_header) for _, client in pairs(buf_clients) do local client_info = make_client_info(client) vim.list_extend(buf_lines, client_info) end - local active_section_header = { + local other_active_section_header = { '', - tostring(#clients) .. ' active client(s): ', + tostring(#other_active_clients) .. ' active client(s) not attached to this buffer: ', } - vim.list_extend(buf_lines, active_section_header) - for _, client in pairs(clients) do + if not vim.tbl_isempty(other_active_clients) then + 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) vim.list_extend(buf_lines, client_info) end - local matching_config_header = { + + local other_matching_configs_header = { + '', + 'Other clients that match the filetype: ' .. buffer_filetype, '', - 'Clients that match the filetype ' .. buffer_filetype .. ':', } - local cmd_not_found_msg = 'False. Please check your path and ensure the server is installed' - vim.list_extend(buf_lines, matching_config_header) - for _, config in pairs(configs) do - local cmd_is_executable, cmd - if config.cmd then - cmd = remove_newlines(config.cmd) - if vim.fn.executable(config.cmd[1]) == 1 then - cmd_is_executable = 'True' - else - cmd_is_executable = cmd_not_found_msg - end - else - cmd = 'cmd not defined' - cmd_is_executable = cmd - end - if config.filetypes then - for _, filetype_match in pairs(config.filetypes) do - if buffer_filetype == filetype_match then - local matching_config_info = { - indent .. '', - indent .. 'Config: ' .. config.name, - indent .. '\tcmd: ' .. cmd, - indent .. '\tcmd is executable: ' .. cmd_is_executable, - indent .. '\tidentified root: ' .. (config.get_root_dir(buffer_dir) or 'None'), - indent .. '\tautostart: ' .. autostart_to_str(config._autostart), - indent .. '\tcustom handlers: ' .. table.concat(vim.tbl_keys(config.handlers), ', '), - } - vim.list_extend(buf_lines, matching_config_info) - end - end - else - local matching_config_info = { - '', - 'Config: ' .. config.name, - '\tfiletype: ' .. 'No filetypes defined, please define filetypes in setup().', - } - vim.list_extend(buf_lines, matching_config_info) + + local other_matching_configs = util.get_other_matching_providers(buffer_filetype) + + if not vim.tbl_isempty(other_matching_configs) then + vim.list_extend(buf_lines, other_matching_configs_header) + for _, config in pairs(other_matching_configs) do + vim.list_extend(buf_lines, make_config_info(config)) end end - buf_lines = vim.lsp.util._trim(buf_lines, {}) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, buf_lines) + + local matching_config_header = { + '', + 'Configured servers list: ' .. table.concat(vim.tbl_keys(configs), ', '), + } + vim.list_extend(buf_lines, matching_config_header) + + local fmt_buf_lines = indent_lines(buf_lines, ' ') + + fmt_buf_lines = vim.lsp.util._trim(fmt_buf_lines, {}) + + vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, fmt_buf_lines) vim.api.nvim_buf_set_option(bufnr, 'modifiable', false) vim.api.nvim_buf_set_option(bufnr, 'filetype', 'lspinfo') - local configs_pattern = '\\%(' .. table.concat(vim.tbl_keys(configs), '\\|') .. '\\)' - vim.cmd('syntax match Title /\\%(Client\\|Config\\):.*\\zs' .. configs_pattern .. '/') - vim.cmd('syntax match Identifier /filetypes:.*\\zs\\<' .. buffer_filetype .. '\\>/') + + vim.api.nvim_buf_set_keymap(bufnr, 'n', '<esc>', '<cmd>bd<CR>', { noremap = true }) + vim.lsp.util.close_preview_autocmd({ 'BufHidden', 'BufLeave' }, win_id) + vim.fn.matchadd( 'Error', - 'No filetypes defined, please define filetypes in setup().\\|' .. 'cmd not defined\\|' .. cmd_not_found_msg + error_messages.no_filetype_defined .. '.\\|' .. 'cmd not defined\\|' .. error_messages.cmd_not_found ) - vim.api.nvim_buf_set_keymap(bufnr, 'n', '<esc>', '<cmd>bd<CR>', { noremap = true }) - vim.lsp.util.close_preview_autocmd({ 'BufHidden', 'BufLeave' }, win_id) + vim.cmd 'let m=matchadd("string", "true")' + vim.cmd 'let m=matchadd("error", "false")' + for _, config in pairs(configs) do + vim.fn.matchadd('Title', '\\%(Client\\|Config\\):.*\\zs' .. config.name .. '\\ze') + vim.fn.matchadd('Visual', 'list:.*\\zs' .. config.name .. '\\ze') + if config.filetypes then + for _, ft in pairs(config.filetypes) do + vim.fn.matchadd('Type', '\\%(filetypes\\|filetype\\):.*\\zs' .. ft .. '\\ze') + end + end + end end |
