diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 9286833..5ae284b 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -5,6 +5,7 @@ local a = vim.api local log = require('telescope.log') local state = require('telescope.state') local utils = require('telescope.utils') +local p_scroller = require('telescope.pickers.scroller') local action_state = require('telescope.actions.state') local action_set = require('telescope.actions.set') @@ -69,6 +70,22 @@ function actions.move_selection_previous(prompt_bufnr) action_set.shift_selection(prompt_bufnr, -1) end +function actions.move_to_top(prompt_bufnr) + local current_picker = actions.get_current_picker(prompt_bufnr) + current_picker:set_selection(p_scroller.top(current_picker.sorting_strategy, + current_picker.max_results, + current_picker.manager:num_results() + )) +end + +function actions.move_to_bottom(prompt_bufnr) + local current_picker = actions.get_current_picker(prompt_bufnr) + current_picker:set_selection(p_scroller.bottom(current_picker.sorting_strategy, + current_picker.max_results, + current_picker.manager:num_results() + )) +end + function actions.add_selection(prompt_bufnr) local current_picker = action_state.get_current_picker(prompt_bufnr) current_picker:add_selection(current_picker:get_selection_row()) diff --git a/lua/telescope/actions/set.lua b/lua/telescope/actions/set.lua index 13cc75f..4e92848 100644 --- a/lua/telescope/actions/set.lua +++ b/lua/telescope/actions/set.lua @@ -26,7 +26,9 @@ local set = setmetatable({}, { ---@param prompt_bufnr number: The prompt bufnr ---@param change number: The amount to shift the selection by set.shift_selection = function(prompt_bufnr, change) - action_state.get_current_picker(prompt_bufnr):move_selection(change) + local count = vim.v.count + count = count == 0 and 1 or count + action_state.get_current_picker(prompt_bufnr):move_selection(change * count) end --- Select the current entry. This is the action set to overwrite common diff --git a/lua/telescope/mappings.lua b/lua/telescope/mappings.lua index efc833e..95ce279 100644 --- a/lua/telescope/mappings.lua +++ b/lua/telescope/mappings.lua @@ -38,6 +38,8 @@ mappings.default_mappings = config.values.default_mappings or { -- TODO: This would be weird if we switch the ordering. ["j"] = actions.move_selection_next, ["k"] = actions.move_selection_previous, + ["H"] = actions.move_to_top, + ["L"] = actions.move_to_bottom, [""] = actions.move_selection_next, [""] = actions.move_selection_previous, @@ -110,7 +112,7 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts) ) else local key_id = assign_function(prompt_bufnr, key_func) - local prefix = "" + local prefix local map_string if opts.expr then @@ -122,10 +124,14 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts) else if mode == "i" and not opts.expr then prefix = "" + elseif mode == "n" then + prefix = ":" + else + prefix = ":" end map_string = string.format( - "%s:lua require('telescope.mappings').execute_keymap(%s, %s)", + "%slua require('telescope.mappings').execute_keymap(%s, %s)", prefix, prompt_bufnr, key_id diff --git a/lua/telescope/pickers/scroller.lua b/lua/telescope/pickers/scroller.lua index 3169c4b..8b84e7c 100644 --- a/lua/telescope/pickers/scroller.lua +++ b/lua/telescope/pickers/scroller.lua @@ -73,4 +73,18 @@ scroller.create = function(scroll_strategy, sorting_strategy) end end +scroller.top = function(sorting_strategy, max_results, num_results) + if sorting_strategy == 'ascending' then + return 0 + end + return (num_results > max_results) and 0 or (max_results - num_results) +end + +scroller.bottom = function(sorting_strategy, max_results, num_results) + if sorting_strategy == 'ascending' then + return math.min(max_results, num_results) - 1 + end + return max_results - 1 +end + return scroller diff --git a/lua/tests/automated/scroller_spec.lua b/lua/tests/automated/scroller_spec.lua index dcc55a0..98233b4 100644 --- a/lua/tests/automated/scroller_spec.lua +++ b/lua/tests/automated/scroller_spec.lua @@ -111,4 +111,22 @@ describe('scroller', function() eq(0, scroller(23, 112, 23)) end) end) + + describe('should give top and bottom index', function() + 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)) + end) + + it('should handle descending', function() + eq(0, p_scroller.top('descending', 20, 1000)) + eq(19, p_scroller.bottom('descending', 20, 1000)) + + eq(10, p_scroller.top('descending', 20, 10)) + eq(19, p_scroller.bottom('descending', 20, 10)) + end) + end) end)