From 8b3d08d7a6e8eccc2997ccbf91a7e12d506196e5 Mon Sep 17 00:00:00 2001 From: elianiva Date: Wed, 24 Feb 2021 21:40:11 +0700 Subject: [PATCH] fix: scroll misbehaving + fixed jump to middle (#547) * fix: scroll misbehaving + fixed jump to middle * add test * fixx * fix nil --- lua/telescope/actions/init.lua | 6 +++++- lua/telescope/actions/set.lua | 1 + lua/telescope/pickers/scroller.lua | 10 +++++++++- lua/tests/automated/scroller_spec.lua | 14 ++++++++------ 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 19a6d04..bf6bd4f 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -80,7 +80,11 @@ end function actions.move_to_middle(prompt_bufnr) local current_picker = actions.get_current_picker(prompt_bufnr) - current_picker:set_selection(p_scroller.middle(nil, current_picker.max_results, nil)) + current_picker:set_selection(p_scroller.middle( + current_picker.sorting_strategy, + current_picker.max_results, + current_picker.manager:num_results() + )) end function actions.move_to_bottom(prompt_bufnr) diff --git a/lua/telescope/actions/set.lua b/lua/telescope/actions/set.lua index 4e92848..bd4f1b3 100644 --- a/lua/telescope/actions/set.lua +++ b/lua/telescope/actions/set.lua @@ -28,6 +28,7 @@ local set = setmetatable({}, { set.shift_selection = function(prompt_bufnr, change) local count = vim.v.count count = count == 0 and 1 or count + count = a.nvim_get_mode().mode == "n" and count or 1 action_state.get_current_picker(prompt_bufnr):move_selection(change * count) end diff --git a/lua/telescope/pickers/scroller.lua b/lua/telescope/pickers/scroller.lua index d330dec..b77fc96 100644 --- a/lua/telescope/pickers/scroller.lua +++ b/lua/telescope/pickers/scroller.lua @@ -81,7 +81,15 @@ scroller.top = function(sorting_strategy, max_results, num_results) end scroller.middle = function(sorting_strategy, max_results, num_results) - return math.floor(max_results/2) + local mid_pos + + if sorting_strategy == 'ascending' then + mid_pos = math.floor(num_results / 2) + else + mid_pos = math.floor(max_results - num_results / 2) + end + + return (num_results < max_results) and mid_pos or math.floor(max_results / 2) end scroller.bottom = function(sorting_strategy, max_results, num_results) diff --git a/lua/tests/automated/scroller_spec.lua b/lua/tests/automated/scroller_spec.lua index bd202e8..c650aac 100644 --- a/lua/tests/automated/scroller_spec.lua +++ b/lua/tests/automated/scroller_spec.lua @@ -114,18 +114,16 @@ describe('scroller', function() describe('should give top, middle and bottom index', function() - it('should handle middle index', function() - eq(5, p_scroller.middle(nil, 11, nil)) - eq(10, p_scroller.middle(nil, 20, nil)) - eq(12, p_scroller.middle(nil, 25, nil)) - end) - it('should handle ascending', function() eq(0, p_scroller.top('ascending', 20, 1000)) eq(19, p_scroller.bottom('ascending', 20, 1000)) eq(0, p_scroller.top('ascending', 20, 10)) eq(9, p_scroller.bottom('ascending', 20, 10)) + + eq(5, p_scroller.middle('ascending', 11, 100)) + eq(10, p_scroller.middle('ascending', 20, 100)) + eq(12, p_scroller.middle('ascending', 25, 100)) end) it('should handle descending', function() @@ -134,6 +132,10 @@ describe('scroller', function() eq(10, p_scroller.top('descending', 20, 10)) eq(19, p_scroller.bottom('descending', 20, 10)) + + eq(25, p_scroller.middle('descending', 30, 10)) + eq(50, p_scroller.middle('descending', 60, 20)) + eq(105, p_scroller.middle('descending', 120, 30)) end) end) end)