Skip to content

Commit b324469

Browse files
authored
fix(pickers): sorting_strategy=asc stale result clearing (#3298)
With `sorting_strategy='ascending'`, the results buffer should never have lines beyond the `max_results` count OR the number of available results, whichever is smaller. closes #3282
1 parent 2ffcfc0 commit b324469

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

lua/telescope/pickers.lua

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,8 @@ function Picker:clear_extra_rows(results_bufnr)
431431
local worst_line, ok, msg
432432
if self.sorting_strategy == "ascending" then
433433
local num_results = self.manager:num_results()
434-
worst_line = self.max_results - num_results
435-
436-
if worst_line <= 0 then
437-
return
438-
end
439-
440-
ok, msg = pcall(vim.api.nvim_buf_set_lines, results_bufnr, num_results, -1, false, {})
434+
worst_line = math.min(num_results, self.max_results)
435+
ok, msg = pcall(vim.api.nvim_buf_set_lines, results_bufnr, worst_line, -1, false, {})
441436
else
442437
worst_line = self:get_row(self.manager:num_results())
443438
if worst_line <= 0 then
@@ -1452,10 +1447,10 @@ end
14521447

14531448
--- Handles updating the picker after all the entries are scored/processed.
14541449
---@param results_bufnr number
1455-
---@param find_id number
1450+
---@param _ number
14561451
---@param prompt string
14571452
---@param status_updater function
1458-
function Picker:get_result_completor(results_bufnr, find_id, prompt, status_updater)
1453+
function Picker:get_result_completor(results_bufnr, _, prompt, status_updater)
14591454
return vim.schedule_wrap(function()
14601455
if self.closed == true or self:is_done() then
14611456
return
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
if vim.fn.has "mac" == 1 or require("telescope.utils").iswin then
2+
return
3+
end
4+
5+
local tester = require "telescope.testharness"
6+
7+
local disp = function(val)
8+
return vim.inspect(val, { newline = " ", indent = "" })
9+
end
10+
11+
describe("builtin.live_grep", function()
12+
for _, configuration in ipairs {
13+
{ sorting_strategy = "descending" },
14+
{ sorting_strategy = "ascending" },
15+
} do
16+
it("clears results correctly when " .. disp(configuration), function()
17+
tester.run_string(string.format(
18+
[[
19+
runner.picker(
20+
"live_grep",
21+
"abcd<esc>G",
22+
{
23+
post_typed = {
24+
{
25+
5,
26+
function()
27+
return #vim.tbl_filter(function(line)
28+
return line ~= ""
29+
end, GetResults())
30+
end,
31+
},
32+
},
33+
},
34+
vim.tbl_extend("force", {
35+
sorter = require("telescope.sorters").get_fzy_sorter(),
36+
layout_strategy = "center",
37+
cwd = "./lua/tests/fixtures/live_grep",
38+
temp__scrolling_limit = 5,
39+
}, vim.json.decode [==[%s]==])
40+
)
41+
]],
42+
vim.json.encode(configuration)
43+
))
44+
end)
45+
end
46+
end)

lua/tests/fixtures/live_grep/a.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
abc
2+
abc
3+
abc
4+
abc
5+
abc
6+
7+
8+
abcd
9+
abcd
10+
abcd
11+
abcd
12+
abcd
13+
14+
abcde

0 commit comments

Comments
 (0)