summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTJ DeVries <devries.timothyj@gmail.com>2020-12-10 23:31:28 -0500
committerTJ DeVries <devries.timothyj@gmail.com>2020-12-10 23:31:28 -0500
commitf3609abd7f9bf34ad482b1beb1c0482b76562af3 (patch)
tree4b298a725cdbcfd400a179a3d6cddede4725f8ea
parentb74c4b3efddf6d6031918c2e2de83e44e26da9f2 (diff)
refactor: Move scroller into own testable module
-rw-r--r--lua/telescope/pickers.lua31
-rw-r--r--lua/telescope/pickers/scroller.lua34
-rw-r--r--lua/tests/automated/scroller_spec.lua56
3 files changed, 100 insertions, 21 deletions
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)