refactor: Move scroller into own testable module

This commit is contained in:
TJ DeVries
2020-12-10 23:31:28 -05:00
parent b74c4b3efd
commit f3609abd7f
3 changed files with 100 additions and 21 deletions

View File

@@ -14,6 +14,7 @@ local utils = require('telescope.utils')
local layout_strategies = require('telescope.pickers.layout_strategies')
local entry_display = require('telescope.pickers.entry_display')
local p_scroller = require('telescope.pickers.scroller')
local EntryManager = require('telescope.entry_manager')
@@ -66,7 +67,7 @@ function Picker:new(opts)
local layout_strategy = get_default(opts.layout_strategy, config.values.layout_strategy)
return setmetatable({
local obj = setmetatable({
prompt_title = get_default(opts.prompt_title, "Prompt"),
results_title = get_default(opts.results_title, "Results"),
preview_title = get_default(opts.preview_title, "Preview"),
@@ -91,7 +92,6 @@ function Picker:new(opts)
sorting_strategy = get_default(opts.sorting_strategy, config.values.sorting_strategy),
selection_strategy = get_default(opts.selection_strategy, config.values.selection_strategy),
scroll_strategy = get_default(opts.scroll_strategy, config.values.scroll_strategy),
get_window_options = opts.get_window_options,
layout_strategy = layout_strategy,
@@ -123,6 +123,13 @@ function Picker:new(opts)
preview_cutoff = get_default(opts.preview_cutoff, config.values.preview_cutoff),
}, self)
obj.scroller = p_scroller.create(
get_default(opts.scroll_strategy, config.values.scroll_strategy)
)
return obj
end
function Picker:_get_initial_window_options()
@@ -694,28 +701,10 @@ function Picker:reset_selection()
self.multi_select = {}
end
function Picker:_handle_scroll_strategy(row)
if self.scroll_strategy == "cycle" then
if row >= self.max_results then
row = 0
elseif row < 0 then
row = self.max_results - 1
end
else
if row >= self.max_results then
row = self.max_results - 1
elseif row < 0 then
row = 0
end
end
return row
end
function Picker:set_selection(row)
-- TODO: Loop around behavior?
-- TODO: Scrolling past max results
row = self:_handle_scroll_strategy(row)
row = self.scroller(self.max_results, self.manager:num_results(), row)
if not self:can_select_row(row) then
-- If the current selected row exceeds number of currently displayed

View File

@@ -0,0 +1,34 @@
local scroller = {}
scroller.create = function(strategy)
if strategy == 'cycle' then
return function(max_results, num_results, row)
local count = math.min(max_results, num_results)
if row >= count then
return 0
elseif row < 0 then
return count - 1
end
return row
end
elseif strategy == 'limit' or strategy == nil then
return function(max_results, num_results, row)
local count = math.min(max_results, num_results)
if row >= count then
return count - 1
elseif row < 0 then
return 0
end
return row
end
else
error("Unsupported strategy: ", strategy)
end
end
return scroller