From f3609abd7f9bf34ad482b1beb1c0482b76562af3 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Thu, 10 Dec 2020 23:31:28 -0500 Subject: [PATCH] refactor: Move scroller into own testable module --- lua/telescope/pickers.lua | 31 +++++---------- lua/telescope/pickers/scroller.lua | 34 ++++++++++++++++ lua/tests/automated/scroller_spec.lua | 56 +++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 lua/telescope/pickers/scroller.lua create mode 100644 lua/tests/automated/scroller_spec.lua diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 77d183b..04b1ccf 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -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 diff --git a/lua/telescope/pickers/scroller.lua b/lua/telescope/pickers/scroller.lua new file mode 100644 index 0000000..f4c90ff --- /dev/null +++ b/lua/telescope/pickers/scroller.lua @@ -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 diff --git a/lua/tests/automated/scroller_spec.lua b/lua/tests/automated/scroller_spec.lua new file mode 100644 index 0000000..7889573 --- /dev/null +++ b/lua/tests/automated/scroller_spec.lua @@ -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)