Skip to content

Commit abd7aa3

Browse files
committed
(mini.extra) Update pickers.lsp to have symbol_query local option.
Details: - The main reason for adding this is to account for some LSP servers which don't properly support "workspace/symbol" request with empty string as query. It should return all symbols, but some servers (like `pyright`) return nothing. The new `local_opts.symbol_query` can be used together with `vim.fn.input('Symbol: ')` to get symbol interactively from user prior to sending the request and showing results in the picker.
1 parent f2b89ef commit abd7aa3

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

doc/mini-extra.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,8 @@ Parameters~
609609
Possible fields:
610610
- <scope> `(string)` - LSP method to use. One of the supported ones (see
611611
list above). Default: `nil` which means explicit scope is needed.
612+
- <symbol_query> `(string)` - query for |vim.lsp.buf.workspace_symbol()|.
613+
Default: empty string for all symbols (according to LSP specification).
612614
{opts} `(table|nil)` Options forwarded to |MiniPick.start()|.
613615

614616
Return~

lua/mini/extra.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,13 +1080,15 @@ end
10801080
--- Possible fields:
10811081
--- - <scope> `(string)` - LSP method to use. One of the supported ones (see
10821082
--- list above). Default: `nil` which means explicit scope is needed.
1083+
--- - <symbol_query> `(string)` - query for |vim.lsp.buf.workspace_symbol()|.
1084+
--- Default: empty string for all symbols (according to LSP specification).
10831085
---@param opts __extra_pickers_opts
10841086
---
10851087
---@return nil Nothing is returned.
10861088
MiniExtra.pickers.lsp = function(local_opts, opts)
10871089
if vim.fn.has('nvim-0.8') == 0 then H.error('`pickers.lsp` requires Neovim>=0.8.') end
10881090
local pick = H.validate_pick('lsp')
1089-
local_opts = vim.tbl_deep_extend('force', { scope = nil }, local_opts or {})
1091+
local_opts = vim.tbl_deep_extend('force', { scope = nil, symbol_query = '' }, local_opts or {})
10901092

10911093
if local_opts.scope == nil then H.error('`pickers.lsp` needs an explicit scope.') end
10921094
--stylua: ignore
@@ -1096,7 +1098,10 @@ MiniExtra.pickers.lsp = function(local_opts, opts)
10961098
local scope = H.pick_validate_scope(local_opts, allowed_scopes, 'lsp')
10971099

10981100
if scope == 'references' then return vim.lsp.buf[scope](nil, { on_list = H.lsp_make_on_list(scope, opts) }) end
1099-
if scope == 'workspace_symbol' then return vim.lsp.buf[scope]('', { on_list = H.lsp_make_on_list(scope, opts) }) end
1101+
if scope == 'workspace_symbol' then
1102+
local query = tostring(local_opts.symbol_query)
1103+
return vim.lsp.buf[scope](query, { on_list = H.lsp_make_on_list(scope, opts) })
1104+
end
11001105
vim.lsp.buf[scope]({ on_list = H.lsp_make_on_list(scope, opts) })
11011106
end
11021107

tests/dir-extra/mocks/lsp.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ vim.lsp.buf.type_definition = function(opts)
7979
opts.on_list(data)
8080
end
8181

82-
vim.lsp.buf.workspace_symbol = function(_, opts)
82+
vim.lsp.buf.workspace_symbol = function(query, opts)
8383
table.insert(_G.lsp_buf_calls, 'workspace_symbol')
84+
_G.workspace_symbol_query = query
8485
local data = {
8586
context = make_context('textDocument/workspaceSymbol'),
8687
items = {

tests/test_extra.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,7 @@ local validate_symbol_scope = function(scope)
24452445
pick_lsp({ scope = scope })
24462446
validate_picker_name('LSP (' .. scope .. ')')
24472447
eq(child.lua_get('_G.lsp_buf_calls'), { scope })
2448+
if scope == 'workspace_symbol' then eq(child.lua_get('_G.workspace_symbol_query'), '') end
24482449
child.expect_screenshot()
24492450

24502451
-- Should highlight some symbols
@@ -2518,6 +2519,14 @@ T['pickers']['lsp()']['works for `type_definition`'] = function() validate_locat
25182519

25192520
T['pickers']['lsp()']['works for `workspace_symbol`'] = function() validate_symbol_scope('workspace_symbol') end
25202521

2522+
T['pickers']['lsp()']['respects `local_opts.symbol_query`'] = function()
2523+
if child.fn.has('nvim-0.8') == 0 then return end
2524+
setup_lsp()
2525+
2526+
pick_lsp({ scope = 'workspace_symbol', symbol_query = 'aaa' })
2527+
eq(child.lua_get('_G.workspace_symbol_query'), 'aaa')
2528+
end
2529+
25212530
T['pickers']['lsp()']['throws error on Neovim<0.8'] = function()
25222531
if child.fn.has('nvim-0.8') == 1 then return end
25232532
expect.error(function() child.lua([[MiniExtra.pickers.lsp({ scope = 'references' })]]) end, '`pickers%.lsp`.*0%.8')

0 commit comments

Comments
 (0)