Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a new DynamicFinder (which can be used with rust_analyzer) #705

Merged
merged 44 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ab8a88e
started tree finder
oberblastmeister Dec 14, 2020
e34339b
made tree more ergonmic
oberblastmeister Dec 14, 2020
0d1330b
deleted unneeded comments
oberblastmeister Dec 14, 2020
1ef3e62
Merge https://github.com/nvim-telescope/telescope.nvim
oberblastmeister Dec 16, 2020
40c6fd4
added stack root and node
oberblastmeister Dec 17, 2020
9c37cd3
added preprocessing
oberblastmeister Dec 17, 2020
b227eed
using staticfinder instead of separate finder, custom entry maker
oberblastmeister Dec 17, 2020
57f1736
added selections and remember
oberblastmeister Dec 17, 2020
5441dc8
removed unused stuff
oberblastmeister Dec 17, 2020
415d19e
fixed warnings
oberblastmeister Dec 17, 2020
7f52cf7
fixed remember and selections pop
oberblastmeister Dec 17, 2020
46c1c16
started branch
oberblastmeister Dec 17, 2020
f8106c5
added go function
oberblastmeister Dec 17, 2020
ff98a6b
changed up test
oberblastmeister Dec 18, 2020
afe1632
Merge branch 'master' of https://github.com/nvim-telescope/telescope.…
oberblastmeister Dec 18, 2020
5229f35
removed root parameter from go function
oberblastmeister Dec 18, 2020
12bed53
changed back to not do_close
oberblastmeister Dec 18, 2020
5d5c73e
removed node and leaf classes
oberblastmeister Dec 18, 2020
1c049f1
removed stack class instead for table.insert and table.remove
oberblastmeister Dec 18, 2020
bc359b5
fixed warning
oberblastmeister Dec 18, 2020
57f9bb7
started branch
oberblastmeister Dec 19, 2020
e741585
added better preprocessor and tree class
oberblastmeister Dec 19, 2020
7601708
started some tests
oberblastmeister Dec 19, 2020
47156de
finished making tests pass
oberblastmeister Dec 19, 2020
5c8c6af
cleaned up
oberblastmeister Dec 19, 2020
40ab3f9
fixed make entry and updated example
oberblastmeister Dec 19, 2020
108c72f
Merge branch 'master' of https://github.com/nvim-telescope/telescope.…
oberblastmeister Dec 19, 2020
a793876
Merge branch 'master' of https://github.com/nvim-telescope/telescope.…
oberblastmeister Jan 1, 2021
31d724b
Merge branch 'master' of https://github.com/nvim-telescope/telescope.…
oberblastmeister Jan 4, 2021
034b54d
started
oberblastmeister Apr 13, 2021
a90f721
added some stuff
oberblastmeister Apr 13, 2021
e157ee7
deleted uneeded stuff
oberblastmeister Mar 30, 2021
0775eba
added cancelable
oberblastmeister Apr 13, 2021
4ba827a
changed workspace requester
oberblastmeister Apr 13, 2021
50fb0b7
use better cancellation mechanism
oberblastmeister Apr 13, 2021
ef227a0
Merge branch 'master' into live_finder
oberblastmeister Apr 13, 2021
261d38a
removed accidental stuff
oberblastmeister Apr 13, 2021
b8f67e0
removed useless print
oberblastmeister Apr 13, 2021
c355bb8
delete more useless stuff
oberblastmeister Apr 13, 2021
8312c90
rename to dynamic
oberblastmeister Apr 13, 2021
8bc676d
added request cancellation
oberblastmeister Apr 13, 2021
e935fbf
CHECK IF NIL
oberblastmeister Apr 13, 2021
59ade72
removed unused
oberblastmeister Apr 13, 2021
a453a1d
added trash global variable
oberblastmeister Apr 13, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
32 changes: 32 additions & 0 deletions lua/telescope/builtin/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -218,6 +221,35 @@ lsp.workspace_symbols = function(opts)
}):find()
end

local function get_workspace_symbols_requester(bufnr)
local cancel = function() end
oberblastmeister marked this conversation as resolved.
Show resolved Hide resolved

return async(function(prompt)
local tx, rx = channel.oneshot()
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, 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_live {
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)

Expand Down
36 changes: 35 additions & 1 deletion lua/telescope/finders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, async = a.await, a.async

local async_static_finder = require('telescope.finders.async_static_finder')
local async_oneshot_finder = require('telescope.finders.async_oneshot_finder')
Expand All @@ -20,7 +22,6 @@ local _callable_obj = function()
return obj
end


--[[ =============================================================

JobFinder
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -185,4 +215,8 @@ finders.new_table = function(t)
return async_static_finder(t)
end

finders.new_live = function(t)
oberblastmeister marked this conversation as resolved.
Show resolved Hide resolved
return DynamicFinder:new(t)
end

return finders
11 changes: 11 additions & 0 deletions lua/telescope/make_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ do
end
end

function make_entry.gen_from_node(_)
return function(node)
local display = node.leaf or node.branch_name
node.display = display
node.ordinal = display
node.is_leaf = node.leaf and true or false

return node
end
end

do
local lookup_keys = {
ordinal = 1,
Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ function Picker:find()
local tx, rx = channel.mpsc()
self.__on_lines = tx.send

local status = {}
local main_loop = async(function()
while true do
await(async_lib.scheduler())
Expand Down Expand Up @@ -437,6 +438,8 @@ function Picker:find()

self.closed = true

status.should_stop = true

-- TODO: Should we actually do this?
collectgarbage(); collectgarbage()
end,
Expand Down
118 changes: 118 additions & 0 deletions lua/tests/automated/tree_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
local process = require('telescope.builtin.menu').process
local process_root = require('telescope.builtin.menu').process_root

local eq = function(a, b)
assert.are.same(a, b)
end

describe('preprocess function', function()
it('should preprocess simple', function()
local tree = {
{
"top level leaf",
"another top level leaf",
},
}

local expected = {
{leaf = "top level leaf", conf = {}},
{leaf = "another top level leaf", conf = {}},
}

local res = process(tree)
eq(expected, res)
end)

it('should preprocess with configuration in the root', function()
local tree = {
{
"top level leaf",
"another top level leaf",
},
title = 'test menu',
callback = print,
}

local expected = {
branch_name = "root",
branches = {
{leaf = "top level leaf", conf = {}},
{leaf = "another top level leaf", conf = {}},
},
conf = {
title = 'test menu',
callback = print,
}
}

local res = process_root(tree)

eq(expected, res)
end)

it('should preprocess with two levels', function()
local tree = {
{
"top level leaf",
"another top level leaf",
["a node"] = {
{
"second level leaf",
"another second level leaf",
}
}
},
}

local expected = {
{leaf = "top level leaf", conf = {}},
{leaf = "another top level leaf", conf = {}},
{
branch_name = "a node",
branches = {
{leaf = "second level leaf", conf = {}},
{leaf = "another second level leaf", conf = {}},
{leaf = "..", conf = {}},
},
conf = {}
},
}

local res = process(tree)

eq(expected, res)
end)

it('should preprocess with two levels and conf', function()
local tree = {
{
"top level leaf",
"another top level leaf",
["a node"] = {
{
"second level leaf",
"another second level leaf",
},
}
},
}

local expected = {
{leaf = "top level leaf", conf = {}},
{leaf = "another top level leaf", conf = {}},
{
branch_name = "a node",
branches = {
{leaf = "second level leaf", conf = {}},
{leaf = "another second level leaf", conf = {}},
{leaf = "..", conf = {}},
},
conf = {}
},
}

local res = process(tree)

eq(expected, res)
end)
end)