Implements horizontal scrolling in previewer & results. (#2437)
* Implements horizontal scrolling in previewer & results. * docs: update wrt. horizontal scrolling in previewer & results
This commit is contained in:
committed by
GitHub
parent
ffe35cb433
commit
5fff2a138b
@@ -248,6 +248,10 @@ Many familiar mapping patterns are set up as defaults.
|
|||||||
| `<C-t>` | Go to a file in a new tab |
|
| `<C-t>` | Go to a file in a new tab |
|
||||||
| `<C-u>` | Scroll up in preview window |
|
| `<C-u>` | Scroll up in preview window |
|
||||||
| `<C-d>` | Scroll down in preview window |
|
| `<C-d>` | Scroll down in preview window |
|
||||||
|
| `<C-f>` | Scroll left in preview window |
|
||||||
|
| `<C-k>` | Scroll right in preview window |
|
||||||
|
| `<M-f>` | Scroll left in results window |
|
||||||
|
| `<M-k>` | Scroll right in results window |
|
||||||
| `<C-/>` | Show mappings for picker actions (insert mode) |
|
| `<C-/>` | Show mappings for picker actions (insert mode) |
|
||||||
| `?` | Show mappings for picker actions (normal mode) |
|
| `?` | Show mappings for picker actions (normal mode) |
|
||||||
| `<C-c>` | Close telescope |
|
| `<C-c>` | Close telescope |
|
||||||
|
|||||||
@@ -2532,6 +2532,22 @@ actions.preview_scrolling_down({prompt_bufnr}) *telescope.actions.preview_scroll
|
|||||||
{prompt_bufnr} (number) The prompt bufnr
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
|
actions.preview_scrolling_left({prompt_bufnr}) *telescope.actions.preview_scrolling_left()*
|
||||||
|
Scroll the preview window to the left
|
||||||
|
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
|
actions.preview_scrolling_right({prompt_bufnr}) *telescope.actions.preview_scrolling_right()*
|
||||||
|
Scroll the preview window to the right
|
||||||
|
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
actions.results_scrolling_up({prompt_bufnr}) *telescope.actions.results_scrolling_up()*
|
actions.results_scrolling_up({prompt_bufnr}) *telescope.actions.results_scrolling_up()*
|
||||||
Scroll the results window up
|
Scroll the results window up
|
||||||
|
|
||||||
@@ -2548,6 +2564,22 @@ actions.results_scrolling_down({prompt_bufnr}) *telescope.actions.results_scroll
|
|||||||
{prompt_bufnr} (number) The prompt bufnr
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
|
actions.results_scrolling_left({prompt_bufnr}) *telescope.actions.results_scrolling_left()*
|
||||||
|
Scroll the results window to the left
|
||||||
|
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
|
actions.results_scrolling_right({prompt_bufnr}) *telescope.actions.results_scrolling_right()*
|
||||||
|
Scroll the results window to the right
|
||||||
|
|
||||||
|
|
||||||
|
Parameters: ~
|
||||||
|
{prompt_bufnr} (number) The prompt bufnr
|
||||||
|
|
||||||
|
|
||||||
actions.center({prompt_bufnr}) *telescope.actions.center()*
|
actions.center({prompt_bufnr}) *telescope.actions.center()*
|
||||||
Center the cursor in the window, can be used after selecting a file to edit
|
Center the cursor in the window, can be used after selecting a file to edit
|
||||||
You can just map `actions.select_default + actions.center`
|
You can just map `actions.select_default + actions.center`
|
||||||
@@ -3407,6 +3439,7 @@ previewers.Previewer() *telescope.previewers.Previewer()*
|
|||||||
- `:teardown()`
|
- `:teardown()`
|
||||||
- `:send_input(input)`
|
- `:send_input(input)`
|
||||||
- `:scroll_fn(direction)`
|
- `:scroll_fn(direction)`
|
||||||
|
- `:scroll_horizontal_fn(direction)`
|
||||||
|
|
||||||
`Previewer:new()` expects a table as input with following keys:
|
`Previewer:new()` expects a table as input with following keys:
|
||||||
- `setup` function(self): Will be called the first time preview will be
|
- `setup` function(self): Will be called the first time preview will be
|
||||||
@@ -3422,6 +3455,8 @@ previewers.Previewer() *telescope.previewers.Previewer()*
|
|||||||
`termopen_previewer` and it can be used to send input to the terminal
|
`termopen_previewer` and it can be used to send input to the terminal
|
||||||
application, like less.
|
application, like less.
|
||||||
- `scroll_fn` function(self, direction): Used to make scrolling work.
|
- `scroll_fn` function(self, direction): Used to make scrolling work.
|
||||||
|
- `scroll_horizontal_fn` function(self, direction): Used to make
|
||||||
|
horizontal scrolling work.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -222,6 +222,18 @@ actions.preview_scrolling_down = function(prompt_bufnr)
|
|||||||
action_set.scroll_previewer(prompt_bufnr, 1)
|
action_set.scroll_previewer(prompt_bufnr, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Scroll the preview window to the left
|
||||||
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
|
actions.preview_scrolling_left = function(prompt_bufnr)
|
||||||
|
action_set.scroll_horizontal_previewer(prompt_bufnr, -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Scroll the preview window to the right
|
||||||
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
|
actions.preview_scrolling_right = function(prompt_bufnr)
|
||||||
|
action_set.scroll_horizontal_previewer(prompt_bufnr, 1)
|
||||||
|
end
|
||||||
|
|
||||||
--- Scroll the results window up
|
--- Scroll the results window up
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
actions.results_scrolling_up = function(prompt_bufnr)
|
actions.results_scrolling_up = function(prompt_bufnr)
|
||||||
@@ -234,6 +246,18 @@ actions.results_scrolling_down = function(prompt_bufnr)
|
|||||||
action_set.scroll_results(prompt_bufnr, 1)
|
action_set.scroll_results(prompt_bufnr, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Scroll the results window to the left
|
||||||
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
|
actions.results_scrolling_left = function(prompt_bufnr)
|
||||||
|
action_set.scroll_horizontal_results(prompt_bufnr, -1)
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Scroll the results window to the right
|
||||||
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
|
actions.results_scrolling_right = function(prompt_bufnr)
|
||||||
|
action_set.scroll_horizontal_results(prompt_bufnr, 1)
|
||||||
|
end
|
||||||
|
|
||||||
--- Center the cursor in the window, can be used after selecting a file to edit
|
--- Center the cursor in the window, can be used after selecting a file to edit
|
||||||
--- You can just map `actions.select_default + actions.center`
|
--- You can just map `actions.select_default + actions.center`
|
||||||
---@param prompt_bufnr number: The prompt bufnr
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
|
|||||||
@@ -241,6 +241,27 @@ action_set.scroll_previewer = function(prompt_bufnr, direction)
|
|||||||
previewer:scroll_fn(math.floor(speed * direction))
|
previewer:scroll_fn(math.floor(speed * direction))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Scrolls the previewer to the left or right.
|
||||||
|
--- Defaults to a half page scroll, but can be overridden using the `scroll_speed`
|
||||||
|
--- option in `layout_config`. See |telescope.layout| for more details.
|
||||||
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
|
---@param direction number: The direction of the scrolling
|
||||||
|
-- Valid directions include: "1", "-1"
|
||||||
|
action_set.scroll_horizontal_previewer = function(prompt_bufnr, direction)
|
||||||
|
local previewer = action_state.get_current_picker(prompt_bufnr).previewer
|
||||||
|
local status = state.get_status(prompt_bufnr)
|
||||||
|
|
||||||
|
-- Check if we actually have a previewer and a preview window
|
||||||
|
if type(previewer) ~= "table" or previewer.scroll_horizontal_fn == nil or status.preview_win == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local default_speed = vim.api.nvim_win_get_height(status.preview_win) / 2
|
||||||
|
local speed = status.picker.layout_config.scroll_speed or default_speed
|
||||||
|
|
||||||
|
previewer:scroll_horizontal_fn(math.floor(speed * direction))
|
||||||
|
end
|
||||||
|
|
||||||
--- Scrolls the results up or down.
|
--- Scrolls the results up or down.
|
||||||
--- Defaults to a half page scroll, but can be overridden using the `scroll_speed`
|
--- Defaults to a half page scroll, but can be overridden using the `scroll_speed`
|
||||||
--- option in `layout_config`. See |telescope.layout| for more details.
|
--- option in `layout_config`. See |telescope.layout| for more details.
|
||||||
@@ -261,6 +282,24 @@ action_set.scroll_results = function(prompt_bufnr, direction)
|
|||||||
action_set.shift_selection(prompt_bufnr, math.floor(speed) * direction)
|
action_set.shift_selection(prompt_bufnr, math.floor(speed) * direction)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Scrolls the results to the left or right.
|
||||||
|
--- Defaults to a half page scroll, but can be overridden using the `scroll_speed`
|
||||||
|
--- option in `layout_config`. See |telescope.layout| for more details.
|
||||||
|
---@param prompt_bufnr number: The prompt bufnr
|
||||||
|
---@param direction number: The direction of the scrolling
|
||||||
|
-- Valid directions include: "1", "-1"
|
||||||
|
action_set.scroll_horizontal_results = function(prompt_bufnr, direction)
|
||||||
|
local status = state.get_status(prompt_bufnr)
|
||||||
|
local default_speed = vim.api.nvim_win_get_height(status.results_win) / 2
|
||||||
|
local speed = status.picker.layout_config.scroll_speed or default_speed
|
||||||
|
|
||||||
|
local input = direction > 0 and [[zl]] or [[zh]]
|
||||||
|
|
||||||
|
vim.api.nvim_win_call(status.results_win, function()
|
||||||
|
vim.cmd([[normal! ]] .. math.floor(speed) .. input)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
-- ==================================================
|
-- ==================================================
|
||||||
-- Transforms modules and sets the corect metatables.
|
-- Transforms modules and sets the corect metatables.
|
||||||
-- ==================================================
|
-- ==================================================
|
||||||
|
|||||||
@@ -148,9 +148,13 @@ mappings.default_mappings = config.values.default_mappings
|
|||||||
|
|
||||||
["<C-u>"] = actions.preview_scrolling_up,
|
["<C-u>"] = actions.preview_scrolling_up,
|
||||||
["<C-d>"] = actions.preview_scrolling_down,
|
["<C-d>"] = actions.preview_scrolling_down,
|
||||||
|
["<C-f>"] = actions.preview_scrolling_left,
|
||||||
|
["<C-k>"] = actions.preview_scrolling_right,
|
||||||
|
|
||||||
["<PageUp>"] = actions.results_scrolling_up,
|
["<PageUp>"] = actions.results_scrolling_up,
|
||||||
["<PageDown>"] = actions.results_scrolling_down,
|
["<PageDown>"] = actions.results_scrolling_down,
|
||||||
|
["<M-f>"] = actions.results_scrolling_left,
|
||||||
|
["<M-k>"] = actions.results_scrolling_right,
|
||||||
|
|
||||||
["<Tab>"] = actions.toggle_selection + actions.move_selection_worse,
|
["<Tab>"] = actions.toggle_selection + actions.move_selection_worse,
|
||||||
["<S-Tab>"] = actions.toggle_selection + actions.move_selection_better,
|
["<S-Tab>"] = actions.toggle_selection + actions.move_selection_better,
|
||||||
|
|||||||
@@ -189,6 +189,19 @@ local scroll_fn = function(self, direction)
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local scroll_horizontal_fn = function(self, direction)
|
||||||
|
if not self.state then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local input = direction > 0 and [[zl]] or [[zh]]
|
||||||
|
local count = math.abs(direction)
|
||||||
|
|
||||||
|
vim.api.nvim_win_call(self.state.winid, function()
|
||||||
|
vim.cmd([[normal! ]] .. count .. input)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
previewers.file_maker = function(filepath, bufnr, opts)
|
previewers.file_maker = function(filepath, bufnr, opts)
|
||||||
opts = vim.F.if_nil(opts, {})
|
opts = vim.F.if_nil(opts, {})
|
||||||
-- TODO(conni2461): here shouldn't be any hardcoded magic numbers ...
|
-- TODO(conni2461): here shouldn't be any hardcoded magic numbers ...
|
||||||
@@ -462,6 +475,10 @@ previewers.new_buffer_previewer = function(opts)
|
|||||||
opts.scroll_fn = scroll_fn
|
opts.scroll_fn = scroll_fn
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not opts.scroll_horizontal_fn then
|
||||||
|
opts.scroll_horizontal_fn = scroll_horizontal_fn
|
||||||
|
end
|
||||||
|
|
||||||
return Previewer:new(opts)
|
return Previewer:new(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ local previewers = {}
|
|||||||
--- - `:teardown()`
|
--- - `:teardown()`
|
||||||
--- - `:send_input(input)`
|
--- - `:send_input(input)`
|
||||||
--- - `:scroll_fn(direction)`
|
--- - `:scroll_fn(direction)`
|
||||||
|
--- - `:scroll_horizontal_fn(direction)`
|
||||||
---
|
---
|
||||||
--- `Previewer:new()` expects a table as input with following keys:
|
--- `Previewer:new()` expects a table as input with following keys:
|
||||||
--- - `setup` function(self): Will be called the first time preview will be
|
--- - `setup` function(self): Will be called the first time preview will be
|
||||||
@@ -64,6 +65,8 @@ local previewers = {}
|
|||||||
--- used to send input to the terminal
|
--- used to send input to the terminal
|
||||||
--- application, like less.
|
--- application, like less.
|
||||||
--- - `scroll_fn` function(self, direction): Used to make scrolling work.
|
--- - `scroll_fn` function(self, direction): Used to make scrolling work.
|
||||||
|
--- - `scroll_horizontal_fn` function(self, direction): Used to make
|
||||||
|
--- horizontal scrolling work.
|
||||||
previewers.Previewer = Previewer
|
previewers.Previewer = Previewer
|
||||||
|
|
||||||
--- A shorthand for creating a new Previewer.
|
--- A shorthand for creating a new Previewer.
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ function Previewer:new(opts)
|
|||||||
_teardown_func = opts.teardown,
|
_teardown_func = opts.teardown,
|
||||||
_send_input = opts.send_input,
|
_send_input = opts.send_input,
|
||||||
_scroll_fn = opts.scroll_fn,
|
_scroll_fn = opts.scroll_fn,
|
||||||
|
_scroll_horizontal_fn = opts.scroll_horizontal_fn,
|
||||||
preview_fn = opts.preview_fn,
|
preview_fn = opts.preview_fn,
|
||||||
_empty_bufnr = nil,
|
_empty_bufnr = nil,
|
||||||
}, Previewer)
|
}, Previewer)
|
||||||
@@ -95,4 +96,12 @@ function Previewer:scroll_fn(direction)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Previewer:scroll_horizontal_fn(direction)
|
||||||
|
if self._scroll_horizontal_fn then
|
||||||
|
self:_scroll_horizontal_fn(direction)
|
||||||
|
else
|
||||||
|
vim.api.nvim_err_writeln "scroll_horizontal_fn is not defined for this previewer"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return Previewer
|
return Previewer
|
||||||
|
|||||||
Reference in New Issue
Block a user