From 253d3aaa6b43eac6b11341b325e34d37dc459af3 Mon Sep 17 00:00:00 2001 From: oberblastmeister <61095988+oberblastmeister@users.noreply.github.com> Date: Tue, 13 Apr 2021 14:39:14 -0400 Subject: [PATCH] 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 --- .luacheckrc | 1 + lua/telescope/builtin/init.lua | 2 ++ lua/telescope/builtin/lsp.lua | 33 +++++++++++++++++++++++++++++++ lua/telescope/finders.lua | 36 +++++++++++++++++++++++++++++++++- 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/.luacheckrc b/.luacheckrc index 49c9bcf..98c76bd 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -13,6 +13,7 @@ ignore = { } globals = { + "_", "TelescopeGlobalState", "TelescopeCachedUppers", "TelescopeCachedTails", diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 2b3c1ca..b9cc99f 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -40,6 +40,7 @@ builtin.git_branches = require('telescope.builtin.git').branches builtin.git_status = require('telescope.builtin.git').status builtin.builtin = require('telescope.builtin.internal').builtin + builtin.planets = require('telescope.builtin.internal').planets builtin.symbols = require('telescope.builtin.internal').symbols builtin.commands = require('telescope.builtin.internal').commands @@ -70,5 +71,6 @@ builtin.lsp_document_diagnostics = require('telescope.builtin.lsp').diagnostics builtin.lsp_workspace_diagnostics = require('telescope.builtin.lsp').workspace_diagnostics builtin.lsp_range_code_actions = require('telescope.builtin.lsp').range_code_actions builtin.lsp_workspace_symbols = require('telescope.builtin.lsp').workspace_symbols +builtin.lsp_dynamic_workspace_symbols = require('telescope.builtin.lsp').dynamic_workspace_symbols return builtin diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 741eb36..b2ab981 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -4,6 +4,9 @@ local finders = require('telescope.finders') local make_entry = require('telescope.make_entry') local pickers = require('telescope.pickers') local utils = require('telescope.utils') +local a = require('plenary.async_lib') +local async, await = a.async, a.await +local channel = a.util.channel local conf = require('telescope.config').values @@ -218,6 +221,36 @@ lsp.workspace_symbols = function(opts) }):find() end +local function get_workspace_symbols_requester(bufnr) + local cancel = function() end + + return async(function(prompt) + local tx, rx = channel.oneshot() + cancel() + _, cancel = vim.lsp.buf_request(bufnr, "workspace/symbol", {query = prompt}, tx) + + local err, _, results_lsp = await(rx()) + assert(not err, err) + + local locations = vim.lsp.util.symbols_to_items(results_lsp or {}, bufnr) or {} + return locations + end) +end + +lsp.dynamic_workspace_symbols = function(opts) + local curr_bufnr = vim.api.nvim_get_current_buf() + + pickers.new(opts, { + prompt_title = 'LSP Dynamic Workspace Symbols', + finder = finders.new_dynamic { + entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), + fn = get_workspace_symbols_requester(curr_bufnr), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter() + }):find() +end + lsp.diagnostics = function(opts) local locations = utils.diagnostics_to_tbl(opts) diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index d2acd05..62c05a1 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -2,6 +2,8 @@ local Job = require('plenary.job') local make_entry = require('telescope.make_entry') local log = require('telescope.log') +local a = require('plenary.async_lib') +local await = a.await local async_static_finder = require('telescope.finders.async_static_finder') local async_oneshot_finder = require('telescope.finders.async_oneshot_finder') @@ -20,7 +22,6 @@ local _callable_obj = function() return obj end - --[[ ============================================================= JobFinder @@ -108,6 +109,35 @@ function JobFinder:_find(prompt, process_result, process_complete) self.job:start() end +local DynamicFinder = _callable_obj() + +function DynamicFinder:new(opts) + opts = opts or {} + + assert(not opts.results, "`results` should be used with finder.new_table") + assert(not opts.static, "`static` should be used with finder.new_oneshot_job") + + local obj = setmetatable({ + curr_buf = opts.curr_buf, + fn = opts.fn, + entry_maker = opts.entry_maker or make_entry.from_string, + }, self) + + return obj +end + +function DynamicFinder:_find(prompt, process_result, process_complete) + a.scope(function() + local results = await(self.fn(prompt)) + + for _, result in ipairs(results) do + if process_result(self.entry_maker(result)) then return end + end + + process_complete() + end) +end + --- Return a new Finder -- -- Use at your own risk. @@ -185,4 +215,8 @@ finders.new_table = function(t) return async_static_finder(t) end +finders.new_dynamic = function(t) + return DynamicFinder:new(t) +end + return finders