Skip to content

Commit bce4194

Browse files
committed
fix: check what the user's telescope config option of 'defaults.sorting_strategy' is when calling 'refresh_picker'/'create_finder'
When 'defaults.sorting_strategy = descending', telescope row numbers begin at 250 instead of 0. This was causing 'create_finder' to return the wrong 'new_row_number' value to 'refresh_picker' since it was only capable of handling 0-based row numbers, which is how telescope numbers rows when 'defaults.sorting_strategy = ascending'. Similarly, additional logic needed to be added directly to 'refresh_picker' for actions that pass 'deleted = true' to it so that it can also handle when 'defaults.sorting_strategy = descending'.
1 parent 533ec4f commit bce4194

3 files changed

Lines changed: 34 additions & 17 deletions

File tree

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ By default, the below table is passed to the `setup` function:
4646

4747
```lua
4848
{
49-
mappings = {
50-
i = {
51-
["<CR>"] = { action = actions.toggle_term, exit_on_action = false }, -- toggles terminal open/closed
52-
["<C-i>"] = { action = actions.create_term, exit_on_action = false }, -- creates a new terminal buffer
53-
["<C-d>"] = { action = actions.delete_term, exit_on_action = false }, -- deletes a terminal buffer
54-
["<C-r>"] = { action = actions.rename_term, exit_on_action = false }, -- provides a prompt to rename a terminal
55-
},
49+
mappings = {
50+
i = {
51+
["<CR>"] = { action = actions.toggle_term, exit_on_action = false }, -- toggles terminal open/closed
52+
["<C-i>"] = { action = actions.create_term, exit_on_action = false }, -- creates a new terminal buffer
53+
["<C-d>"] = { action = actions.delete_term, exit_on_action = false }, -- deletes a terminal buffer
54+
["<C-r>"] = { action = actions.rename_term, exit_on_action = false }, -- provides a prompt to rename a terminal
55+
},
5656
}, -- key mappings bound inside the telescope window
5757
telescope_titles = {
5858
preview = "Preview", -- title of the preview buffer in telescope

lua/lib/actions/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ local Terminal = require("toggleterm.terminal").Terminal
77

88
local M = {}
99

10-
--- Create a new terminal and open it. If exit_on_action is true, focus it. If the term's direction is "float" and
11-
--- exit_on_action is false, don't automatically open the terminal upon creation to prevent flashes.
10+
--- Create a new terminal and open it. If `exit_on_action = true`, focus it. If the term's direction is `float` and
11+
--- `exit_on_action = false`, don't automatically open the terminal upon creation to prevent flashes.
1212
--- @param prompt_bufnr number The buffer number of the telescope prompt.
1313
--- @param exit_on_action boolean Whether to exit the telescope buffer when the action executes.
1414
function M.create_term(prompt_bufnr, exit_on_action)

lua/util.lua

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local toggleterm = require("toggleterm.terminal")
33
local toggleterm_ui = require("toggleterm.ui")
44
local Path = require("plenary.path")
55
local actions_state = require("telescope.actions.state")
6+
local telescope_config = require("telescope.config").values
67

78
local M = {}
89
local function_name_to_description = {
@@ -149,7 +150,14 @@ function M.create_finder(cur_row_term_id)
149150
if cur_row_term_id then
150151
for i, term in ipairs(terms) do
151152
if term.id == cur_row_term_id then
152-
new_row_num = i - 1
153+
-- When telescope's `defaults.sorting_strategy` config property is set to descending (which is the defualt),
154+
-- the telescope rows start at 250 and count down. In other words, row 250 is the equivalent of row 0 and row 249 is the same
155+
-- as row 1 `defaults.sorting_strategy = descending`.
156+
if telescope_config.sorting_strategy == "descending" then
157+
new_row_num = 250 - i
158+
elseif telescope_config.sorting_strategy == "ascending" then
159+
new_row_num = i - 1
160+
end
153161
break
154162
end
155163
end
@@ -186,18 +194,28 @@ function M.focus_on_telescope(prompt_bufnr)
186194
print("Telescope buffer not visible in any window")
187195
end
188196

189-
--- Focus on the open telescope buffer and refresh the picker so the changes to the terminal buffers caused by an action in
190-
--- actions/init.lua are reflected in telescope.
197+
--- Refresh the picker so the changes to the terminal buffers caused by an action in
198+
--- actions/init.lua are reflected in telescope. Useful for actions that are executed with exit_on_action = false.
191199
--- @param prompt_bufnr number The buffer number of the prompt.
192200
--- @param selection table The current selection object.
193201
--- @param deleted boolean|nil A boolean indicating if the selection was deleted.
194202
function M.refresh_picker(prompt_bufnr, selection, deleted)
195203
local current_picker = actions_state.get_current_picker(prompt_bufnr)
196204
local finder, new_row_number = M.create_finder(selection.id)
197205

198-
-- If an item has been deleted, we need to adjust the row number
199-
if deleted and selection.index > 1 then
200-
new_row_number = selection.index - 2
206+
-- When telescope's `defaults.sorting_strategy` config property is set to descending (which is the defualt),
207+
-- the telescope rows start at 250 and count down. In other words, row 250 is the equivalent of row 0 and row 249 is the same
208+
-- as row 1 `defaults.sorting_strategy = descending`.
209+
if deleted then
210+
if telescope_config.sorting_strategy == "descending" then
211+
if selection.index > 1 then
212+
new_row_number = 250 - selection.index + 1
213+
end
214+
elseif telescope_config.sorting_strategy == "ascending" then
215+
if selection.index > 1 then
216+
new_row_number = selection.index - 2
217+
end
218+
end
201219
end
202220

203221
current_picker:refresh(finder, { reset_prompt = false })
@@ -211,8 +229,7 @@ function M.refresh_picker(prompt_bufnr, selection, deleted)
211229
end
212230
end
213231

214-
--- Set the selection row in the telescope picker. Useful for keeping the selection on the current entry after an action in
215-
--- actions/init.lua is run and refresh_picker is run.
232+
--- Set the selection row in the telescope picker. Useful for keeping the selection on the current entry after an action in actions/init.lua is run.
216233
--- @param picker table The telescope picker object.
217234
--- @param row_number number The row number to set the selection to.
218235
function M.set_selection_row(picker, row_number)

0 commit comments

Comments
 (0)