aboutsummaryrefslogtreecommitdiffstats
path: root/lua/lspconfig/ui/lspinfo.lua
diff options
context:
space:
mode:
authorMichael Lingelbach <m.j.lbach@gmail.com>2021-08-24 08:40:45 -0400
committerGitHub <noreply@github.com>2021-08-24 08:40:45 -0400
commit09c3ca620e21a365630006e98823fcf4888e7e11 (patch)
treed2ef2b152a16e8460ebd6b606bb0401aca59878c /lua/lspconfig/ui/lspinfo.lua
parent[docgen] Update CONFIG.md (diff)
downloadnvim-lspconfig-09c3ca620e21a365630006e98823fcf4888e7e11.tar
nvim-lspconfig-09c3ca620e21a365630006e98823fcf4888e7e11.tar.gz
nvim-lspconfig-09c3ca620e21a365630006e98823fcf4888e7e11.tar.bz2
nvim-lspconfig-09c3ca620e21a365630006e98823fcf4888e7e11.tar.lz
nvim-lspconfig-09c3ca620e21a365630006e98823fcf4888e7e11.tar.xz
nvim-lspconfig-09c3ca620e21a365630006e98823fcf4888e7e11.tar.zst
nvim-lspconfig-09c3ca620e21a365630006e98823fcf4888e7e11.zip
chore: refactor ui components (#1172)
Diffstat (limited to 'lua/lspconfig/ui/lspinfo.lua')
-rw-r--r--lua/lspconfig/ui/lspinfo.lua155
1 files changed, 155 insertions, 0 deletions
diff --git a/lua/lspconfig/ui/lspinfo.lua b/lua/lspconfig/ui/lspinfo.lua
new file mode 100644
index 00000000..90839291
--- /dev/null
+++ b/lua/lspconfig/ui/lspinfo.lua
@@ -0,0 +1,155 @@
+local configs = require 'lspconfig/configs'
+local windows = require 'lspconfig/ui/windows'
+
+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 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
+
+ local buf_lines = {}
+
+ local buf_client_names = {}
+ for _, client in pairs(buf_clients) do
+ 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
+ end
+
+ local function autostart_to_str(autostart)
+ local autostart_status = 'True'
+ if autostart == false then
+ autostart_status = 'Disabled'
+ end
+ return autostart_status
+ 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
+
+ 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 = {
+ '',
+ tostring(#clients) .. ' active client(s): ',
+ }
+ vim.list_extend(buf_lines, active_section_header)
+ for _, client in pairs(clients) do
+ local client_info = make_client_info(client)
+ vim.list_extend(buf_lines, client_info)
+ end
+ local matching_config_header = {
+ '',
+ '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)
+ end
+ end
+ buf_lines = vim.lsp.util._trim(buf_lines, {})
+ vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, 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.fn.matchadd(
+ 'Error',
+ 'No filetypes defined, please define filetypes in setup().\\|' .. 'cmd not defined\\|' .. cmd_not_found_msg
+ )
+ vim.api.nvim_buf_set_keymap(bufnr, 'n', '<esc>', '<cmd>bd<CR>', { noremap = true })
+ vim.lsp.util.close_preview_autocmd({ 'BufHidden', 'BufLeave' }, win_id)
+end