From ab8a88ef78bedc2868487e543d37d0605b1482df Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sun, 13 Dec 2020 20:02:44 -0500 Subject: [PATCH 01/38] started tree finder --- lua/telescope/builtin/init.lua | 2 + lua/telescope/builtin/internal.lua | 79 ++++++++++++++++++++++++++++++ lua/telescope/finders.lua | 36 ++++++++++++++ lua/telescope/make_entry.lua | 24 +++++++++ 4 files changed, 141 insertions(+) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 14e99a6810..07b6cc45b5 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -35,6 +35,8 @@ builtin.git_branches = require('telescope.builtin.git').branches builtin.git_status = require('telescope.builtin.git').status builtin.builtin = require('telescope.builtin.internal').builtin +builtin.menu = require('telescope.builtin.internal').menu +builtin.test_menu = require('telescope.builtin.internal').test_menu builtin.planets = require('telescope.builtin.internal').planets builtin.symbols = require('telescope.builtin.internal').symbols builtin.commands = require('telescope.builtin.internal').commands diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 00868bbd3e..d4223f5aa3 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -45,6 +45,85 @@ internal.builtin = function(opts) }):find() end +do + internal.menu = function(opts) + if opts.callback == nil then + vim.cmd [[echoerr 'There is no callback']] + return + end + + pickers.new(opts, { + prompt_title = opts.title or 'Menu', + finder = finders.new_tree { + results = opts.t, + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions._goto_file_selection:replace(function() + local entry = actions.get_selected_entry() + local value = entry.value + if value == "" then + -- it is a leaf + opts.callback(entry.key) + actions.close(prompt_bufnr) + elseif type(value) == "table" then + -- it is a node + actions.close(prompt_bufnr) + -- recurse + internal.menu{t = value, callback = opts.callback} + -- sometimes does not start insert for some reason + vim.api.nvim_input('i') + else + vim.cmd [[echoerr 'value must be leaf or table']] + end + end) + + return true + end, + }):find() + end +end + +internal.test_menu = function(opts) + internal.menu { + t = { + a_leaf = "", + another_leaf = "", + blah = "", + ["1 level deep node"] = { + leaf = "", + another_leaf = "", + inside_a_node = "", + }, + ["2 level deep node"] = { + leaf = "", + another_leaf = "", + inside_a_node = "", + node_inside_node = { + final_leaf = "" + }, + }, + }, + title = 'test menu', + callback = function(selection) + print("just testing the callback selection ", selection) + end + } +end + +-- function internal.test_menu() +-- internal.menu({ +-- hello = { +-- dude = "", +-- wow = "", +-- another = "", +-- }, +-- wothis = "", +-- interstin = "", +-- }) +-- end + + internal.planets = function(opts) local show_pluto = opts.show_pluto or false diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index f968ad3dee..72a5d86072 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -248,6 +248,38 @@ function StaticFinder:new(opts) return setmetatable({ results = results }, self) end +function StaticFinder:new_tree(opts) + assert(opts, "Options are required. See documentation for usage") + + local input_results + if vim.tbl_islist(opts) then + input_results = opts + else + input_results = opts.results + end + + local entry_maker = opts.entry_maker or make_entry.gen_from_tree() + + assert(input_results) + assert(input_results, "Results are required for static finder") + assert(type(input_results) == 'table', "self.results must be a table") + + local results = {} + local counter = 0 + for k, v in pairs(input_results) do + local entry = entry_maker(k, v) + + if entry then + -- entry.index = counter + table.insert(results, entry) + end + + counter = counter + 1 + end + + return setmetatable({ results = results }, self) +end + function StaticFinder:_find(_, process_result, process_complete) for _, v in ipairs(self.results) do process_result(v) @@ -331,4 +363,8 @@ finders.new_table = function(t) return StaticFinder:new(t) end +finders.new_tree = function(t) + return StaticFinder:new_tree(t) +end + return finders diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 0a12764e04..e83fc30b66 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -57,6 +57,30 @@ do end end +do + local lookup_keys = { + ordinal = 1, + value = 2, + display = 1, + key = 1, + } + + local mt_tree_entry = { + __index = function(t, k) + return rawget(t, rawget(lookup_keys, k)) + end + } + + function make_entry.gen_from_tree() + return function(key, value) + return setmetatable({ + key, + value, + }, mt_tree_entry) + end + end +end + do local lookup_keys = { ordinal = 1, From e34339bcca9a3ea51d31cd16c9607b77025cb6f0 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sun, 13 Dec 2020 20:43:50 -0500 Subject: [PATCH 02/38] made tree more ergonmic --- lua/telescope/builtin/internal.lua | 27 ++++++++++++++------------- lua/telescope/make_entry.lua | 18 ++++++++++++++---- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index d4223f5aa3..674458dcd8 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -62,9 +62,10 @@ do actions._goto_file_selection:replace(function() local entry = actions.get_selected_entry() local value = entry.value - if value == "" then + local key = entry.key + if value == nil then -- it is a leaf - opts.callback(entry.key) + opts.callback(key) actions.close(prompt_bufnr) elseif type(value) == "table" then -- it is a node @@ -87,26 +88,26 @@ end internal.test_menu = function(opts) internal.menu { t = { - a_leaf = "", - another_leaf = "", - blah = "", + "a_leaf", + "another_leaf", + "blah", ["1 level deep node"] = { - leaf = "", - another_leaf = "", - inside_a_node = "", + "leaf", + "another_leaf", + "inside_a_node", }, ["2 level deep node"] = { - leaf = "", - another_leaf = "", - inside_a_node = "", + "leaf", + "another_leaf", + "inside_a_node", node_inside_node = { - final_leaf = "" + "final_leaf", }, }, }, title = 'test menu', callback = function(selection) - print("just testing the callback selection ", selection) + print("test callback selection:", selection) end } end diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index e83fc30b66..6d45cbe75c 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -73,10 +73,20 @@ do function make_entry.gen_from_tree() return function(key, value) - return setmetatable({ - key, - value, - }, mt_tree_entry) + if type(key) == "number" then + -- it is a leaf + return setmetatable({ + value, + -- this means it is leaf + nil, + }, mt_tree_entry) + else + -- it is a node + return setmetatable({ + key, + value, + }, mt_tree_entry) + end end end end From 0d1330b4d508b742b7651d14d5b39ecc73220506 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sun, 13 Dec 2020 20:56:00 -0500 Subject: [PATCH 03/38] deleted unneeded comments --- lua/telescope/builtin/internal.lua | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 674458dcd8..4d1f2e8bc3 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -112,19 +112,6 @@ internal.test_menu = function(opts) } end --- function internal.test_menu() --- internal.menu({ --- hello = { --- dude = "", --- wow = "", --- another = "", --- }, --- wothis = "", --- interstin = "", --- }) --- end - - internal.planets = function(opts) local show_pluto = opts.show_pluto or false From 40c6fd47a08744e1227c3b6d799d33acb2f73a6e Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 10:04:50 -0500 Subject: [PATCH 04/38] added stack root and node --- lua/telescope/builtin/internal.lua | 72 +++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 4d1f2e8bc3..7ed2d82dbe 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -46,6 +46,38 @@ internal.builtin = function(opts) end do + local Stack = {} + Stack.__index = Stack + + function Stack.new(init) + init = init or {} + local self = setmetatable(init, Stack) + return self + end + + function Stack:push(...) + local args = vim.tbl_flatten {...} + for _, v in ipairs(args) do + table.insert(self, v) + end + end + + function Stack:not_empty() + return #self.t > 0 + end + + function Stack:pop() + table.remove(self) + end + + function Stack:last() + if self:not_empty() then + return self.t[#self.t] + end + end + + local selections = Stack.new() + internal.menu = function(opts) if opts.callback == nil then vim.cmd [[echoerr 'There is no callback']] @@ -60,22 +92,23 @@ do sorter = conf.generic_sorter(opts), attach_mappings = function(prompt_bufnr) actions._goto_file_selection:replace(function() + actions.close(prompt_bufnr) local entry = actions.get_selected_entry() local value = entry.value local key = entry.key if value == nil then -- it is a leaf - opts.callback(key) - actions.close(prompt_bufnr) + selections:push(key) + opts.callback(selections) elseif type(value) == "table" then -- it is a node - actions.close(prompt_bufnr) + selections:push(key) -- recurse - internal.menu{t = value, callback = opts.callback} + internal.menu {t = value, callback = opts.callback} -- sometimes does not start insert for some reason vim.api.nvim_input('i') else - vim.cmd [[echoerr 'value must be leaf or table']] + error "value must be leaf or table" end end) @@ -106,12 +139,37 @@ internal.test_menu = function(opts) }, }, title = 'test menu', - callback = function(selection) - print("test callback selection:", selection) + callback = function(selections) + print("test callback selection:", selections[#selections]) end } end +Node = {} +Node.__index = Node + +Node.new = function(opts) + local self = setmetatable({}, Node) + + self.t = opts.t + self.callback = opts.callback + self.title = opts.title + + table.insert(self.t, "..") + + return self +end + +Root = {} +Root.__index = Root + +Root.new = function(opts) + local self = setmetatable({}, Node) + self.t = opts.t + self.callback = opts.callback + self.title = opts.title +end + internal.planets = function(opts) local show_pluto = opts.show_pluto or false From 9c37cd3fcba7cac12193868cecfdb511eb589749 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 11:15:33 -0500 Subject: [PATCH 05/38] added preprocessing --- lua/telescope/builtin/init.lua | 6 +- lua/telescope/builtin/internal.lua | 58 ++++---- lua/telescope/builtin/menu.lua | 213 +++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+), 27 deletions(-) create mode 100644 lua/telescope/builtin/menu.lua diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 07b6cc45b5..4ef082b06a 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -35,8 +35,10 @@ builtin.git_branches = require('telescope.builtin.git').branches builtin.git_status = require('telescope.builtin.git').status builtin.builtin = require('telescope.builtin.internal').builtin -builtin.menu = require('telescope.builtin.internal').menu -builtin.test_menu = require('telescope.builtin.internal').test_menu +builtin.menu = require('telescope.builtin.menu').menu +builtin.test_menu = require('telescope.builtin.menu').test_menu +builtin.Node = require('telescope.builtin.menu').Node + builtin.planets = require('telescope.builtin.internal').planets builtin.symbols = require('telescope.builtin.internal').symbols builtin.commands = require('telescope.builtin.internal').commands diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 7ed2d82dbe..527073da6d 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -45,6 +45,35 @@ internal.builtin = function(opts) }):find() end +Node = {} +Node.__index = Node + +Node.new = function(opts) + local self = setmetatable({}, Node) + + self.t = opts.t + self.callback = opts.callback + self.title = opts.title or 'Menu' + + table.insert(self.t, "..") + + return self +end + +Node.new_root = function(opts) + local self = setmetatable({}, Node) + + self.t = opts.t + if opts.callback == nil then + error "Root node must have default callback" + else + self.callback = opts.callback + end + self.title = opts.title or 'Menu' + + return self +end + do local Stack = {} Stack.__index = Stack @@ -79,6 +108,10 @@ do local selections = Stack.new() internal.menu = function(opts) + if getmetatable(opts[1]) == internal.Node then + error "First value must be root" + end + if opts.callback == nil then vim.cmd [[echoerr 'There is no callback']] return @@ -145,31 +178,6 @@ internal.test_menu = function(opts) } end -Node = {} -Node.__index = Node - -Node.new = function(opts) - local self = setmetatable({}, Node) - - self.t = opts.t - self.callback = opts.callback - self.title = opts.title - - table.insert(self.t, "..") - - return self -end - -Root = {} -Root.__index = Root - -Root.new = function(opts) - local self = setmetatable({}, Node) - self.t = opts.t - self.callback = opts.callback - self.title = opts.title -end - internal.planets = function(opts) local show_pluto = opts.show_pluto or false diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua new file mode 100644 index 0000000000..e943fdedc9 --- /dev/null +++ b/lua/telescope/builtin/menu.lua @@ -0,0 +1,213 @@ +local api = vim.api +local actions = require('telescope.actions') +local finders = require('telescope.finders') +local make_entry = require('telescope.make_entry') +local path = require('telescope.path') +local pickers = require('telescope.pickers') +local previewers = require('telescope.previewers') +local sorters = require('telescope.sorters') +local utils = require('telescope.utils') + +local conf = require('telescope.config').values + +local filter = vim.tbl_filter + +local menu = {} + +Node = {} +Node.__index = Node + +Node.new = function(opts) + local self = setmetatable({}, Node) + + self.t = opts[1] + self.callback = opts.callback + self.title = opts.title or 'Menu' + + table.insert(self.t, "..") + + return self +end + +Node.new_root = function(opts) + dump(opts) + local self = setmetatable({}, Node) + + self.t = opts[1] + if opts.callback == nil then + error "Root node must have default callback" + else + self.callback = opts.callback + end + self.title = opts.title or 'Menu' + + return self +end + +menu.Node = Node + +local function preprocess(tree, root) + local results = {} + + for k, v in pairs(tree) do + if type(v) == "string" then -- leaf + table.insert(results, { leaf = v, root = root }) -- and have each element have the table. So you also have it when we hit enter + elseif type(k) == "string" then + table.insert(results, { branch = k, root = root }) -- when selecting you can then just check if branch is not nil. if thats the case the restart menu + else + -- we should never get here. Am i correct? + end + end + + table.insert(results, { branch = '..', root = root }) + + return results +end + +do + local Stack = {} + Stack.__index = Stack + + function Stack.new(init) + init = init or {} + local self = setmetatable(init, Stack) + return self + end + + function Stack:push(...) + local args = vim.tbl_flatten {...} + for _, v in ipairs(args) do + table.insert(self, v) + end + end + + function Stack:is_empty() + return (#(self.t or {})) == 0 + end + + function Stack:pop() + table.remove(self) + end + + local selections = Stack.new() + local root + + -- cleanup the state + local function cleanup() + root = nil + selections = Stack.new() + end + + menu.open = function(tree, opts) + opts = opts or {} + local node = tree + dump(node) + + if root == nil then + root = node + end + + pickers.new(opts, { + prompt_title = node.title, + finder = finders.new_tree { + results = node.t, + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions._goto_file_selection:replace(function() + actions.close(prompt_bufnr) + + local entry = actions.get_selected_entry() + local value = entry.value + local key = entry.key + + if value == nil then + -- it is a leaf + if key == ".." then + local last = selections:pop() + if selections:is_empty() then + menu.open(root) + else + menu.open(root[last]) + end + api.nvim_input('i') + return + end + + selections:push(key) + local callback = node.callback or root.callback + callback(selections) + + cleanup() + + elseif type(value) == "table" then + -- it is a node + selections:push(key) + -- recurse + menu.open(value, opts) + -- sometimes does not start insert for some reason + vim.api.nvim_input('i') + else + error "value must be leaf or table" + end + end) + + return true + end, + }):find() + end +end + +menu.test = function(opts) + menu.open(Node.new_root { + { + "a leaf", + "another_leaf", + "blah", + another_node = Node.new { + { + "inner", + "inner2", + } + } + }, + callback = function(selections) + dump(selections) + end, + title = "testing", + }, opts) + -- menu.menu { + -- n = Node.new_root { + -- t = { + -- "a_leaf", + -- "another_leaf", + -- "blah", + -- ["1 level deep node"] = Node.new_root { + -- t = { + -- "leaf", + -- "another_leaf", + -- "inside_a_node", + -- } + -- }, + -- ["2 level deep node"] = { + -- t = { + -- "leaf", + -- "another_leaf", + -- "inside_a_node", + -- node_inside_node = Node.new_root { + -- t = { + -- "final_leaf", + -- } + -- }, + -- } + -- }, + -- }, + -- }, + -- title = 'test menu', + -- callback = function(selections) + -- print("test callback selection:", selections[#selections]) + -- end + -- } +end + +return menu From b227eed8ecad7eea0a58c056515cff7232120509 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 12:13:44 -0500 Subject: [PATCH 06/38] using staticfinder instead of separate finder, custom entry maker --- lua/telescope/builtin/init.lua | 2 +- lua/telescope/builtin/internal.lua | 133 ----------------------------- lua/telescope/builtin/menu.lua | 61 +++++++------ lua/telescope/finders.lua | 39 ++------- lua/telescope/make_entry.lua | 16 ++++ 5 files changed, 58 insertions(+), 193 deletions(-) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 4ef082b06a..1b8f6d2a56 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -36,7 +36,7 @@ builtin.git_status = require('telescope.builtin.git').status builtin.builtin = require('telescope.builtin.internal').builtin builtin.menu = require('telescope.builtin.menu').menu -builtin.test_menu = require('telescope.builtin.menu').test_menu +builtin.test_menu = require('telescope.builtin.menu').test builtin.Node = require('telescope.builtin.menu').Node builtin.planets = require('telescope.builtin.internal').planets diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 527073da6d..00868bbd3e 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -45,139 +45,6 @@ internal.builtin = function(opts) }):find() end -Node = {} -Node.__index = Node - -Node.new = function(opts) - local self = setmetatable({}, Node) - - self.t = opts.t - self.callback = opts.callback - self.title = opts.title or 'Menu' - - table.insert(self.t, "..") - - return self -end - -Node.new_root = function(opts) - local self = setmetatable({}, Node) - - self.t = opts.t - if opts.callback == nil then - error "Root node must have default callback" - else - self.callback = opts.callback - end - self.title = opts.title or 'Menu' - - return self -end - -do - local Stack = {} - Stack.__index = Stack - - function Stack.new(init) - init = init or {} - local self = setmetatable(init, Stack) - return self - end - - function Stack:push(...) - local args = vim.tbl_flatten {...} - for _, v in ipairs(args) do - table.insert(self, v) - end - end - - function Stack:not_empty() - return #self.t > 0 - end - - function Stack:pop() - table.remove(self) - end - - function Stack:last() - if self:not_empty() then - return self.t[#self.t] - end - end - - local selections = Stack.new() - - internal.menu = function(opts) - if getmetatable(opts[1]) == internal.Node then - error "First value must be root" - end - - if opts.callback == nil then - vim.cmd [[echoerr 'There is no callback']] - return - end - - pickers.new(opts, { - prompt_title = opts.title or 'Menu', - finder = finders.new_tree { - results = opts.t, - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions._goto_file_selection:replace(function() - actions.close(prompt_bufnr) - local entry = actions.get_selected_entry() - local value = entry.value - local key = entry.key - if value == nil then - -- it is a leaf - selections:push(key) - opts.callback(selections) - elseif type(value) == "table" then - -- it is a node - selections:push(key) - -- recurse - internal.menu {t = value, callback = opts.callback} - -- sometimes does not start insert for some reason - vim.api.nvim_input('i') - else - error "value must be leaf or table" - end - end) - - return true - end, - }):find() - end -end - -internal.test_menu = function(opts) - internal.menu { - t = { - "a_leaf", - "another_leaf", - "blah", - ["1 level deep node"] = { - "leaf", - "another_leaf", - "inside_a_node", - }, - ["2 level deep node"] = { - "leaf", - "another_leaf", - "inside_a_node", - node_inside_node = { - "final_leaf", - }, - }, - }, - title = 'test menu', - callback = function(selections) - print("test callback selection:", selections[#selections]) - end - } -end - internal.planets = function(opts) local show_pluto = opts.show_pluto or false diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index e943fdedc9..87c9285d87 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -22,15 +22,14 @@ Node.new = function(opts) self.t = opts[1] self.callback = opts.callback - self.title = opts.title or 'Menu' + self.title = opts.title table.insert(self.t, "..") - return self + return self:preprocess() end Node.new_root = function(opts) - dump(opts) local self = setmetatable({}, Node) self.t = opts[1] @@ -41,29 +40,38 @@ Node.new_root = function(opts) end self.title = opts.title or 'Menu' - return self + return self:preprocess() end -menu.Node = Node +Node.new_leaf = function(opts) + local self = setmetatable({}, Node) + + self.t = opts[1] + self.callback = opts.callback + + return self:preprocess() +end -local function preprocess(tree, root) +function Node:preprocess() local results = {} - for k, v in pairs(tree) do - if type(v) == "string" then -- leaf - table.insert(results, { leaf = v, root = root }) -- and have each element have the table. So you also have it when we hit enter + for k, v in pairs(self.t) do + if type(k) == "number" then -- leaf + table.insert(results, { leaf = v }) elseif type(k) == "string" then - table.insert(results, { branch = k, root = root }) -- when selecting you can then just check if branch is not nil. if thats the case the restart menu + table.insert(results, { branch_name = k, branches = v }) else - -- we should never get here. Am i correct? + error "BUG: should not get here" end end - table.insert(results, { branch = '..', root = root }) + self.t = results - return results + return self end +menu.Node = Node + do local Stack = {} Stack.__index = Stack @@ -100,8 +108,10 @@ do menu.open = function(tree, opts) opts = opts or {} + + opts.entry_maker = make_entry.gen_from_node(opts) + local node = tree - dump(node) if root == nil then root = node @@ -109,8 +119,9 @@ do pickers.new(opts, { prompt_title = node.title, - finder = finders.new_tree { + finder = finders.new_table { results = node.t, + entry_maker = opts.entry_maker }, sorter = conf.generic_sorter(opts), attach_mappings = function(prompt_bufnr) @@ -118,12 +129,10 @@ do actions.close(prompt_bufnr) local entry = actions.get_selected_entry() - local value = entry.value - local key = entry.key - if value == nil then - -- it is a leaf - if key == ".." then + if entry.is_leaf then + + if entry.value == ".." then local last = selections:pop() if selections:is_empty() then menu.open(root) @@ -134,21 +143,19 @@ do return end - selections:push(key) + selections:push(entry.value) local callback = node.callback or root.callback callback(selections) cleanup() - - elseif type(value) == "table" then + else -- it is a node - selections:push(key) + selections:push(entry.value) -- recurse - menu.open(value, opts) + opts.prompt_title = entry.value.title or root.title + menu.open(entry.value, opts) -- sometimes does not start insert for some reason vim.api.nvim_input('i') - else - error "value must be leaf or table" end end) diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 72a5d86072..8622a9310d 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -231,13 +231,20 @@ function StaticFinder:new(opts) local entry_maker = opts.entry_maker or make_entry.gen_from_string() + -- print('entry_maker') + -- dump(entry_maker) + assert(input_results) assert(input_results, "Results are required for static finder") assert(type(input_results) == 'table', "self.results must be a table") local results = {} for k, v in ipairs(input_results) do + -- print('value') + -- dump(v) local entry = entry_maker(v) + -- print('a entry') + -- dump(entry) if entry then entry.index = k @@ -248,38 +255,6 @@ function StaticFinder:new(opts) return setmetatable({ results = results }, self) end -function StaticFinder:new_tree(opts) - assert(opts, "Options are required. See documentation for usage") - - local input_results - if vim.tbl_islist(opts) then - input_results = opts - else - input_results = opts.results - end - - local entry_maker = opts.entry_maker or make_entry.gen_from_tree() - - assert(input_results) - assert(input_results, "Results are required for static finder") - assert(type(input_results) == 'table', "self.results must be a table") - - local results = {} - local counter = 0 - for k, v in pairs(input_results) do - local entry = entry_maker(k, v) - - if entry then - -- entry.index = counter - table.insert(results, entry) - end - - counter = counter + 1 - end - - return setmetatable({ results = results }, self) -end - function StaticFinder:_find(_, process_result, process_complete) for _, v in ipairs(self.results) do process_result(v) diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 6d45cbe75c..0e63379c93 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -57,6 +57,22 @@ do end end +function make_entry.gen_from_node(_) + return function(node) + local display = node.leaf or node.branch_name + local value = node.branches or node.leaf + -- bool to tell if it is a leaf + local is_leaf = node.leaf and true or false + + return { + value = value, + display = display, + ordinal = display, + is_leaf = is_leaf + } + end +end + do local lookup_keys = { ordinal = 1, From 57f1736abebf51f4437e4de3ea09a01c485c40db Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 12:46:18 -0500 Subject: [PATCH 07/38] added selections and remember --- lua/telescope/builtin/menu.lua | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 87c9285d87..106282e6aa 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -83,26 +83,30 @@ do end function Stack:push(...) - local args = vim.tbl_flatten {...} + local args = {...} for _, v in ipairs(args) do table.insert(self, v) end end function Stack:is_empty() - return (#(self.t or {})) == 0 + return #self == 0 end function Stack:pop() - table.remove(self) + return table.remove(self) end + -- the selections that the user has made, used for .., includes the entire tree in order to recurse + local remember = Stack.new() + -- the selections that the user has made, only the display, does not include the entire tree local selections = Stack.new() local root -- cleanup the state local function cleanup() root = nil + remember = Stack.new() selections = Stack.new() end @@ -133,26 +137,28 @@ do if entry.is_leaf then if entry.value == ".." then - local last = selections:pop() - if selections:is_empty() then + local last = remember:pop() + if remember:is_empty() then menu.open(root) else - menu.open(root[last]) + menu.open(last) end api.nvim_input('i') return end + remember:push(entry.value) selections:push(entry.value) + local callback = node.callback or root.callback callback(selections) cleanup() else -- it is a node - selections:push(entry.value) + remember:push(entry.value) + selections:push(entry.display) -- for tree only add display, not full tree -- recurse - opts.prompt_title = entry.value.title or root.title menu.open(entry.value, opts) -- sometimes does not start insert for some reason vim.api.nvim_input('i') @@ -170,11 +176,16 @@ menu.test = function(opts) { "a leaf", "another_leaf", - "blah", another_node = Node.new { { "inner", "inner2", + second_level_node = Node.new { + { + "inner inner leaf", + "another inner inner leaf", + } + } } } }, From 5441dc83bb49b13140131de94e474da33211f1dd Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 12:46:39 -0500 Subject: [PATCH 08/38] removed unused stuff --- lua/telescope/builtin/menu.lua | 38 ---------------------------------- 1 file changed, 38 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 106282e6aa..8a8610e782 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -2,16 +2,10 @@ local api = vim.api local actions = require('telescope.actions') local finders = require('telescope.finders') local make_entry = require('telescope.make_entry') -local path = require('telescope.path') local pickers = require('telescope.pickers') -local previewers = require('telescope.previewers') -local sorters = require('telescope.sorters') -local utils = require('telescope.utils') local conf = require('telescope.config').values -local filter = vim.tbl_filter - local menu = {} Node = {} @@ -194,38 +188,6 @@ menu.test = function(opts) end, title = "testing", }, opts) - -- menu.menu { - -- n = Node.new_root { - -- t = { - -- "a_leaf", - -- "another_leaf", - -- "blah", - -- ["1 level deep node"] = Node.new_root { - -- t = { - -- "leaf", - -- "another_leaf", - -- "inside_a_node", - -- } - -- }, - -- ["2 level deep node"] = { - -- t = { - -- "leaf", - -- "another_leaf", - -- "inside_a_node", - -- node_inside_node = Node.new_root { - -- t = { - -- "final_leaf", - -- } - -- }, - -- } - -- }, - -- }, - -- }, - -- title = 'test menu', - -- callback = function(selections) - -- print("test callback selection:", selections[#selections]) - -- end - -- } end return menu From 415d19eee1fc1988976ebcee579c7cc4b508aec0 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 12:53:57 -0500 Subject: [PATCH 09/38] fixed warnings --- lua/telescope/builtin/menu.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 8a8610e782..e57dabfd59 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -8,7 +8,7 @@ local conf = require('telescope.config').values local menu = {} -Node = {} +local Node = {} Node.__index = Node Node.new = function(opts) @@ -184,7 +184,9 @@ menu.test = function(opts) } }, callback = function(selections) - dump(selections) + for selection in selections do + print(selection) + end end, title = "testing", }, opts) From 7f52cf784c24150fae30e72dd3ae19e1eca324ea Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 13:44:08 -0500 Subject: [PATCH 10/38] fixed remember and selections pop --- lua/telescope/builtin/menu.lua | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index e57dabfd59..8be62a8657 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -40,15 +40,21 @@ end Node.new_leaf = function(opts) local self = setmetatable({}, Node) - self.t = opts[1] + self.t = { leaf = opts[1] } self.callback = opts.callback - return self:preprocess() + return self end function Node:preprocess() local results = {} + if type(self.t) == "string" then + table.insert(results, { leaf = self.t }) + self.t = results + return self + end + for k, v in pairs(self.t) do if type(k) == "number" then -- leaf table.insert(results, { leaf = v }) @@ -131,8 +137,13 @@ do if entry.is_leaf then if entry.value == ".." then + -- first pop is the one you are currently in + remember:pop() + -- second pop is the last one local last = remember:pop() - if remember:is_empty() then + + -- if there is no last one, just open the root + if last == nil then menu.open(root) else menu.open(last) @@ -144,7 +155,7 @@ do remember:push(entry.value) selections:push(entry.value) - local callback = node.callback or root.callback + local callback = entry.callback or root.callback callback(selections) cleanup() @@ -152,6 +163,7 @@ do -- it is a node remember:push(entry.value) selections:push(entry.display) -- for tree only add display, not full tree + -- recurse menu.open(entry.value, opts) -- sometimes does not start insert for some reason @@ -176,7 +188,7 @@ menu.test = function(opts) "inner2", second_level_node = Node.new { { - "inner inner leaf", + -- Node.new_leaf {"inner inner leaf", callback = function() print('this is a specific callback') end}, "another inner inner leaf", } } @@ -184,7 +196,7 @@ menu.test = function(opts) } }, callback = function(selections) - for selection in selections do + for _, selection in ipairs(selections) do print(selection) end end, From 46c1c165ee4af6c8f3cc546a88d7d14c7ba68bae Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 13:52:28 -0500 Subject: [PATCH 11/38] started branch --- lua/telescope/builtin/init.lua | 2 +- lua/telescope/builtin/menu.lua | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 1b8f6d2a56..0ba59690b9 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -35,7 +35,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.menu = require('telescope.builtin.menu').menu +builtin.menu = require('telescope.builtin.menu').open builtin.test_menu = require('telescope.builtin.menu').test builtin.Node = require('telescope.builtin.menu').Node diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 8be62a8657..1f0b7454dd 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -97,6 +97,10 @@ do return table.remove(self) end + function Stack:last() + return self[#self] + end + -- the selections that the user has made, used for .., includes the entire tree in order to recurse local remember = Stack.new() -- the selections that the user has made, only the display, does not include the entire tree From f8106c54606b6db562013dee2a765a73033f99a0 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 17:57:19 -0500 Subject: [PATCH 12/38] added go function --- lua/telescope/builtin/menu.lua | 177 ++++++++++++++++----------------- 1 file changed, 83 insertions(+), 94 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 1f0b7454dd..43f94973e8 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -72,116 +72,105 @@ end menu.Node = Node -do - local Stack = {} - Stack.__index = Stack +local Stack = {} +Stack.__index = Stack - function Stack.new(init) - init = init or {} - local self = setmetatable(init, Stack) - return self - end - - function Stack:push(...) - local args = {...} - for _, v in ipairs(args) do - table.insert(self, v) - end +function Stack.new(init) + local self = setmetatable({}, Stack) + if init ~= nil then + self:push(init) end + return self +end - function Stack:is_empty() - return #self == 0 +function Stack:push(...) + local args = {...} + for _, v in ipairs(args) do + table.insert(self, v) end +end - function Stack:pop() - return table.remove(self) - end +function Stack:is_empty() + return #self == 0 +end - function Stack:last() - return self[#self] - end +function Stack:pop() + return table.remove(self) +end - -- the selections that the user has made, used for .., includes the entire tree in order to recurse - local remember = Stack.new() - -- the selections that the user has made, only the display, does not include the entire tree - local selections = Stack.new() - local root +function Stack:last() + return self[#self] +end - -- cleanup the state - local function cleanup() - root = nil - remember = Stack.new() - selections = Stack.new() - end +-- helper function to recurse with more arguments +-- remember contains the actual tree that is remembered so we can use it with .. +-- selections contains only the display so we can pass it into the callback +local function go(tree, opts, root, remember, selections) + opts.entry_maker = opts.entry_maker or make_entry.gen_from_node(opts) + + pickers.new(opts, { + prompt_title = tree.title or root.title, + finder = finders.new_table { + results = tree.t, + entry_maker = opts.entry_maker + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions._goto_file_selection:replace(function() + actions.close(prompt_bufnr) - menu.open = function(tree, opts) - opts = opts or {} + local entry = actions.get_selected_entry() - opts.entry_maker = make_entry.gen_from_node(opts) + if entry.is_leaf then - local node = tree + -- .. means go back + if entry.value == ".." then + -- first pop is the one you are currently in + remember:pop() + -- the last one is the last tree selected, but do not pop off, we want it to be available for the next prompt + local last = remember:last() - if root == nil then - root = node - end + -- pop current selection because we went back + selections:pop() - pickers.new(opts, { - prompt_title = node.title, - finder = finders.new_table { - results = node.t, - entry_maker = opts.entry_maker - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions._goto_file_selection:replace(function() - actions.close(prompt_bufnr) - - local entry = actions.get_selected_entry() - - if entry.is_leaf then - - if entry.value == ".." then - -- first pop is the one you are currently in - remember:pop() - -- second pop is the last one - local last = remember:pop() - - -- if there is no last one, just open the root - if last == nil then - menu.open(root) - else - menu.open(last) - end - api.nvim_input('i') - return - end - - remember:push(entry.value) - selections:push(entry.value) - - local callback = entry.callback or root.callback - callback(selections) - - cleanup() - else - -- it is a node - remember:push(entry.value) - selections:push(entry.display) -- for tree only add display, not full tree - - -- recurse - menu.open(entry.value, opts) - -- sometimes does not start insert for some reason - vim.api.nvim_input('i') + go(last, opts, root, remember, selections) + api.nvim_input('i') + return end - end) - return true - end, - }):find() - end + remember:push(entry.value) + selections:push(entry.value) + + local callback = entry.callback or root.callback + callback(selections) + else + -- it is a node + remember:push(entry.value) + selections:push(entry.display) -- for tree only add display, not full tree + + -- recurse + go(entry.value, opts, root, remember, selections) + + -- sometimes does not start insert for some reason + vim.api.nvim_input('i') + end + end) + + return true + end, + }):find() +end + +-- entry point for menu +menu.open = function(root) + local selections = Stack.new() + local remember = Stack.new(root) + local opts = {} + + go(root, opts, root, remember, selections) end -menu.test = function(opts) +menu.test = function() menu.open(Node.new_root { { "a leaf", @@ -205,7 +194,7 @@ menu.test = function(opts) end end, title = "testing", - }, opts) + }) end return menu From ff98a6b5f993aea8d81b14b2af071362dc6b3447 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 19:40:39 -0500 Subject: [PATCH 13/38] changed up test --- lua/telescope/builtin/menu.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 43f94973e8..fb7d8b1be7 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -173,16 +173,16 @@ end menu.test = function() menu.open(Node.new_root { { - "a leaf", - "another_leaf", - another_node = Node.new { + "top level leaf", + "another top level leaf", + ["a node"] = Node.new { { - "inner", - "inner2", - second_level_node = Node.new { + "second level leaf", + "another second level leaf", + ["second level node"] = Node.new { { -- Node.new_leaf {"inner inner leaf", callback = function() print('this is a specific callback') end}, - "another inner inner leaf", + "third level leaf", } } } From 5229f35583bc2cd385a1a93b3565b80158e6e228 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 20:06:42 -0500 Subject: [PATCH 14/38] removed root parameter from go function --- lua/telescope/builtin/menu.lua | 63 ++++++++++++++++++++++++---------- lua/telescope/make_entry.lua | 37 ++------------------ 2 files changed, 47 insertions(+), 53 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index fb7d8b1be7..526837a3d4 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -37,10 +37,13 @@ Node.new_root = function(opts) return self:preprocess() end -Node.new_leaf = function(opts) - local self = setmetatable({}, Node) +local Leaf = {} +Leaf.__index = Leaf + +function Leaf.new(opts) + local self = setmetatable({}, Leaf) - self.t = { leaf = opts[1] } + self.t = opts[1] self.callback = opts.callback return self @@ -49,15 +52,19 @@ end function Node:preprocess() local results = {} - if type(self.t) == "string" then - table.insert(results, { leaf = self.t }) - self.t = results - return self - end - for k, v in pairs(self.t) do if type(k) == "number" then -- leaf - table.insert(results, { leaf = v }) + + if type(v) == "string" then + -- its a leaf without a specific callback, just a regular string + table.insert(results, { leaf = v }) + elseif type(v) == "table" then + -- its a leaf with a specific callback and other options, a Leaf class + table.insert(results, { leaf = v.t, callback = v.callback }) + else + error "BUG: should not get here" + end + elseif type(k) == "string" then table.insert(results, { branch_name = k, branches = v }) else @@ -102,10 +109,16 @@ function Stack:last() return self[#self] end +function Stack:first() + return self[1] +end + -- helper function to recurse with more arguments -- remember contains the actual tree that is remembered so we can use it with .. -- selections contains only the display so we can pass it into the callback -local function go(tree, opts, root, remember, selections) +local function go(tree, opts, remember, selections) + local root = remember:first() + opts.entry_maker = opts.entry_maker or make_entry.gen_from_node(opts) pickers.new(opts, { @@ -121,19 +134,24 @@ local function go(tree, opts, root, remember, selections) local entry = actions.get_selected_entry() + if entry == nil then + error "No entry selected" + end + if entry.is_leaf then -- .. means go back if entry.value == ".." then -- first pop is the one you are currently in remember:pop() - -- the last one is the last tree selected, but do not pop off, we want it to be available for the next prompt + -- the last one is the last tree selected, but do not pop off, + -- we want it to be available for the next prompt local last = remember:last() -- pop current selection because we went back selections:pop() - go(last, opts, root, remember, selections) + go(last, opts, remember, selections) api.nvim_input('i') return end @@ -149,7 +167,7 @@ local function go(tree, opts, root, remember, selections) selections:push(entry.display) -- for tree only add display, not full tree -- recurse - go(entry.value, opts, root, remember, selections) + go(entry.value, opts, remember, selections) -- sometimes does not start insert for some reason vim.api.nvim_input('i') @@ -167,10 +185,11 @@ menu.open = function(root) local remember = Stack.new(root) local opts = {} - go(root, opts, root, remember, selections) + go(root, opts, remember, selections) end menu.test = function() + -- all options for default will propagate if they are not specified for inner nodes menu.open(Node.new_root { { "top level leaf", @@ -181,19 +200,27 @@ menu.test = function() "another second level leaf", ["second level node"] = Node.new { { - -- Node.new_leaf {"inner inner leaf", callback = function() print('this is a specific callback') end}, + Leaf.new { + "another third level leaf with specific callback", + -- this callback will override the default one + callback = function() print('this is a specific callback') end + }, "third level leaf", - } + }, + title = 'this title overrides the defualt one' } } } }, + -- default callback if not specified for leaf + -- passed in a stack of all the selections that the user has made callback = function(selections) for _, selection in ipairs(selections) do print(selection) end end, - title = "testing", + -- this title will be default if title for node is not specified + title = "testing menu", }) end diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 1d07346c1e..3f31d1888d 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -68,45 +68,12 @@ function make_entry.gen_from_node(_) value = value, display = display, ordinal = display, - is_leaf = is_leaf + is_leaf = is_leaf, + callback = node.callback, } end end -do - local lookup_keys = { - ordinal = 1, - value = 2, - display = 1, - key = 1, - } - - local mt_tree_entry = { - __index = function(t, k) - return rawget(t, rawget(lookup_keys, k)) - end - } - - function make_entry.gen_from_tree() - return function(key, value) - if type(key) == "number" then - -- it is a leaf - return setmetatable({ - value, - -- this means it is leaf - nil, - }, mt_tree_entry) - else - -- it is a node - return setmetatable({ - key, - value, - }, mt_tree_entry) - end - end - end -end - do local lookup_keys = { ordinal = 1, From 12bed530bbc83258426f22d4673527a07d82a7ac Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Thu, 17 Dec 2020 20:15:00 -0500 Subject: [PATCH 15/38] changed back to not do_close --- lua/telescope/actions/init.lua | 2 +- lua/telescope/builtin/menu.lua | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index b0dcc26c80..e2318888ba 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -226,7 +226,7 @@ end actions.run_builtin = function(prompt_bufnr) local entry = actions.get_selected_entry(prompt_bufnr) - do_close(prompt_bufnr, true) + _do_close(prompt_bufnr, true) require('telescope.builtin')[entry.text]() end diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 526837a3d4..c24dc4f3ff 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -152,6 +152,7 @@ local function go(tree, opts, remember, selections) selections:pop() go(last, opts, remember, selections) + api.nvim_input('i') return end From 5d5c73eada89ad2c9c193fbc5d68b5194f74598c Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Fri, 18 Dec 2020 13:05:58 -0500 Subject: [PATCH 16/38] removed node and leaf classes --- lua/telescope/builtin/menu.lua | 91 +++++++++++++++------------------- 1 file changed, 41 insertions(+), 50 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index c24dc4f3ff..a71cb1f582 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -8,51 +8,11 @@ local conf = require('telescope.config').values local menu = {} -local Node = {} -Node.__index = Node - -Node.new = function(opts) - local self = setmetatable({}, Node) - - self.t = opts[1] - self.callback = opts.callback - self.title = opts.title - - table.insert(self.t, "..") - - return self:preprocess() -end - -Node.new_root = function(opts) - local self = setmetatable({}, Node) - - self.t = opts[1] - if opts.callback == nil then - error "Root node must have default callback" - else - self.callback = opts.callback - end - self.title = opts.title or 'Menu' - - return self:preprocess() -end - -local Leaf = {} -Leaf.__index = Leaf - -function Leaf.new(opts) - local self = setmetatable({}, Leaf) - - self.t = opts[1] - self.callback = opts.callback - - return self -end - -function Node:preprocess() +-- takes node +local function preprocess(node) local results = {} - for k, v in pairs(self.t) do + for k, v in pairs(node.t) do if type(k) == "number" then -- leaf if type(v) == "string" then @@ -72,12 +32,43 @@ function Node:preprocess() end end - self.t = results + node.t = results - return self + return node end -menu.Node = Node +function menu.node(opts) + local node = {} + node.t = opts[1] + table.insert(node.t, "..") -- add go back + node.callback = opts.callback + node.title = opts.title + + return preprocess(node) +end + +function menu.root(opts) + local root = {} + + root.t = opts[1] + if opts.callback == nil then + error "Root node must have default callback" + else + root.callback = opts.callback + end + root.title = opts.title or 'Menu' + + return preprocess(root) +end + +function menu.leaf(opts) + local leaf = {} + + leaf.t = opts[1] + leaf.callback = opts.callback + + return leaf +end local Stack = {} Stack.__index = Stack @@ -191,17 +182,17 @@ end menu.test = function() -- all options for default will propagate if they are not specified for inner nodes - menu.open(Node.new_root { + menu.open(menu.root { { "top level leaf", "another top level leaf", - ["a node"] = Node.new { + ["a node"] = menu.node { { "second level leaf", "another second level leaf", - ["second level node"] = Node.new { + ["second level node"] = menu.node { { - Leaf.new { + menu.leaf { "another third level leaf with specific callback", -- this callback will override the default one callback = function() print('this is a specific callback') end From 1c049f12e0d93510f0bb75e3fbbff8f05e5a2bf6 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Fri, 18 Dec 2020 13:11:11 -0500 Subject: [PATCH 17/38] removed stack class instead for table.insert and table.remove --- lua/telescope/builtin/menu.lua | 57 ++++++++-------------------------- 1 file changed, 13 insertions(+), 44 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index a71cb1f582..27f0271ea4 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -70,45 +70,11 @@ function menu.leaf(opts) return leaf end -local Stack = {} -Stack.__index = Stack - -function Stack.new(init) - local self = setmetatable({}, Stack) - if init ~= nil then - self:push(init) - end - return self -end - -function Stack:push(...) - local args = {...} - for _, v in ipairs(args) do - table.insert(self, v) - end -end - -function Stack:is_empty() - return #self == 0 -end - -function Stack:pop() - return table.remove(self) -end - -function Stack:last() - return self[#self] -end - -function Stack:first() - return self[1] -end - -- helper function to recurse with more arguments -- remember contains the actual tree that is remembered so we can use it with .. -- selections contains only the display so we can pass it into the callback local function go(tree, opts, remember, selections) - local root = remember:first() + local root = remember[1] opts.entry_maker = opts.entry_maker or make_entry.gen_from_node(opts) @@ -134,13 +100,13 @@ local function go(tree, opts, remember, selections) -- .. means go back if entry.value == ".." then -- first pop is the one you are currently in - remember:pop() + table.remove(remember) -- the last one is the last tree selected, but do not pop off, -- we want it to be available for the next prompt - local last = remember:last() + local last = remember[#remember] -- pop current selection because we went back - selections:pop() + table.remove(selections) go(last, opts, remember, selections) @@ -148,15 +114,15 @@ local function go(tree, opts, remember, selections) return end - remember:push(entry.value) - selections:push(entry.value) + table.insert(remember, entry.value) + table.insert(selections, entry.value) local callback = entry.callback or root.callback callback(selections) else -- it is a node - remember:push(entry.value) - selections:push(entry.display) -- for tree only add display, not full tree + table.insert(remember, entry.value) + table.insert(selections, entry.display) -- recurse go(entry.value, opts, remember, selections) @@ -173,8 +139,11 @@ end -- entry point for menu menu.open = function(root) - local selections = Stack.new() - local remember = Stack.new(root) + local selections = {} + + local remember = {} + table.insert(remember, root) + local opts = {} go(root, opts, remember, selections) From bc359b53bc1aa8d35397742854d6c826b93ea5c6 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Fri, 18 Dec 2020 13:13:32 -0500 Subject: [PATCH 18/38] fixed warning --- lua/telescope/actions/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index e2318888ba..b0dcc26c80 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -226,7 +226,7 @@ end actions.run_builtin = function(prompt_bufnr) local entry = actions.get_selected_entry(prompt_bufnr) - _do_close(prompt_bufnr, true) + do_close(prompt_bufnr, true) require('telescope.builtin')[entry.text]() end From 57f9bb77817db0de50266a1dbbfe777d987cd2c6 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Fri, 18 Dec 2020 19:28:20 -0500 Subject: [PATCH 19/38] started branch --- lua/telescope/builtin/menu.lua | 168 +++++++++++++++++++++++---------- lua/telescope/finders.lua | 7 -- lua/telescope/make_entry.lua | 2 - 3 files changed, 117 insertions(+), 60 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 27f0271ea4..cb0a06d3d6 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -10,31 +10,47 @@ local menu = {} -- takes node local function preprocess(node) + print('arg to pairs') + dump(node[1]) local results = {} - for k, v in pairs(node.t) do - if type(k) == "number" then -- leaf + for key, value in pairs(node[1]) do + if type(key) == "number" then -- leaf - if type(v) == "string" then - -- its a leaf without a specific callback, just a regular string - table.insert(results, { leaf = v }) - elseif type(v) == "table" then - -- its a leaf with a specific callback and other options, a Leaf class - table.insert(results, { leaf = v.t, callback = v.callback }) + if type(value) == "string" then + + table.insert(results, {leaf = value}) + + elseif type(value) == "table" then + -- its a leaf with a specific callback and other options + local processed = {} + processed.leaf = value + + table.insert(results, processed) else error "BUG: should not get here" end - elseif type(k) == "string" then - table.insert(results, { branch_name = k, branches = v }) + elseif type(key) == "string" then -- node + + print('value:') + dump(value) + table.insert(value[1], "..") + + local processed = {} + processed.branch_name = key + processed.branches = value + + table.insert(results, processed) + else error "BUG: should not get here" end end - node.t = results - - return node + print('results:') + dump(results) + return results end function menu.node(opts) @@ -81,7 +97,7 @@ local function go(tree, opts, remember, selections) pickers.new(opts, { prompt_title = tree.title or root.title, finder = finders.new_table { - results = tree.t, + results = tree, entry_maker = opts.entry_maker }, sorter = conf.generic_sorter(opts), @@ -108,6 +124,7 @@ local function go(tree, opts, remember, selections) -- pop current selection because we went back table.remove(selections) + -- no need to process because it is the last one go(last, opts, remember, selections) api.nvim_input('i') @@ -117,15 +134,19 @@ local function go(tree, opts, remember, selections) table.insert(remember, entry.value) table.insert(selections, entry.value) + dump(root) local callback = entry.callback or root.callback callback(selections) else -- it is a node - table.insert(remember, entry.value) - table.insert(selections, entry.display) + dump(entry.value) + local processed_value = preprocess(entry.value) + dump(processed_value) + table.insert(remember, processed_value) + table.insert(selections, processed_value) -- recurse - go(entry.value, opts, remember, selections) + go(processed_value, opts, remember, selections) -- sometimes does not start insert for some reason vim.api.nvim_input('i') @@ -138,51 +159,96 @@ local function go(tree, opts, remember, selections) end -- entry point for menu -menu.open = function(root) +menu.open = function(opts) + opts = opts or {} + opts.tree = preprocess(opts.tree) + local selections = {} local remember = {} - table.insert(remember, root) + table.insert(remember, opts.tree) - local opts = {} + dump(opts) - go(root, opts, remember, selections) + go(opts.tree, opts, remember, selections) end +-- menu.test = function() +-- -- all options for default will propagate if they are not specified for inner nodes +-- menu.open(menu.root { +-- { +-- "top level leaf", +-- "another top level leaf", +-- ["a node"] = menu.node { +-- { +-- "second level leaf", +-- "another second level leaf", +-- ["second level node"] = menu.node { +-- { +-- menu.leaf { +-- "another third level leaf with specific callback", +-- -- this callback will override the default one +-- callback = function() print('this is a specific callback') end +-- }, +-- "third level leaf", +-- }, +-- title = 'this title overrides the defualt one' +-- } +-- } +-- } +-- }, +-- -- default callback if not specified for leaf +-- -- passed in a stack of all the selections that the user has made +-- callback = function(selections) +-- for _, selection in ipairs(selections) do +-- print(selection) +-- end +-- end, +-- -- this title will be default if title for node is not specified +-- title = "testing menu", +-- }) +-- end + menu.test = function() - -- all options for default will propagate if they are not specified for inner nodes - menu.open(menu.root { - { - "top level leaf", - "another top level leaf", - ["a node"] = menu.node { - { - "second level leaf", - "another second level leaf", - ["second level node"] = menu.node { - { - menu.leaf { - "another third level leaf with specific callback", - -- this callback will override the default one - callback = function() print('this is a specific callback') end + menu.open { + tree = { + { + "top level leaf", + "another top level leaf", + ["a node"] = { + -- instead of directly setting the key to the value, we set it to a table of options, [1] is the contents + { + "second level leaf", + "another second level leaf", + ["second level node"] = { + { + -- vs this way, we can have multiple options + { + "third level leaf with a specific callback different", + -- for example we might want to set the description + description = 'this is a description', -- not implemented yet + callback = function() + print("this is a specific callback") + end, + }, + "third level leaf", }, - "third level leaf", - }, - title = 'this title overrides the defualt one' - } + -- because it is a table inside of a table, we can set options as the contents of the tree will be node[1] + -- other options will be anything except [1] + title = 'this title overrides the defualt one' + } + }, + title = 'second level title' } - } - }, - -- default callback if not specified for leaf - -- passed in a stack of all the selections that the user has made - callback = function(selections) - for _, selection in ipairs(selections) do - print(selection) + }, + title = 'test menu', + callback = function(selections) + for _, selection in pairs(selections) do + print(selection) + end end - end, - -- this title will be default if title for node is not specified - title = "testing menu", - }) + }, + } end return menu diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 8622a9310d..f010867254 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -231,20 +231,13 @@ function StaticFinder:new(opts) local entry_maker = opts.entry_maker or make_entry.gen_from_string() - -- print('entry_maker') - -- dump(entry_maker) - assert(input_results) assert(input_results, "Results are required for static finder") assert(type(input_results) == 'table', "self.results must be a table") local results = {} for k, v in ipairs(input_results) do - -- print('value') - -- dump(v) local entry = entry_maker(v) - -- print('a entry') - -- dump(entry) if entry then entry.index = k diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 3f31d1888d..5f3b3359a3 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -61,7 +61,6 @@ function make_entry.gen_from_node(_) return function(node) local display = node.leaf or node.branch_name local value = node.branches or node.leaf - -- bool to tell if it is a leaf local is_leaf = node.leaf and true or false return { @@ -69,7 +68,6 @@ function make_entry.gen_from_node(_) display = display, ordinal = display, is_leaf = is_leaf, - callback = node.callback, } end end From e741585c60e9687fe2d1687b25020bdfef624af4 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sat, 19 Dec 2020 10:21:31 -0500 Subject: [PATCH 20/38] added better preprocessor and tree class --- lua/telescope/builtin/menu.lua | 147 ++++++++++++++++----------------- 1 file changed, 70 insertions(+), 77 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index cb0a06d3d6..b837273785 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -8,10 +8,17 @@ local conf = require('telescope.config').values local menu = {} --- takes node +--- will mutate table +local function extend_non_number_keys(table, add) + for k, v in pairs(add) do + if type(k) == "string" then + table[k] = v + end + end +end + +-- takes node syntax and processes it into internal representation local function preprocess(node) - print('arg to pairs') - dump(node[1]) local results = {} for key, value in pairs(node[1]) do @@ -23,67 +30,48 @@ local function preprocess(node) elseif type(value) == "table" then -- its a leaf with a specific callback and other options - local processed = {} - processed.leaf = value + local processed = {leaf = value[1]} + -- all non numbers keys are conf for the leaf + processed.conf = {} + extend_non_number_keys(processed.conf, value) table.insert(results, processed) + else error "BUG: should not get here" end elseif type(key) == "string" then -- node - print('value:') - dump(value) table.insert(value[1], "..") - - local processed = {} - processed.branch_name = key - processed.branches = value - - table.insert(results, processed) + table.insert(results, {branch_name = key, branches = preprocess(value)}) -- recurse until we hit a leaf else error "BUG: should not get here" end end - print('results:') - dump(results) + -- node[1] is contents, everything else (non number keys) are conf + results.conf = {} + extend_non_number_keys(results.conf, node) + return results end -function menu.node(opts) - local node = {} - node.t = opts[1] - table.insert(node.t, "..") -- add go back - node.callback = opts.callback - node.title = opts.title +local Tree = {} +Tree.__index = Tree - return preprocess(node) +function Tree:iter() end -function menu.root(opts) - local root = {} - - root.t = opts[1] - if opts.callback == nil then - error "Root node must have default callback" - else - root.callback = opts.callback - end - root.title = opts.title or 'Menu' - - return preprocess(root) +function Tree:display() end -function menu.leaf(opts) - local leaf = {} - - leaf.t = opts[1] - leaf.callback = opts.callback - - return leaf +-- will mutate consume the tree_syntax +function Tree.new(tree_syntax) + local tree = preprocess(tree_syntax) + local root = {branch_name = "root", branches = tree} + return root end -- helper function to recurse with more arguments @@ -173,42 +161,6 @@ menu.open = function(opts) go(opts.tree, opts, remember, selections) end --- menu.test = function() --- -- all options for default will propagate if they are not specified for inner nodes --- menu.open(menu.root { --- { --- "top level leaf", --- "another top level leaf", --- ["a node"] = menu.node { --- { --- "second level leaf", --- "another second level leaf", --- ["second level node"] = menu.node { --- { --- menu.leaf { --- "another third level leaf with specific callback", --- -- this callback will override the default one --- callback = function() print('this is a specific callback') end --- }, --- "third level leaf", --- }, --- title = 'this title overrides the defualt one' --- } --- } --- } --- }, --- -- default callback if not specified for leaf --- -- passed in a stack of all the selections that the user has made --- callback = function(selections) --- for _, selection in ipairs(selections) do --- print(selection) --- end --- end, --- -- this title will be default if title for node is not specified --- title = "testing menu", --- }) --- end - menu.test = function() menu.open { tree = { @@ -251,4 +203,45 @@ menu.test = function() } end +local tree = { + { + "top level leaf", + "another top level leaf", + ["a node"] = { + -- instead of directly setting the key to the value, we set it to a table of options, [1] is the contents + { + "second level leaf", + "another second level leaf", + ["second level node"] = { + { + -- vs this way, we can have multiple options + { + "third level leaf with a specific callback different", + -- for example we might want to set the description + description = 'this is a description', -- not implemented yet + callback = function() + print("this is a specific callback") + end, + }, + "third level leaf", + }, + -- because it is a table inside of a table, we can set options as the contents of the tree will be node[1] + -- other options will be anything except [1] + title = 'this title overrides the defualt one' + } + }, + title = 'second level title' + } + }, + title = 'test menu', + callback = function(selections) + for _, selection in pairs(selections) do + print(selection) + end + end +} + +local res = Tree.new(tree) +dump(res) + return menu From 76017083b178159a9e2524789d450915a9dc4cb2 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sat, 19 Dec 2020 11:22:46 -0500 Subject: [PATCH 21/38] started some tests --- lua/telescope/builtin/menu.lua | 21 +++- lua/tests/automated/tree_spec.lua | 182 ++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 lua/tests/automated/tree_spec.lua diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index b837273785..f1addc1bb3 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -26,7 +26,7 @@ local function preprocess(node) if type(value) == "string" then - table.insert(results, {leaf = value}) + table.insert(results, {leaf = value, conf = {}}) elseif type(value) == "table" then -- its a leaf with a specific callback and other options @@ -52,6 +52,7 @@ local function preprocess(node) end -- node[1] is contents, everything else (non number keys) are conf + -- results = {results} results.conf = {} extend_non_number_keys(results.conf, node) @@ -74,6 +75,9 @@ function Tree.new(tree_syntax) return root end +menu.preprocess = preprocess +menu.Tree = Tree + -- helper function to recurse with more arguments -- remember contains the actual tree that is remembered so we can use it with .. -- selections contains only the display so we can pass it into the callback @@ -241,7 +245,20 @@ local tree = { end } -local res = Tree.new(tree) +local tree2 = { + { + "top level leaf", + "another top level leaf", + ["a node"] = { + { + "second level leaf", + "another second level leaf", + } + } + }, +} + +local res = preprocess(tree2) dump(res) return menu diff --git a/lua/tests/automated/tree_spec.lua b/lua/tests/automated/tree_spec.lua new file mode 100644 index 0000000000..1f24c28285 --- /dev/null +++ b/lua/tests/automated/tree_spec.lua @@ -0,0 +1,182 @@ +local preprocess = require('telescope.builtin.menu').preprocess + +local _eq = function(a, b) + b = preprocess(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 = {}}, + }, + conf = {} + } + + _eq(expected, tree) + end) + + it('should preprocess with configuration', function() + local tree = { + { + "top level leaf", + "another top level leaf", + }, + title = 'test menu', + callback = print, + } + + local expected = { + { + {leaf = "top level leaf", conf = {}}, + {leaf = "another top level leaf", conf = {}}, + }, + conf = { + title = 'test menu', + callback = print, + } + } + + _eq(expected, tree) + 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 = {} + }, + }, + conf = {} + } + + _eq(expected, tree) + 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 = {} + }, + }, + conf = {} + } + + -- _eq(expected, tree) + end) + + it('should preprocess the tree correctly with only one level', function() + local tree = { + { + "top level leaf", + "another top level leaf", + ["a node"] = { + { + { + "second level leaf with a specific callback different", + description = 'this is a description', + callback = function() + print("this is a specific callback") + end, + }, + "another leaf", + }, + title = 'second level title' + } + }, + title = 'test menu', + callback = function(selections) + for _, selection in pairs(selections) do + print(selection) + end + end + } + + local res = preprocess(tree) + + local expected = { + { + {leaf = "top level leaf"}, + {leaf = "another top level leaf"}, + { + branch_name = "a node", + branches = { + { + leaf = "second level leaf with a specific callback different", + conf = { + description = 'this is a description', + callback = function() + print("this is a specific callback") + end, + } + }, + "another leaf", + }, + conf = { + title = 'second level title' + } + } + }, + conf = { + title = 'test menu', + callback = function(selections) + for _, selection in pairs(selections) do + print(selection) + end + end + } + } + + -- eq(res, expected) + end) +end) From 47156de6ba5b635e1fc59f2699836698424894f7 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sat, 19 Dec 2020 11:48:35 -0500 Subject: [PATCH 22/38] finished making tests pass --- lua/telescope/builtin/menu.lua | 36 ++++++------ lua/tests/automated/tree_spec.lua | 96 ++++++------------------------- 2 files changed, 38 insertions(+), 94 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index f1addc1bb3..999a613d62 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -17,7 +17,8 @@ local function extend_non_number_keys(table, add) end end --- takes node syntax and processes it into internal representation +-- takes node syntax and processes it into internal representation, +-- does no fully preprocess root, as that is a special node without a key of string, it is just there local function preprocess(node) local results = {} @@ -44,21 +45,27 @@ local function preprocess(node) elseif type(key) == "string" then -- node table.insert(value[1], "..") - table.insert(results, {branch_name = key, branches = preprocess(value)}) -- recurse until we hit a leaf + local preprocessed = {branch_name = key, branches = preprocess(value)} + preprocessed.conf = {} + extend_non_number_keys(preprocessed.conf, value) + table.insert(results, preprocessed) -- recurse until we hit a leaf else error "BUG: should not get here" end end - -- node[1] is contents, everything else (non number keys) are conf - -- results = {results} - results.conf = {} - extend_non_number_keys(results.conf, node) - return results end +local function preprocess_root(root) + local processed = {branches = preprocess(root), branch_name = "root"} + processed.conf = {} + extend_non_number_keys(processed.conf, root) + + return processed +end + local Tree = {} Tree.__index = Tree @@ -76,6 +83,7 @@ function Tree.new(tree_syntax) end menu.preprocess = preprocess +menu.preprocess_root = preprocess_root menu.Tree = Tree -- helper function to recurse with more arguments @@ -249,16 +257,12 @@ local tree2 = { { "top level leaf", "another top level leaf", - ["a node"] = { - { - "second level leaf", - "another second level leaf", - } - } + ["a node"] = {{ + "second level leaf", + "another second level leaf" + }} }, + title = 'root title' } -local res = preprocess(tree2) -dump(res) - return menu diff --git a/lua/tests/automated/tree_spec.lua b/lua/tests/automated/tree_spec.lua index 1f24c28285..20257b3fee 100644 --- a/lua/tests/automated/tree_spec.lua +++ b/lua/tests/automated/tree_spec.lua @@ -1,4 +1,9 @@ local preprocess = require('telescope.builtin.menu').preprocess +local preprocess_root = require('telescope.builtin.menu').preprocess_root + +local eq = function(a, b) + assert.are.same(a, b) +end local _eq = function(a, b) b = preprocess(b) @@ -15,17 +20,14 @@ describe('preprocess function', function() } local expected = { - { - {leaf = "top level leaf", conf = {}}, - {leaf = "another top level leaf", conf = {}}, - }, - conf = {} + {leaf = "top level leaf", conf = {}}, + {leaf = "another top level leaf", conf = {}}, } _eq(expected, tree) end) - it('should preprocess with configuration', function() + it('should preprocess with configuration in the root', function() local tree = { { "top level leaf", @@ -36,7 +38,8 @@ describe('preprocess function', function() } local expected = { - { + branch_name = "root", + branches = { {leaf = "top level leaf", conf = {}}, {leaf = "another top level leaf", conf = {}}, }, @@ -46,7 +49,9 @@ describe('preprocess function', function() } } - _eq(expected, tree) + local res = preprocess_root(tree) + + eq(expected, res) end) it('should preprocess with two levels', function() @@ -64,7 +69,6 @@ describe('preprocess function', function() } local expected = { - { {leaf = "top level leaf", conf = {}}, {leaf = "another top level leaf", conf = {}}, { @@ -76,11 +80,11 @@ describe('preprocess function', function() }, conf = {} }, - }, - conf = {} } - _eq(expected, tree) + local res = preprocess(tree) + + eq(expected, res) end) it('should preprocess with two levels and conf', function() @@ -106,77 +110,13 @@ describe('preprocess function', function() {leaf = "second level leaf", conf = {}}, {leaf = "another second level leaf", conf = {}}, {leaf = "..", conf = {}}, - conf = {} }, + conf = {} }, - conf = {} - } - - -- _eq(expected, tree) - end) - - it('should preprocess the tree correctly with only one level', function() - local tree = { - { - "top level leaf", - "another top level leaf", - ["a node"] = { - { - { - "second level leaf with a specific callback different", - description = 'this is a description', - callback = function() - print("this is a specific callback") - end, - }, - "another leaf", - }, - title = 'second level title' - } - }, - title = 'test menu', - callback = function(selections) - for _, selection in pairs(selections) do - print(selection) - end - end } local res = preprocess(tree) - local expected = { - { - {leaf = "top level leaf"}, - {leaf = "another top level leaf"}, - { - branch_name = "a node", - branches = { - { - leaf = "second level leaf with a specific callback different", - conf = { - description = 'this is a description', - callback = function() - print("this is a specific callback") - end, - } - }, - "another leaf", - }, - conf = { - title = 'second level title' - } - } - }, - conf = { - title = 'test menu', - callback = function(selections) - for _, selection in pairs(selections) do - print(selection) - end - end - } - } - - -- eq(res, expected) + eq(expected, res) end) end) From 5c8c6af3d77a9df40d9536ab0ea0248baab04748 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sat, 19 Dec 2020 11:53:36 -0500 Subject: [PATCH 23/38] cleaned up --- lua/telescope/builtin/menu.lua | 72 +++++-------------------------- lua/tests/automated/tree_spec.lua | 18 +++----- 2 files changed, 17 insertions(+), 73 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 999a613d62..38e9757d0a 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -19,7 +19,7 @@ end -- takes node syntax and processes it into internal representation, -- does no fully preprocess root, as that is a special node without a key of string, it is just there -local function preprocess(node) +local function process(node) local results = {} for key, value in pairs(node[1]) do @@ -45,7 +45,7 @@ local function preprocess(node) elseif type(key) == "string" then -- node table.insert(value[1], "..") - local preprocessed = {branch_name = key, branches = preprocess(value)} + local preprocessed = {branch_name = key, branches = process(value)} preprocessed.conf = {} extend_non_number_keys(preprocessed.conf, value) table.insert(results, preprocessed) -- recurse until we hit a leaf @@ -58,8 +58,8 @@ local function preprocess(node) return results end -local function preprocess_root(root) - local processed = {branches = preprocess(root), branch_name = "root"} +local function process_root(root) + local processed = {branches = process(root), branch_name = "root"} processed.conf = {} extend_non_number_keys(processed.conf, root) @@ -75,15 +75,13 @@ end function Tree:display() end --- will mutate consume the tree_syntax function Tree.new(tree_syntax) - local tree = preprocess(tree_syntax) - local root = {branch_name = "root", branches = tree} - return root + local root = process_root(tree_syntax) + return setmetatable(root, Tree) end -menu.preprocess = preprocess -menu.preprocess_root = preprocess_root +menu.process = process +menu.process_root = process_root menu.Tree = Tree -- helper function to recurse with more arguments @@ -140,7 +138,7 @@ local function go(tree, opts, remember, selections) else -- it is a node dump(entry.value) - local processed_value = preprocess(entry.value) + local processed_value = process(entry.value) dump(processed_value) table.insert(remember, processed_value) table.insert(selections, processed_value) @@ -161,7 +159,7 @@ end -- entry point for menu menu.open = function(opts) opts = opts or {} - opts.tree = preprocess(opts.tree) + opts.tree = process(opts.tree) local selections = {} @@ -215,54 +213,4 @@ menu.test = function() } end -local tree = { - { - "top level leaf", - "another top level leaf", - ["a node"] = { - -- instead of directly setting the key to the value, we set it to a table of options, [1] is the contents - { - "second level leaf", - "another second level leaf", - ["second level node"] = { - { - -- vs this way, we can have multiple options - { - "third level leaf with a specific callback different", - -- for example we might want to set the description - description = 'this is a description', -- not implemented yet - callback = function() - print("this is a specific callback") - end, - }, - "third level leaf", - }, - -- because it is a table inside of a table, we can set options as the contents of the tree will be node[1] - -- other options will be anything except [1] - title = 'this title overrides the defualt one' - } - }, - title = 'second level title' - } - }, - title = 'test menu', - callback = function(selections) - for _, selection in pairs(selections) do - print(selection) - end - end -} - -local tree2 = { - { - "top level leaf", - "another top level leaf", - ["a node"] = {{ - "second level leaf", - "another second level leaf" - }} - }, - title = 'root title' -} - return menu diff --git a/lua/tests/automated/tree_spec.lua b/lua/tests/automated/tree_spec.lua index 20257b3fee..5d2eb50cec 100644 --- a/lua/tests/automated/tree_spec.lua +++ b/lua/tests/automated/tree_spec.lua @@ -1,15 +1,10 @@ -local preprocess = require('telescope.builtin.menu').preprocess -local preprocess_root = require('telescope.builtin.menu').preprocess_root +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 -local _eq = function(a, b) - b = preprocess(b) - assert.are.same(a, b) -end - describe('preprocess function', function() it('should preprocess simple', function() local tree = { @@ -24,7 +19,8 @@ describe('preprocess function', function() {leaf = "another top level leaf", conf = {}}, } - _eq(expected, tree) + local res = process(tree) + eq(expected, res) end) it('should preprocess with configuration in the root', function() @@ -49,7 +45,7 @@ describe('preprocess function', function() } } - local res = preprocess_root(tree) + local res = process_root(tree) eq(expected, res) end) @@ -82,7 +78,7 @@ describe('preprocess function', function() }, } - local res = preprocess(tree) + local res = process(tree) eq(expected, res) end) @@ -115,7 +111,7 @@ describe('preprocess function', function() }, } - local res = preprocess(tree) + local res = process(tree) eq(expected, res) end) From 40ab3f95c897ce0f7f46e5362eaf633ebe5ec85c Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Sat, 19 Dec 2020 12:54:11 -0500 Subject: [PATCH 24/38] fixed make entry and updated example --- lua/telescope/builtin/menu.lua | 55 +++++++++++++--------------------- lua/telescope/make_entry.lua | 12 +++----- 2 files changed, 25 insertions(+), 42 deletions(-) diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua index 38e9757d0a..af22cc597a 100644 --- a/lua/telescope/builtin/menu.lua +++ b/lua/telescope/builtin/menu.lua @@ -93,9 +93,9 @@ local function go(tree, opts, remember, selections) opts.entry_maker = opts.entry_maker or make_entry.gen_from_node(opts) pickers.new(opts, { - prompt_title = tree.title or root.title, + prompt_title = tree.conf.title or root.conf.title, finder = finders.new_table { - results = tree, + results = tree.branches, entry_maker = opts.entry_maker }, sorter = conf.generic_sorter(opts), @@ -112,7 +112,7 @@ local function go(tree, opts, remember, selections) if entry.is_leaf then -- .. means go back - if entry.value == ".." then + if entry.leaf == ".." then -- first pop is the one you are currently in table.remove(remember) -- the last one is the last tree selected, but do not pop off, @@ -129,22 +129,18 @@ local function go(tree, opts, remember, selections) return end - table.insert(remember, entry.value) - table.insert(selections, entry.value) + table.insert(remember, entry) + table.insert(selections, entry.leaf) - dump(root) - local callback = entry.callback or root.callback + local callback = entry.conf.callback or root.conf.callback callback(selections) else -- it is a node - dump(entry.value) - local processed_value = process(entry.value) - dump(processed_value) - table.insert(remember, processed_value) - table.insert(selections, processed_value) + table.insert(remember, entry) + table.insert(selections, entry.branch_name) -- recurse - go(processed_value, opts, remember, selections) + go(entry, opts, remember, selections) -- sometimes does not start insert for some reason vim.api.nvim_input('i') @@ -159,15 +155,13 @@ end -- entry point for menu menu.open = function(opts) opts = opts or {} - opts.tree = process(opts.tree) + opts.tree = process_root(opts.tree) local selections = {} local remember = {} table.insert(remember, opts.tree) - dump(opts) - go(opts.tree, opts, remember, selections) end @@ -175,41 +169,34 @@ menu.test = function() menu.open { tree = { { - "top level leaf", - "another top level leaf", + "hello dude", ["a node"] = { - -- instead of directly setting the key to the value, we set it to a table of options, [1] is the contents { - "second level leaf", - "another second level leaf", + "a leaf inside of the node", + "another leaf inside of node", ["second level node"] = { { - -- vs this way, we can have multiple options + "leaf inside second level node", { - "third level leaf with a specific callback different", - -- for example we might want to set the description - description = 'this is a description', -- not implemented yet + "another leaf inside second level node with specific callback", callback = function() - print("this is a specific callback") + print('this is a specific callback') end, }, - "third level leaf", }, - -- because it is a table inside of a table, we can set options as the contents of the tree will be node[1] - -- other options will be anything except [1] - title = 'this title overrides the defualt one' + title = 'this overrides the root title' } }, - title = 'second level title' + -- no need to speecify title here, it is propagated from the root } }, title = 'test menu', callback = function(selections) - for _, selection in pairs(selections) do + for _, selection in ipairs(selections) do print(selection) end - end - }, + end, + } } end diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 5f3b3359a3..354f35e15b 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -60,15 +60,11 @@ end function make_entry.gen_from_node(_) return function(node) local display = node.leaf or node.branch_name - local value = node.branches or node.leaf - local is_leaf = node.leaf and true or false + node.display = display + node.ordinal = display + node.is_leaf = node.leaf and true or false - return { - value = value, - display = display, - ordinal = display, - is_leaf = is_leaf, - } + return node end end From 034b54d2245433c5ffd831463e580accb59b89d2 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 08:15:04 -0400 Subject: [PATCH 25/38] started --- lua/telescope/finders.lua | 185 +++++++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 1 deletion(-) diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index d2acd0517c..064aadd02b 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -20,7 +20,6 @@ local _callable_obj = function() return obj end - --[[ ============================================================= JobFinder @@ -108,6 +107,190 @@ function JobFinder:_find(prompt, process_result, process_complete) self.job:start() end +local LiveFinder = _callable_obj() + +function LiveFinder: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({ + fn = opts.fn, + entry_maker = opts.entry_maker or make_entry.from_string, + fn_command = opts.fn_command, + cwd = opts.cwd, + writer = opts.writer, + + -- Maximum number of results to process. + -- Particularly useful for live updating large queries. + maximum_results = opts.maximum_results, + }, self) + + return obj +end + +function LiveFinder:_find(opts) +end + +local OneshotJobFinder = _callable_obj() + +function OneshotJobFinder: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({ + fn_command = opts.fn_command, + entry_maker = opts.entry_maker or make_entry.from_string, + + cwd = opts.cwd, + writer = opts.writer, + + maximum_results = opts.maximum_results, + + _started = false, + }, self) + + obj._find = coroutine.wrap(function(finder, _, process_result, process_complete) + local num_execution = 1 + local num_results = 0 + + local results = setmetatable({}, { + __newindex = function(t, k, v) + rawset(t, k, v) + process_result(v) + end + }) + + local job_opts = finder:fn_command(_) + if not job_opts then + error(debug.traceback("expected `job_opts` from fn_command")) + end + + local writer = nil + if job_opts.writer and Job.is_job(job_opts.writer) then + writer = job_opts.writer + elseif job_opts.writer then + writer = Job:new(job_opts.writer) + end + + local on_output = function(_, line) + -- This will call the metamethod, process_result + num_results = num_results + 1 + results[num_results] = finder.entry_maker(line) + end + + local completed = false + local job = Job:new { + command = job_opts.command, + args = job_opts.args, + cwd = job_opts.cwd or finder.cwd, + + maximum_results = finder.maximum_results, + + writer = writer, + + enable_recording = false, + + on_stdout = on_output, + on_stderr = on_output, + + on_exit = function() + process_complete() + completed = true + end, + } + + job:start() + + while true do + finder, _, process_result, process_complete = coroutine.yield() + num_execution = num_execution + 1 + + local current_count = num_results + for index = 1, current_count do + process_result(results[index]) + end + + if completed then + process_complete() + end + end + end) + + return obj +end + +function OneshotJobFinder:old_find(_, process_result, process_complete) + local first_run = false + + if not self._started then + first_run = true + + self._started = true + + end + + -- First time we get called, start on up that job. + -- Every time after that, just use the results LUL + if not first_run then + return + end +end + + + +--[[ ============================================================= +Static Finders + +A static finder has results that never change. +They are passed in directly as a result. +-- ============================================================= ]] +local StaticFinder = _callable_obj() + +function StaticFinder:new(opts) + assert(opts, "Options are required. See documentation for usage") + + local input_results + if vim.tbl_islist(opts) then + input_results = opts + else + input_results = opts.results + end + + local entry_maker = opts.entry_maker or make_entry.gen_from_string() + + assert(input_results) + assert(input_results, "Results are required for static finder") + assert(type(input_results) == 'table', "self.results must be a table") + + local results = {} + for k, v in ipairs(input_results) do + local entry = entry_maker(v) + + if entry then + entry.index = k + table.insert(results, entry) + end + end + + return setmetatable({ results = results }, self) +end + +function StaticFinder:_find(_, process_result, process_complete) + for _, v in ipairs(self.results) do + process_result(v) + end + + process_complete() +end + + +-- local + + --- Return a new Finder -- -- Use at your own risk. From a90f7213ece0085fb7eba25f39439a81ed6cb6e9 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 08:16:08 -0400 Subject: [PATCH 26/38] added some stuff --- lua/telescope/builtin/init.lua | 1 + lua/telescope/builtin/lsp.lua | 55 ++++++++++++++++++++++++++++++++++ lua/telescope/finders.lua | 21 ++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 2b3c1cacdd..d1809bde56 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -70,5 +70,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_live_workspace_symbols = require('telescope.builtin.lsp').live_workspace_symbols return builtin diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 741eb364b9..5493457300 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,58 @@ lsp.workspace_symbols = function(opts) }):find() end +local function get_workspace_symbols_requester() + local cancel = function() end + + return async(function(prompt_bufnr, prompt) + -- cancel() + + local tx, rx = channel.oneshot() + cancel = vim.lsp.buf_request(prompt_bufnr, "workspace/symbol", {query = prompt}, tx) + + local err, _, results_lsp = await(rx()) + assert(not err, err) + -- dump(results_lsp) + -- assert(not err, err) + -- dump(await(rx())) + + local locations = vim.lsp.util.symbols_to_items(results_lsp, prompt_bufnr) or {} + -- dump(locations) + -- local locations vim.tbl_map(function(result) + -- return + -- end, results_lsp) + -- dump(locations) + -- return locations + return locations + -- local locations = {} + -- for _, server_results in pairs(results_lsp) do + -- if server_results.result then + -- vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {}) + -- end + -- end + + -- return locations + end) +end + +lsp.live_workspace_symbols = function(opts) + local curr_buf = vim.api.nvim_get_current_buf() + + pickers.new(opts, { + prompt_title = 'LSP Workspace Symbols', + finder = finders.new_live { + curr_buf = curr_buf, + entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), + fn = get_workspace_symbols_requester(), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.prefilter_sorter{ + tag = "symbol_type", + sorter = conf.generic_sorter(opts) + } + }):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 064aadd02b..96b62fd740 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, 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') @@ -116,6 +118,7 @@ function LiveFinder:new(opts) 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, fn_command = opts.fn_command, @@ -130,7 +133,19 @@ function LiveFinder:new(opts) return obj end -function LiveFinder:_find(opts) +function LiveFinder:_find(prompt, process_result, process_complete) + local fn = async(function() + local results = await(self.fn(self.curr_buf, prompt)) + for _, result in ipairs(results) do + dump("processing result", result) + process_result(result) + end + + await(a.scheduler()) + process_complete() + end) + + a.util.run(fn()) end local OneshotJobFinder = _callable_obj() @@ -368,4 +383,8 @@ finders.new_table = function(t) return async_static_finder(t) end +finders.new_live = function(t) + return LiveFinder:new(t) +end + return finders From e157ee76c2d4c0fbaa5242ee3e0e5aae9d448113 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 30 Mar 2021 14:34:08 -0400 Subject: [PATCH 27/38] deleted uneeded stuff --- lua/telescope/builtin/lsp.lua | 30 ++++++------------------------ lua/telescope/finders.lua | 14 ++------------ 2 files changed, 8 insertions(+), 36 deletions(-) diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 5493457300..19a0b52dcc 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -225,33 +225,14 @@ local function get_workspace_symbols_requester() local cancel = function() end return async(function(prompt_bufnr, prompt) - -- cancel() - local tx, rx = channel.oneshot() cancel = vim.lsp.buf_request(prompt_bufnr, "workspace/symbol", {query = prompt}, tx) local err, _, results_lsp = await(rx()) assert(not err, err) - -- dump(results_lsp) - -- assert(not err, err) - -- dump(await(rx())) local locations = vim.lsp.util.symbols_to_items(results_lsp, prompt_bufnr) or {} - -- dump(locations) - -- local locations vim.tbl_map(function(result) - -- return - -- end, results_lsp) - -- dump(locations) - -- return locations return locations - -- local locations = {} - -- for _, server_results in pairs(results_lsp) do - -- if server_results.result then - -- vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {}) - -- end - -- end - - -- return locations end) end @@ -259,17 +240,18 @@ lsp.live_workspace_symbols = function(opts) local curr_buf = vim.api.nvim_get_current_buf() pickers.new(opts, { - prompt_title = 'LSP Workspace Symbols', + prompt_title = 'LSP Live Workspace Symbols', finder = finders.new_live { curr_buf = curr_buf, entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), fn = get_workspace_symbols_requester(), }, previewer = conf.qflist_previewer(opts), - sorter = conf.prefilter_sorter{ - tag = "symbol_type", - sorter = conf.generic_sorter(opts) - } + sorter = conf.generic_sorter() + -- sorter = conf.prefilter_sorter{ + -- tag = "symbol_type", + -- sorter = conf.generic_sorter(opts) + -- } }):find() end diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 96b62fd740..df9ee95055 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -121,31 +121,21 @@ function LiveFinder:new(opts) curr_buf = opts.curr_buf, fn = opts.fn, entry_maker = opts.entry_maker or make_entry.from_string, - fn_command = opts.fn_command, - cwd = opts.cwd, - writer = opts.writer, - - -- Maximum number of results to process. - -- Particularly useful for live updating large queries. - maximum_results = opts.maximum_results, }, self) return obj end function LiveFinder:_find(prompt, process_result, process_complete) - local fn = async(function() + a.scope(function() local results = await(self.fn(self.curr_buf, prompt)) for _, result in ipairs(results) do - dump("processing result", result) - process_result(result) + process_result(self.entry_maker(result)) end await(a.scheduler()) process_complete() end) - - a.util.run(fn()) end local OneshotJobFinder = _callable_obj() From 0775eba1db62f794fd362537ecaa2a044341d610 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 08:37:04 -0400 Subject: [PATCH 28/38] added cancelable --- lua/telescope/finders.lua | 6 +++++- lua/telescope/finders/async_oneshot_finder.lua | 10 ++++++++++ lua/telescope/pickers.lua | 8 +++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index df9ee95055..2271d16c1f 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -126,10 +126,14 @@ function LiveFinder:new(opts) return obj end -function LiveFinder:_find(prompt, process_result, process_complete) +function LiveFinder:_find(prompt, process_result, process_complete, status) a.scope(function() local results = await(self.fn(self.curr_buf, prompt)) + for _, result in ipairs(results) do + if status.should_stop then + return + end process_result(self.entry_maker(result)) end diff --git a/lua/telescope/finders/async_oneshot_finder.lua b/lua/telescope/finders/async_oneshot_finder.lua index 2345096798..b9af7b955e 100644 --- a/lua/telescope/finders/async_oneshot_finder.lua +++ b/lua/telescope/finders/async_oneshot_finder.lua @@ -9,6 +9,16 @@ local make_entry = require('telescope.make_entry') local Job = require('plenary.job') +local get_printer = function(message) + local counter = 0 + return function() + print(string.format("%s: The counte is %s", message, counter)) + counter = counter + 1 + end +end + +local printer = get_printer("should stop") + return function(opts) opts = opts or {} diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 7e658c3682..c2782b951f 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -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()) @@ -382,6 +383,9 @@ function Picker:find() return end + status.should_stop = true + status = {should_stop = false} + if not first_line then first_line = 0 end if not last_line then last_line = 1 end @@ -413,7 +417,7 @@ function Picker:find() local process_complete = self:get_result_completor(self.results_bufnr, find_id, prompt, status_updater) local ok, msg = pcall(function() - self.finder(prompt, process_result, vim.schedule_wrap(process_complete)) + self.finder(prompt, process_result, vim.schedule_wrap(process_complete), status) end) if not ok then @@ -437,6 +441,8 @@ function Picker:find() self.closed = true + status.should_stop = true + -- TODO: Should we actually do this? collectgarbage(); collectgarbage() end, From 4ba827adc838dbf50392a98d78bc1ea29782b9cc Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 09:06:14 -0400 Subject: [PATCH 29/38] changed workspace requester --- lua/telescope/builtin/init.lua | 2 +- lua/telescope/builtin/lsp.lua | 21 ++++++++------------- lua/telescope/finders.lua | 10 +++++----- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index d1809bde56..abe39eb6b6 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -70,6 +70,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_live_workspace_symbols = require('telescope.builtin.lsp').live_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 19a0b52dcc..d28f535e70 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -221,37 +221,32 @@ lsp.workspace_symbols = function(opts) }):find() end -local function get_workspace_symbols_requester() +local function get_workspace_symbols_requester(bufnr) local cancel = function() end - return async(function(prompt_bufnr, prompt) + return async(function(prompt) local tx, rx = channel.oneshot() - cancel = vim.lsp.buf_request(prompt_bufnr, "workspace/symbol", {query = prompt}, tx) + 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, prompt_bufnr) or {} + local locations = vim.lsp.util.symbols_to_items(results_lsp, bufnr) or {} return locations end) end -lsp.live_workspace_symbols = function(opts) - local curr_buf = vim.api.nvim_get_current_buf() +lsp.dynamic_workspace_symbols = function(opts) + local curr_bufnr = vim.api.nvim_get_current_buf() pickers.new(opts, { - prompt_title = 'LSP Live Workspace Symbols', + prompt_title = 'LSP Dynamic Workspace Symbols', finder = finders.new_live { - curr_buf = curr_buf, entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), - fn = get_workspace_symbols_requester(), + fn = get_workspace_symbols_requester(curr_bufnr), }, previewer = conf.qflist_previewer(opts), sorter = conf.generic_sorter() - -- sorter = conf.prefilter_sorter{ - -- tag = "symbol_type", - -- sorter = conf.generic_sorter(opts) - -- } }):find() end diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 2271d16c1f..ec2762ca94 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -109,9 +109,9 @@ function JobFinder:_find(prompt, process_result, process_complete) self.job:start() end -local LiveFinder = _callable_obj() +local DynamicFinder = _callable_obj() -function LiveFinder:new(opts) +function DynamicFinder:new(opts) opts = opts or {} assert(not opts.results, "`results` should be used with finder.new_table") @@ -126,9 +126,9 @@ function LiveFinder:new(opts) return obj end -function LiveFinder:_find(prompt, process_result, process_complete, status) +function DynamicFinder:_find(prompt, process_result, process_complete, status) a.scope(function() - local results = await(self.fn(self.curr_buf, prompt)) + local results = await(self.fn(prompt)) for _, result in ipairs(results) do if status.should_stop then @@ -378,7 +378,7 @@ finders.new_table = function(t) end finders.new_live = function(t) - return LiveFinder:new(t) + return DynamicFinder:new(t) end return finders From 50fb0b7076cc3b74262407c22f11310440fb7a33 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 12:21:10 -0400 Subject: [PATCH 30/38] use better cancellation mechanism --- lua/telescope/finders.lua | 8 ++------ lua/telescope/pickers.lua | 5 +---- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index ec2762ca94..2fef98a7c1 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -126,18 +126,14 @@ function DynamicFinder:new(opts) return obj end -function DynamicFinder:_find(prompt, process_result, process_complete, status) +function DynamicFinder:_find(prompt, process_result, process_complete) a.scope(function() local results = await(self.fn(prompt)) for _, result in ipairs(results) do - if status.should_stop then - return - end - process_result(self.entry_maker(result)) + if process_result(self.entry_maker(result)) then return end end - await(a.scheduler()) process_complete() end) end diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index c2782b951f..c90d37e4f9 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -383,9 +383,6 @@ function Picker:find() return end - status.should_stop = true - status = {should_stop = false} - if not first_line then first_line = 0 end if not last_line then last_line = 1 end @@ -417,7 +414,7 @@ function Picker:find() local process_complete = self:get_result_completor(self.results_bufnr, find_id, prompt, status_updater) local ok, msg = pcall(function() - self.finder(prompt, process_result, vim.schedule_wrap(process_complete), status) + self.finder(prompt, process_result, vim.schedule_wrap(process_complete)) end) if not ok then From 261d38abd0bb4439fe1ad038fa19c8d420b86f28 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 12:27:30 -0400 Subject: [PATCH 31/38] removed accidental stuff --- lua/telescope/builtin/init.lua | 3 - lua/telescope/builtin/menu.lua | 203 --------------------------------- lua/telescope/finders.lua | 162 -------------------------- 3 files changed, 368 deletions(-) delete mode 100644 lua/telescope/builtin/menu.lua diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 55b13dea11..b9cc99f7c9 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -40,9 +40,6 @@ builtin.git_branches = require('telescope.builtin.git').branches builtin.git_status = require('telescope.builtin.git').status builtin.builtin = require('telescope.builtin.internal').builtin -builtin.menu = require('telescope.builtin.menu').open -builtin.test_menu = require('telescope.builtin.menu').test -builtin.Node = require('telescope.builtin.menu').Node builtin.planets = require('telescope.builtin.internal').planets builtin.symbols = require('telescope.builtin.internal').symbols diff --git a/lua/telescope/builtin/menu.lua b/lua/telescope/builtin/menu.lua deleted file mode 100644 index af22cc597a..0000000000 --- a/lua/telescope/builtin/menu.lua +++ /dev/null @@ -1,203 +0,0 @@ -local api = vim.api -local actions = require('telescope.actions') -local finders = require('telescope.finders') -local make_entry = require('telescope.make_entry') -local pickers = require('telescope.pickers') - -local conf = require('telescope.config').values - -local menu = {} - ---- will mutate table -local function extend_non_number_keys(table, add) - for k, v in pairs(add) do - if type(k) == "string" then - table[k] = v - end - end -end - --- takes node syntax and processes it into internal representation, --- does no fully preprocess root, as that is a special node without a key of string, it is just there -local function process(node) - local results = {} - - for key, value in pairs(node[1]) do - if type(key) == "number" then -- leaf - - if type(value) == "string" then - - table.insert(results, {leaf = value, conf = {}}) - - elseif type(value) == "table" then - -- its a leaf with a specific callback and other options - - local processed = {leaf = value[1]} - -- all non numbers keys are conf for the leaf - processed.conf = {} - extend_non_number_keys(processed.conf, value) - table.insert(results, processed) - - else - error "BUG: should not get here" - end - - elseif type(key) == "string" then -- node - - table.insert(value[1], "..") - local preprocessed = {branch_name = key, branches = process(value)} - preprocessed.conf = {} - extend_non_number_keys(preprocessed.conf, value) - table.insert(results, preprocessed) -- recurse until we hit a leaf - - else - error "BUG: should not get here" - end - end - - return results -end - -local function process_root(root) - local processed = {branches = process(root), branch_name = "root"} - processed.conf = {} - extend_non_number_keys(processed.conf, root) - - return processed -end - -local Tree = {} -Tree.__index = Tree - -function Tree:iter() -end - -function Tree:display() -end - -function Tree.new(tree_syntax) - local root = process_root(tree_syntax) - return setmetatable(root, Tree) -end - -menu.process = process -menu.process_root = process_root -menu.Tree = Tree - --- helper function to recurse with more arguments --- remember contains the actual tree that is remembered so we can use it with .. --- selections contains only the display so we can pass it into the callback -local function go(tree, opts, remember, selections) - local root = remember[1] - - opts.entry_maker = opts.entry_maker or make_entry.gen_from_node(opts) - - pickers.new(opts, { - prompt_title = tree.conf.title or root.conf.title, - finder = finders.new_table { - results = tree.branches, - entry_maker = opts.entry_maker - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions._goto_file_selection:replace(function() - actions.close(prompt_bufnr) - - local entry = actions.get_selected_entry() - - if entry == nil then - error "No entry selected" - end - - if entry.is_leaf then - - -- .. means go back - if entry.leaf == ".." then - -- first pop is the one you are currently in - table.remove(remember) - -- the last one is the last tree selected, but do not pop off, - -- we want it to be available for the next prompt - local last = remember[#remember] - - -- pop current selection because we went back - table.remove(selections) - - -- no need to process because it is the last one - go(last, opts, remember, selections) - - api.nvim_input('i') - return - end - - table.insert(remember, entry) - table.insert(selections, entry.leaf) - - local callback = entry.conf.callback or root.conf.callback - callback(selections) - else - -- it is a node - table.insert(remember, entry) - table.insert(selections, entry.branch_name) - - -- recurse - go(entry, opts, remember, selections) - - -- sometimes does not start insert for some reason - vim.api.nvim_input('i') - end - end) - - return true - end, - }):find() -end - --- entry point for menu -menu.open = function(opts) - opts = opts or {} - opts.tree = process_root(opts.tree) - - local selections = {} - - local remember = {} - table.insert(remember, opts.tree) - - go(opts.tree, opts, remember, selections) -end - -menu.test = function() - menu.open { - tree = { - { - "hello dude", - ["a node"] = { - { - "a leaf inside of the node", - "another leaf inside of node", - ["second level node"] = { - { - "leaf inside second level node", - { - "another leaf inside second level node with specific callback", - callback = function() - print('this is a specific callback') - end, - }, - }, - title = 'this overrides the root title' - } - }, - -- no need to speecify title here, it is propagated from the root - } - }, - title = 'test menu', - callback = function(selections) - for _, selection in ipairs(selections) do - print(selection) - end - end, - } - } -end - -return menu diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 3ec3ee4982..3021359919 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -138,164 +138,6 @@ function DynamicFinder:_find(prompt, process_result, process_complete) end) end -local OneshotJobFinder = _callable_obj() - -function OneshotJobFinder: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({ - fn_command = opts.fn_command, - entry_maker = opts.entry_maker or make_entry.from_string, - - cwd = opts.cwd, - writer = opts.writer, - - maximum_results = opts.maximum_results, - - _started = false, - }, self) - - obj._find = coroutine.wrap(function(finder, _, process_result, process_complete) - local num_execution = 1 - local num_results = 0 - - local results = setmetatable({}, { - __newindex = function(t, k, v) - rawset(t, k, v) - process_result(v) - end - }) - - local job_opts = finder:fn_command(_) - if not job_opts then - error(debug.traceback("expected `job_opts` from fn_command")) - end - - local writer = nil - if job_opts.writer and Job.is_job(job_opts.writer) then - writer = job_opts.writer - elseif job_opts.writer then - writer = Job:new(job_opts.writer) - end - - local on_output = function(_, line) - -- This will call the metamethod, process_result - num_results = num_results + 1 - results[num_results] = finder.entry_maker(line) - end - - local completed = false - local job = Job:new { - command = job_opts.command, - args = job_opts.args, - cwd = job_opts.cwd or finder.cwd, - - maximum_results = finder.maximum_results, - - writer = writer, - - enable_recording = false, - - on_stdout = on_output, - on_stderr = on_output, - - on_exit = function() - process_complete() - completed = true - end, - } - - job:start() - - while true do - finder, _, process_result, process_complete = coroutine.yield() - num_execution = num_execution + 1 - - local current_count = num_results - for index = 1, current_count do - process_result(results[index]) - end - - if completed then - process_complete() - end - end - end) - - return obj -end - -function OneshotJobFinder:old_find(_, process_result, process_complete) - local first_run = false - - if not self._started then - first_run = true - - self._started = true - - end - - -- First time we get called, start on up that job. - -- Every time after that, just use the results LUL - if not first_run then - return - end -end - - - ---[[ ============================================================= -Static Finders - -A static finder has results that never change. -They are passed in directly as a result. --- ============================================================= ]] -local StaticFinder = _callable_obj() - -function StaticFinder:new(opts) - assert(opts, "Options are required. See documentation for usage") - - local input_results - if vim.tbl_islist(opts) then - input_results = opts - else - input_results = opts.results - end - - local entry_maker = opts.entry_maker or make_entry.gen_from_string() - - assert(input_results) - assert(input_results, "Results are required for static finder") - assert(type(input_results) == 'table', "self.results must be a table") - - local results = {} - for k, v in ipairs(input_results) do - local entry = entry_maker(v) - - if entry then - entry.index = k - table.insert(results, entry) - end - end - - return setmetatable({ results = results }, self) -end - -function StaticFinder:_find(_, process_result, process_complete) - for _, v in ipairs(self.results) do - process_result(v) - end - - process_complete() -end - - --- local - - --- Return a new Finder -- -- Use at your own risk. @@ -377,8 +219,4 @@ finders.new_live = function(t) return DynamicFinder:new(t) end -finders.new_tree = function(t) - return StaticFinder:new_tree(t) -end - return finders From b8f67e075a4214803408a583b7fbafa75f20cfd2 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 12:28:25 -0400 Subject: [PATCH 32/38] removed useless print --- lua/telescope/finders/async_oneshot_finder.lua | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lua/telescope/finders/async_oneshot_finder.lua b/lua/telescope/finders/async_oneshot_finder.lua index b9af7b955e..2345096798 100644 --- a/lua/telescope/finders/async_oneshot_finder.lua +++ b/lua/telescope/finders/async_oneshot_finder.lua @@ -9,16 +9,6 @@ local make_entry = require('telescope.make_entry') local Job = require('plenary.job') -local get_printer = function(message) - local counter = 0 - return function() - print(string.format("%s: The counte is %s", message, counter)) - counter = counter + 1 - end -end - -local printer = get_printer("should stop") - return function(opts) opts = opts or {} From c355bb8d2603a2b3a05c58e9961786ac174014f1 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 12:29:37 -0400 Subject: [PATCH 33/38] delete more useless stuff --- lua/telescope/make_entry.lua | 11 --- lua/telescope/pickers.lua | 3 - lua/tests/automated/tree_spec.lua | 118 ------------------------------ 3 files changed, 132 deletions(-) delete mode 100644 lua/tests/automated/tree_spec.lua diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 1b4b4455ed..b9635bb10b 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -58,17 +58,6 @@ 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, diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index c90d37e4f9..7e658c3682 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -370,7 +370,6 @@ 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()) @@ -438,8 +437,6 @@ function Picker:find() self.closed = true - status.should_stop = true - -- TODO: Should we actually do this? collectgarbage(); collectgarbage() end, diff --git a/lua/tests/automated/tree_spec.lua b/lua/tests/automated/tree_spec.lua deleted file mode 100644 index 5d2eb50cec..0000000000 --- a/lua/tests/automated/tree_spec.lua +++ /dev/null @@ -1,118 +0,0 @@ -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) From 8312c90043324641f8883c35b2e176d84b1801ef Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 12:32:29 -0400 Subject: [PATCH 34/38] rename to dynamic --- lua/telescope/builtin/lsp.lua | 2 +- lua/telescope/finders.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index d28f535e70..61704baafb 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -241,7 +241,7 @@ lsp.dynamic_workspace_symbols = function(opts) pickers.new(opts, { prompt_title = 'LSP Dynamic Workspace Symbols', - finder = finders.new_live { + finder = finders.new_dynamic { entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), fn = get_workspace_symbols_requester(curr_bufnr), }, diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 3021359919..5d423c2650 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -215,7 +215,7 @@ finders.new_table = function(t) return async_static_finder(t) end -finders.new_live = function(t) +finders.new_dynamic = function(t) return DynamicFinder:new(t) end From 8bc676da08d9a94c28c3d630f9529263c900b2a1 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 12:38:07 -0400 Subject: [PATCH 35/38] added request cancellation --- lua/telescope/builtin/lsp.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 61704baafb..e678d964e9 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -226,7 +226,8 @@ local function get_workspace_symbols_requester(bufnr) return async(function(prompt) local tx, rx = channel.oneshot() - cancel = vim.lsp.buf_request(bufnr, "workspace/symbol", {query = prompt}, tx) + cancel() + _, cancel = vim.lsp.buf_request(bufnr, "workspace/symbol", {query = prompt}, tx) local err, _, results_lsp = await(rx()) assert(not err, err) From e935fbf10d3d1651422af9e1ca64c7fd37abfbab Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 14:19:10 -0400 Subject: [PATCH 36/38] CHECK IF NIL --- lua/telescope/builtin/lsp.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index e678d964e9..b2ab981443 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -232,7 +232,7 @@ local function get_workspace_symbols_requester(bufnr) local err, _, results_lsp = await(rx()) assert(not err, err) - local locations = vim.lsp.util.symbols_to_items(results_lsp, bufnr) or {} + local locations = vim.lsp.util.symbols_to_items(results_lsp or {}, bufnr) or {} return locations end) end From 59ade7291b97d86d6412353eac1f0cd5148959d6 Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 14:35:20 -0400 Subject: [PATCH 37/38] removed unused --- lua/telescope/finders.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 5d423c2650..62c05a10f6 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -3,7 +3,7 @@ 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 await = a.await local async_static_finder = require('telescope.finders.async_static_finder') local async_oneshot_finder = require('telescope.finders.async_oneshot_finder') From a453a1ddc3a91b9d2f789f708217bdacc1c6283e Mon Sep 17 00:00:00 2001 From: Brian Shu Date: Tue, 13 Apr 2021 14:36:29 -0400 Subject: [PATCH 38/38] added trash global variable --- .luacheckrc | 1 + 1 file changed, 1 insertion(+) diff --git a/.luacheckrc b/.luacheckrc index 49c9bcfaed..98c76bd4c9 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -13,6 +13,7 @@ ignore = { } globals = { + "_", "TelescopeGlobalState", "TelescopeCachedUppers", "TelescopeCachedTails",