feat: Add selection strategies
This commit is contained in:
@@ -36,6 +36,8 @@ builtin.git_files = function(opts)
|
|||||||
|
|
||||||
local file_picker = pickers.new {
|
local file_picker = pickers.new {
|
||||||
previewer = show_preview and file_previewer,
|
previewer = show_preview and file_previewer,
|
||||||
|
|
||||||
|
selection_strategy = opts.selection_strategy,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- local file_sorter = telescope.sorters.get_ngram_sorter()
|
-- local file_sorter = telescope.sorters.get_ngram_sorter()
|
||||||
|
|||||||
@@ -40,11 +40,16 @@ assert(Previewer)
|
|||||||
--- Create new picker
|
--- Create new picker
|
||||||
--- @param opts PickOpts
|
--- @param opts PickOpts
|
||||||
function Picker:new(opts)
|
function Picker:new(opts)
|
||||||
|
opts = opts or {}
|
||||||
|
|
||||||
return setmetatable({
|
return setmetatable({
|
||||||
filter = opts.filter,
|
filter = opts.filter,
|
||||||
previewer = opts.previewer,
|
previewer = opts.previewer,
|
||||||
maps = opts.maps,
|
maps = opts.maps,
|
||||||
|
|
||||||
get_window_options = opts.get_window_options,
|
get_window_options = opts.get_window_options,
|
||||||
|
|
||||||
|
selection_strategy = opts.selection_strategy,
|
||||||
}, Picker)
|
}, Picker)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -180,6 +185,8 @@ function Picker:find(opts)
|
|||||||
|
|
||||||
vim.api.nvim_buf_set_lines(results_bufnr, 0, self.max_results, false, utils.repeated_table(self.max_results, ""))
|
vim.api.nvim_buf_set_lines(results_bufnr, 0, self.max_results, false, utils.repeated_table(self.max_results, ""))
|
||||||
|
|
||||||
|
local selection_strategy = self.selection_strategy or 'reset'
|
||||||
|
|
||||||
local on_lines = function(_, _, _, first_line, last_line)
|
local on_lines = function(_, _, _, first_line, last_line)
|
||||||
local prompt = vim.api.nvim_buf_get_lines(prompt_bufnr, first_line, last_line, false)[1]
|
local prompt = vim.api.nvim_buf_get_lines(prompt_bufnr, first_line, last_line, false)[1]
|
||||||
|
|
||||||
@@ -238,7 +245,21 @@ function Picker:find(opts)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local process_complete = vim.schedule_wrap(function()
|
local process_complete = vim.schedule_wrap(function()
|
||||||
|
if selection_strategy == 'row' then
|
||||||
self:set_selection(self:get_selection_row())
|
self:set_selection(self:get_selection_row())
|
||||||
|
elseif selection_strategy == 'follow' then
|
||||||
|
local index = self.manager:find_entry(self:get_selection())
|
||||||
|
|
||||||
|
if index then
|
||||||
|
local follow_row = self.max_results - index + 1
|
||||||
|
self:set_selection(follow_row)
|
||||||
|
else
|
||||||
|
self:set_selection(self.max_results)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- selection_strategy == 'reset'
|
||||||
|
self:set_selection(self.max_results)
|
||||||
|
end
|
||||||
|
|
||||||
local worst_line = self.max_results - self.manager.num_results()
|
local worst_line = self.max_results - self.manager.num_results()
|
||||||
if worst_line <= 0 then
|
if worst_line <= 0 then
|
||||||
@@ -507,18 +528,27 @@ pickers.entry_manager = function(max_results, set_entry)
|
|||||||
return (entry_state[index] or {}).entry
|
return (entry_state[index] or {}).entry
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
find_entry = function(_, entry)
|
||||||
|
if entry == nil then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
for k, v in ipairs(entry_state) do
|
||||||
|
local existing_entry = v.entry
|
||||||
|
|
||||||
|
-- FIXME: This has the problem of assuming that display will not be the same for two different entries.
|
||||||
|
if existing_entry.display == entry.display then
|
||||||
|
return k
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end,
|
||||||
|
|
||||||
_get_state = function()
|
_get_state = function()
|
||||||
return entry_state
|
return entry_state
|
||||||
end,
|
end,
|
||||||
}, {
|
}, {})
|
||||||
-- insert =
|
|
||||||
|
|
||||||
-- __index = function(_, line)
|
|
||||||
-- end,
|
|
||||||
|
|
||||||
-- __newindex = function(_, index, line)
|
|
||||||
-- end,
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user