Skip to content

Commit 03981bd

Browse files
authored
feat: improve LspInfo (neovim#2081)
* feat: improve LspInfo * feat: update README for highlight * fix: wrong typo * fix: ci failed * fix: remove unnecessary block * fix: stylua format * fix: set default border to none * fix: update the doc * fix: define names in if statement * fix: use default_options to set border * fix: use available servers list * fix: fixup * fix: format by stylua * fix: use bufdelete event * fix: format * fix: add tips * fix: stylua format * fix: use wrap * fix: add 122 to luacheck ignore * fix: reset the default options * fix: merge master * fix: remove unecessary code * feat: update the highlight group * feat: update doc for highlight * fix: remove highlig from README * fix: remae highlight group in doc
1 parent 520c609 commit 03981bd

File tree

5 files changed

+94
-28
lines changed

5 files changed

+94
-28
lines changed

.luacheckrc

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
cache = true
55

66
ignore = {
7+
"122", -- Setting a read-only field of a global variable.
78
"212", -- Unused argument, In the case of callback function, _arg_name is easier to understand than _, so this option is set to off.
89
"631", -- max_line_length, vscode pkg URL is too long
910
}

doc/lspconfig.txt

+12
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,18 @@ options, the `lspconfig` wiki lists community created plugins that build upon
631631
the built-in client to provide functionality tailored to specific language
632632
servers.
633633

634+
==============================================================================
635+
Highlights *lspconfig-highlight*
636+
637+
LspInfoTitle Client name
638+
LspInfoList Server name list
639+
LspInfoFiletype `filetypes` area
640+
LspInfoTip Tip
641+
LspInfoBorder Window border
642+
To set the border use: >
643+
require('lspconfig.ui.windows').default_options.border = 'single'
644+
< Accepts the same values as the `border` option to |nvim_open_win()|
645+
634646
==============================================================================
635647

636648
vim:tw=78:ts=8:ft=help:norl:

lua/lspconfig/ui/lspinfo.lua

+41-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local api = vim.api
12
local configs = require 'lspconfig.configs'
23
local windows = require 'lspconfig.ui.windows'
34
local util = require 'lspconfig.util'
@@ -52,7 +53,7 @@ local function make_config_info(config, bufnr)
5253
config_info.cmd_is_executable = 'NA'
5354
end
5455

55-
local buffer_dir = vim.api.nvim_buf_call(bufnr, function()
56+
local buffer_dir = api.nvim_buf_call(bufnr, function()
5657
return vim.fn.expand '%:p:h'
5758
end)
5859
local root_dir = config.get_root_dir(buffer_dir)
@@ -150,7 +151,12 @@ return function()
150151
local buf_clients = vim.lsp.buf_get_clients()
151152
local clients = vim.lsp.get_active_clients()
152153
local buffer_filetype = vim.bo.filetype
153-
local original_bufnr = vim.api.nvim_get_current_buf()
154+
local original_bufnr = api.nvim_get_current_buf()
155+
156+
windows.default_options.wrap = true
157+
windows.default_options.breakindent = true
158+
windows.default_options.breakindentopt = 'shift:25'
159+
windows.default_options.showbreak = 'NONE'
154160

155161
local win_info = windows.percentage_range_window(0.8, 0.7)
156162
local bufnr, win_id = win_info.bufnr, win_info.win_id
@@ -169,6 +175,9 @@ return function()
169175
end
170176
end
171177

178+
-- insert the tips at the top of window
179+
table.insert(buf_lines, 'Use [q] or [Esc] to quit the window')
180+
172181
local header = {
173182
'',
174183
'Language client log: ' .. (vim.lsp.get_log_path()),
@@ -216,22 +225,39 @@ return function()
216225

217226
local matching_config_header = {
218227
'',
219-
'Configured servers list: ' .. table.concat(vim.tbl_keys(configs), ', '),
228+
'Configured servers list: ' .. table.concat(util.available_servers(), ', '),
220229
}
230+
221231
vim.list_extend(buf_lines, matching_config_header)
222232

223233
local fmt_buf_lines = indent_lines(buf_lines, ' ')
224234

225235
fmt_buf_lines = vim.lsp.util._trim(fmt_buf_lines, {})
226236

227-
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, fmt_buf_lines)
228-
vim.api.nvim_buf_set_option(bufnr, 'modifiable', false)
229-
vim.api.nvim_buf_set_option(bufnr, 'filetype', 'lspinfo')
237+
api.nvim_buf_set_lines(bufnr, 0, -1, true, fmt_buf_lines)
238+
api.nvim_buf_set_option(bufnr, 'modifiable', false)
239+
api.nvim_buf_set_option(bufnr, 'filetype', 'lspinfo')
230240

231-
vim.api.nvim_buf_set_keymap(bufnr, 'n', '<esc>', '<cmd>bd<CR>', { noremap = true })
232-
vim.api.nvim_command(
233-
string.format('autocmd BufHidden,BufLeave <buffer> ++once lua pcall(vim.api.nvim_win_close, %d, true)', win_id)
234-
)
241+
local augroup = api.nvim_create_augroup('lspinfo', { clear = false })
242+
243+
local function close()
244+
api.nvim_clear_autocmds { group = augroup, buffer = bufnr }
245+
if api.nvim_buf_is_valid(bufnr) then
246+
api.nvim_buf_delete(bufnr, { force = true })
247+
end
248+
if api.nvim_win_is_valid(win_id) then
249+
api.nvim_win_close(win_id, true)
250+
end
251+
end
252+
253+
vim.keymap.set('n', '<ESC>', close, { buffer = bufnr, nowait = true })
254+
vim.keymap.set('n', 'q', close, { buffer = bufnr, nowait = true })
255+
api.nvim_create_autocmd({ 'BufDelete', 'BufLeave', 'BufHidden' }, {
256+
once = true,
257+
buffer = bufnr,
258+
callback = close,
259+
group = augroup,
260+
})
235261

236262
vim.fn.matchadd(
237263
'Error',
@@ -246,12 +272,14 @@ return function()
246272
vim.cmd 'let m=matchadd("string", "true")'
247273
vim.cmd 'let m=matchadd("error", "false")'
248274
for _, config in pairs(configs) do
249-
vim.fn.matchadd('Title', '\\%(Client\\|Config\\):.*\\zs' .. config.name .. '\\ze')
250-
vim.fn.matchadd('Visual', 'list:.*\\zs' .. config.name .. '\\ze')
275+
vim.fn.matchadd('LspInfoTitle', '\\%(Client\\|Config\\):.*\\zs' .. config.name .. '\\ze')
276+
vim.fn.matchadd('LspInfoList', 'list:.*\\zs' .. config.name .. '\\ze')
251277
if config.filetypes then
252278
for _, ft in pairs(config.filetypes) do
253-
vim.fn.matchadd('Type', '\\%(filetypes\\|filetype\\):.*\\zs' .. ft .. '\\ze')
279+
vim.fn.matchadd('LspInfoFiletype', '\\%(filetypes\\|filetype\\):.*\\zs' .. ft .. '\\ze')
254280
end
255281
end
256282
end
283+
284+
api.nvim_buf_add_highlight(bufnr, 0, 'LspInfoTip', 0, 0, -1)
257285
end

lua/lspconfig/ui/windows.lua

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
-- The following is extracted and modified from plenary.vnim by
22
-- TJ Devries. It is not a stable API, and is expected to change
33
--
4+
local api = vim.api
5+
46
local function apply_defaults(original, defaults)
57
if original == nil then
68
original = {}
@@ -19,13 +21,10 @@ end
1921

2022
local win_float = {}
2123

22-
win_float.default_options = {
23-
winblend = 15,
24-
percentage = 0.9,
25-
}
24+
win_float.default_options = {}
2625

2726
function win_float.default_opts(options)
28-
options = apply_defaults(options, win_float.default_options)
27+
options = apply_defaults(options, { percentage = 0.9 })
2928

3029
local width = math.floor(vim.o.columns * options.percentage)
3130
local height = math.floor(vim.o.lines * options.percentage)
@@ -52,6 +51,8 @@ function win_float.default_opts(options)
5251
},
5352
}
5453

54+
opts.border = options.border and options.border
55+
5556
return opts
5657
end
5758

@@ -85,6 +86,7 @@ function win_float.percentage_range_window(col_range, row_range, options)
8586

8687
win_opts.height = math.ceil(vim.o.lines * height_percentage)
8788
win_opts.row = math.ceil(vim.o.lines * row_start_percentage)
89+
win_opts.border = options.border or 'none'
8890

8991
local width_percentage, col_start_percentage
9092
if type(col_range) == 'number' then
@@ -102,11 +104,21 @@ function win_float.percentage_range_window(col_range, row_range, options)
102104
win_opts.col = math.floor(vim.o.columns * col_start_percentage)
103105
win_opts.width = math.floor(vim.o.columns * width_percentage)
104106

105-
local bufnr = options.bufnr or vim.api.nvim_create_buf(false, true)
106-
local win_id = vim.api.nvim_open_win(bufnr, true, win_opts)
107-
vim.api.nvim_win_set_buf(win_id, bufnr)
107+
local bufnr = options.bufnr or api.nvim_create_buf(false, true)
108+
local win_id = api.nvim_open_win(bufnr, true, win_opts)
109+
api.nvim_win_set_option(win_id, 'winhl', 'FloatBorder:LspInfoBorder')
110+
111+
for k, v in pairs(win_float.default_options) do
112+
if k ~= 'border' then
113+
vim.opt_local[k] = v
114+
end
115+
end
116+
117+
api.nvim_win_set_buf(win_id, bufnr)
108118

109-
vim.cmd 'setlocal nocursorcolumn ts=2 sw=2'
119+
api.nvim_win_set_option(win_id, 'cursorcolumn', false)
120+
api.nvim_buf_set_option(bufnr, 'tabstop', 2)
121+
api.nvim_buf_set_option(bufnr, 'shiftwidth', 2)
110122

111123
return {
112124
bufnr = bufnr,

plugin/lspconfig.lua

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local api = vim.api
2+
13
if vim.g.lspconfig ~= nil then
24
return
35
end
@@ -22,7 +24,7 @@ end
2224
local lsp_complete_configured_servers = function(arg)
2325
return completion_sort(vim.tbl_filter(function(s)
2426
return s:sub(1, #arg) == arg
25-
end, vim.tbl_keys(require 'lspconfig.configs')))
27+
end, require('lspconfig.util').available_servers()))
2628
end
2729

2830
local lsp_get_active_client_ids = function(arg)
@@ -46,15 +48,25 @@ local get_clients_from_cmd_args = function(arg)
4648
return vim.tbl_values(result)
4749
end
4850

51+
for group, hi in pairs {
52+
LspInfoBorder = { link = 'Label', default = true },
53+
LspInfoList = { link = 'Function', default = true },
54+
LspInfoTip = { link = 'Comment', default = true },
55+
LspInfoTitle = { link = 'Title', default = true },
56+
LspInfoFiletype = { link = 'Type', default = true },
57+
} do
58+
api.nvim_set_hl(0, group, hi)
59+
end
60+
4961
-- Called from plugin/lspconfig.vim because it requires knowing that the last
5062
-- script in scriptnames to be executed is lspconfig.
51-
vim.api.nvim_create_user_command('LspInfo', function()
63+
api.nvim_create_user_command('LspInfo', function()
5264
require 'lspconfig.ui.lspinfo'()
5365
end, {
5466
desc = 'Displays attached, active, and configured language servers',
5567
})
5668

57-
vim.api.nvim_create_user_command('LspStart', function(info)
69+
api.nvim_create_user_command('LspStart', function(info)
5870
local server_name = string.len(info.args) > 0 and info.args or nil
5971
if server_name then
6072
local config = require('lspconfig.configs')[server_name]
@@ -73,7 +85,8 @@ end, {
7385
nargs = '?',
7486
complete = lsp_complete_configured_servers,
7587
})
76-
vim.api.nvim_create_user_command('LspRestart', function(info)
88+
89+
api.nvim_create_user_command('LspRestart', function(info)
7790
for _, client in ipairs(get_clients_from_cmd_args(info.args)) do
7891
client.stop()
7992
vim.defer_fn(function()
@@ -86,7 +99,7 @@ end, {
8699
complete = lsp_get_active_client_ids,
87100
})
88101

89-
vim.api.nvim_create_user_command('LspStop', function(info)
102+
api.nvim_create_user_command('LspStop', function(info)
90103
local current_buf = vim.api.nvim_get_current_buf()
91104
for _, client in ipairs(get_clients_from_cmd_args(info.args)) do
92105
local filetypes = client.config.filetypes
@@ -100,7 +113,7 @@ end, {
100113
complete = lsp_get_active_client_ids,
101114
})
102115

103-
vim.api.nvim_create_user_command('LspLog', function()
116+
api.nvim_create_user_command('LspLog', function()
104117
vim.cmd(string.format('tabnew %s', vim.lsp.get_log_path()))
105118
end, {
106119
desc = 'Opens the Nvim LSP client log.',

0 commit comments

Comments
 (0)