Skip to content

Commit

Permalink
fix(pickers): sorting_strategy=asc stale result clearing (#3298)
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 authored Sep 23, 2024
1 parent 2ffcfc0 commit b324469
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
13 changes: 4 additions & 9 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 @@ -1452,10 +1447,10 @@ end

--- Handles updating the picker after all the entries are scored/processed.
---@param results_bufnr number
---@param find_id number
---@param _ number
---@param prompt string
---@param status_updater function
function Picker:get_result_completor(results_bufnr, find_id, prompt, status_updater)
function Picker:get_result_completor(results_bufnr, _, prompt, status_updater)
return vim.schedule_wrap(function()
if self.closed == true or self:is_done() then
return
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 b324469

Please sign in to comment.