Skip to content

Commit

Permalink
fix(pickers): sorting_strategy=asc stale result clearing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jamestrew committed Sep 19, 2024
1 parent 40d6d81 commit 81c8566
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,8 @@ function Picker:clear_extra_rows(results_bufnr)
local worst_line, ok, msg
if self.sorting_strategy == "ascending" then
local num_results = self.manager:num_results()
worst_line = self.max_results - num_results

if worst_line <= 0 then
return
end

ok, msg = pcall(vim.api.nvim_buf_set_lines, results_bufnr, num_results, -1, false, {})
worst_line = math.min(num_results, self.max_results)
ok, msg = pcall(vim.api.nvim_buf_set_lines, results_bufnr, worst_line, -1, false, {})
else
worst_line = self:get_row(self.manager:num_results())
if worst_line <= 0 then
Expand Down Expand Up @@ -1465,7 +1460,7 @@ function Picker:get_result_completor(results_bufnr, find_id, prompt, status_upda

status_updater { completed = true }

self:clear_extra_rows(results_bufnr)
self:clear_extra_rows(results_bufnr) -- SUS
self.sorter:_finish(prompt)

if self.sorting_strategy == "descending" then
Expand Down
46 changes: 46 additions & 0 deletions lua/tests/automated/pickers/live_grep_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
if vim.fn.has "mac" == 1 or require("telescope.utils").iswin then
return
end

local tester = require "telescope.testharness"

local disp = function(val)
return vim.inspect(val, { newline = " ", indent = "" })
end

describe("builtin.live_grep", function()
for _, configuration in ipairs {
{ sorting_strategy = "descending" },
{ sorting_strategy = "ascending" },
} do
it("clears results correctly when " .. disp(configuration), function()
tester.run_string(string.format(
[[
runner.picker(
"live_grep",
"abcd<esc>G",
{
post_typed = {
{
5,
function()
return #vim.tbl_filter(function(line)
return line ~= ""
end, GetResults())
end,
},
},
},
vim.tbl_extend("force", {
sorter = require("telescope.sorters").get_fzy_sorter(),
layout_strategy = "center",
cwd = "./lua/tests/fixtures/live_grep",
temp__scrolling_limit = 5,
}, vim.json.decode [==[%s]==])
)
]],
vim.json.encode(configuration)
))
end)
end
end)
14 changes: 14 additions & 0 deletions lua/tests/fixtures/live_grep/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
abc
abc
abc
abc
abc


abcd
abcd
abcd
abcd
abcd

abcde

0 comments on commit 81c8566

Please sign in to comment.