refactor: Move scroller into own testable module
This commit is contained in:
@@ -14,6 +14,7 @@ local utils = require('telescope.utils')
|
|||||||
|
|
||||||
local layout_strategies = require('telescope.pickers.layout_strategies')
|
local layout_strategies = require('telescope.pickers.layout_strategies')
|
||||||
local entry_display = require('telescope.pickers.entry_display')
|
local entry_display = require('telescope.pickers.entry_display')
|
||||||
|
local p_scroller = require('telescope.pickers.scroller')
|
||||||
|
|
||||||
local EntryManager = require('telescope.entry_manager')
|
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)
|
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"),
|
prompt_title = get_default(opts.prompt_title, "Prompt"),
|
||||||
results_title = get_default(opts.results_title, "Results"),
|
results_title = get_default(opts.results_title, "Results"),
|
||||||
preview_title = get_default(opts.preview_title, "Preview"),
|
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),
|
sorting_strategy = get_default(opts.sorting_strategy, config.values.sorting_strategy),
|
||||||
selection_strategy = get_default(opts.selection_strategy, config.values.selection_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,
|
get_window_options = opts.get_window_options,
|
||||||
layout_strategy = layout_strategy,
|
layout_strategy = layout_strategy,
|
||||||
@@ -123,6 +123,13 @@ function Picker:new(opts)
|
|||||||
|
|
||||||
preview_cutoff = get_default(opts.preview_cutoff, config.values.preview_cutoff),
|
preview_cutoff = get_default(opts.preview_cutoff, config.values.preview_cutoff),
|
||||||
}, self)
|
}, self)
|
||||||
|
|
||||||
|
|
||||||
|
obj.scroller = p_scroller.create(
|
||||||
|
get_default(opts.scroll_strategy, config.values.scroll_strategy)
|
||||||
|
)
|
||||||
|
|
||||||
|
return obj
|
||||||
end
|
end
|
||||||
|
|
||||||
function Picker:_get_initial_window_options()
|
function Picker:_get_initial_window_options()
|
||||||
@@ -694,28 +701,10 @@ function Picker:reset_selection()
|
|||||||
self.multi_select = {}
|
self.multi_select = {}
|
||||||
end
|
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)
|
function Picker:set_selection(row)
|
||||||
-- TODO: Loop around behavior?
|
-- TODO: Loop around behavior?
|
||||||
-- TODO: Scrolling past max results
|
-- 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 not self:can_select_row(row) then
|
||||||
-- If the current selected row exceeds number of currently displayed
|
-- If the current selected row exceeds number of currently displayed
|
||||||
|
|||||||
34
lua/telescope/pickers/scroller.lua
Normal file
34
lua/telescope/pickers/scroller.lua
Normal 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
|
||||||
56
lua/tests/automated/scroller_spec.lua
Normal file
56
lua/tests/automated/scroller_spec.lua
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
local p_scroller = require('telescope.pickers.scroller')
|
||||||
|
|
||||||
|
local eq = assert.are.same
|
||||||
|
|
||||||
|
describe('scroller', function()
|
||||||
|
local max_results = 10
|
||||||
|
|
||||||
|
describe('cycle', function()
|
||||||
|
local cycle_scroller = p_scroller.create('cycle')
|
||||||
|
|
||||||
|
it('should return values within the max results', function()
|
||||||
|
eq(5, cycle_scroller(max_results, max_results, 5))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should return 0 at 0', function()
|
||||||
|
eq(0, cycle_scroller(max_results, max_results, 0))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should cycle you to the top when you go below 0', function()
|
||||||
|
eq(max_results - 1, cycle_scroller(max_results, max_results, -1))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should cycle you to 0 when you go past the results', function()
|
||||||
|
eq(0, cycle_scroller(max_results, max_results, max_results + 1))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should cycle when current results is less than max_results', function()
|
||||||
|
eq(0, cycle_scroller(max_results, 5, 7))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
describe('other', function()
|
||||||
|
local limit_scroller = p_scroller.create('limit')
|
||||||
|
|
||||||
|
it('should return values within the max results', function()
|
||||||
|
eq(5, limit_scroller(max_results, max_results, 5))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should return 0 at 0', function()
|
||||||
|
eq(0, limit_scroller(max_results, max_results, 0))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should not cycle', function()
|
||||||
|
eq(0, limit_scroller(max_results, max_results, -1))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should cycle you to 0 when you go past the results', function()
|
||||||
|
eq(max_results - 1, limit_scroller(max_results, max_results, max_results + 1))
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('should stay at current results when current results is less than max_results', function()
|
||||||
|
local current = 5
|
||||||
|
eq(current - 1, limit_scroller(max_results, current, 7))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
Reference in New Issue
Block a user