Skip to content

Commit 253d3aa

Browse files
added a new DynamicFinder (which can be used with rust_analyzer) (#705)
* started tree finder * made tree more ergonmic * deleted unneeded comments * added stack root and node * added preprocessing * using staticfinder instead of separate finder, custom entry maker * added selections and remember * removed unused stuff * fixed warnings * fixed remember and selections pop * started branch * added go function * changed up test * removed root parameter from go function * changed back to not do_close * removed node and leaf classes * removed stack class instead for table.insert and table.remove * fixed warning * started branch * added better preprocessor and tree class * started some tests * finished making tests pass * cleaned up * fixed make entry and updated example * started * added some stuff * deleted uneeded stuff * added cancelable * changed workspace requester * use better cancellation mechanism * removed accidental stuff * removed useless print * delete more useless stuff * rename to dynamic * added request cancellation * CHECK IF NIL * removed unused * added trash global variable
1 parent 5bd6f5c commit 253d3aa

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

.luacheckrc

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ignore = {
1313
}
1414

1515
globals = {
16+
"_",
1617
"TelescopeGlobalState",
1718
"TelescopeCachedUppers",
1819
"TelescopeCachedTails",

lua/telescope/builtin/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ builtin.git_branches = require('telescope.builtin.git').branches
4040
builtin.git_status = require('telescope.builtin.git').status
4141

4242
builtin.builtin = require('telescope.builtin.internal').builtin
43+
4344
builtin.planets = require('telescope.builtin.internal').planets
4445
builtin.symbols = require('telescope.builtin.internal').symbols
4546
builtin.commands = require('telescope.builtin.internal').commands
@@ -70,5 +71,6 @@ builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics
7071
builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics
7172
builtin.lsp_range_code_actions = require('telescope.builtin.lsp').range_code_actions
7273
builtin.lsp_workspace_symbols = require('telescope.builtin.lsp').workspace_symbols
74+
builtin.lsp_dynamic_workspace_symbols = require('telescope.builtin.lsp').dynamic_workspace_symbols
7375

7476
return builtin

lua/telescope/builtin/lsp.lua

+33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ local finders = require('telescope.finders')
44
local make_entry = require('telescope.make_entry')
55
local pickers = require('telescope.pickers')
66
local utils = require('telescope.utils')
7+
local a = require('plenary.async_lib')
8+
local async, await = a.async, a.await
9+
local channel = a.util.channel
710

811
local conf = require('telescope.config').values
912

@@ -218,6 +221,36 @@ lsp.workspace_symbols = function(opts)
218221
}):find()
219222
end
220223

224+
local function get_workspace_symbols_requester(bufnr)
225+
local cancel = function() end
226+
227+
return async(function(prompt)
228+
local tx, rx = channel.oneshot()
229+
cancel()
230+
_, cancel = vim.lsp.buf_request(bufnr, "workspace/symbol", {query = prompt}, tx)
231+
232+
local err, _, results_lsp = await(rx())
233+
assert(not err, err)
234+
235+
local locations = vim.lsp.util.symbols_to_items(results_lsp or {}, bufnr) or {}
236+
return locations
237+
end)
238+
end
239+
240+
lsp.dynamic_workspace_symbols = function(opts)
241+
local curr_bufnr = vim.api.nvim_get_current_buf()
242+
243+
pickers.new(opts, {
244+
prompt_title = 'LSP Dynamic Workspace Symbols',
245+
finder = finders.new_dynamic {
246+
entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts),
247+
fn = get_workspace_symbols_requester(curr_bufnr),
248+
},
249+
previewer = conf.qflist_previewer(opts),
250+
sorter = conf.generic_sorter()
251+
}):find()
252+
end
253+
221254
lsp.diagnostics = function(opts)
222255
local locations = utils.diagnostics_to_tbl(opts)
223256

lua/telescope/finders.lua

+35-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local Job = require('plenary.job')
22

33
local make_entry = require('telescope.make_entry')
44
local log = require('telescope.log')
5+
local a = require('plenary.async_lib')
6+
local await = a.await
57

68
local async_static_finder = require('telescope.finders.async_static_finder')
79
local async_oneshot_finder = require('telescope.finders.async_oneshot_finder')
@@ -20,7 +22,6 @@ local _callable_obj = function()
2022
return obj
2123
end
2224

23-
2425
--[[ =============================================================
2526
2627
JobFinder
@@ -108,6 +109,35 @@ function JobFinder:_find(prompt, process_result, process_complete)
108109
self.job:start()
109110
end
110111

112+
local DynamicFinder = _callable_obj()
113+
114+
function DynamicFinder:new(opts)
115+
opts = opts or {}
116+
117+
assert(not opts.results, "`results` should be used with finder.new_table")
118+
assert(not opts.static, "`static` should be used with finder.new_oneshot_job")
119+
120+
local obj = setmetatable({
121+
curr_buf = opts.curr_buf,
122+
fn = opts.fn,
123+
entry_maker = opts.entry_maker or make_entry.from_string,
124+
}, self)
125+
126+
return obj
127+
end
128+
129+
function DynamicFinder:_find(prompt, process_result, process_complete)
130+
a.scope(function()
131+
local results = await(self.fn(prompt))
132+
133+
for _, result in ipairs(results) do
134+
if process_result(self.entry_maker(result)) then return end
135+
end
136+
137+
process_complete()
138+
end)
139+
end
140+
111141
--- Return a new Finder
112142
--
113143
-- Use at your own risk.
@@ -185,4 +215,8 @@ finders.new_table = function(t)
185215
return async_static_finder(t)
186216
end
187217

218+
finders.new_dynamic = function(t)
219+
return DynamicFinder:new(t)
220+
end
221+
188222
return finders

0 commit comments

Comments
 (0)