From b324469959908c1c7434eb65d80e87895e6828f7 Mon Sep 17 00:00:00 2001 From: James Trew <66286082+jamestrew@users.noreply.github.com> Date: Mon, 23 Sep 2024 02:02:08 +0000 Subject: [PATCH] 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 --- lua/telescope/pickers.lua | 13 ++---- .../automated/pickers/live_grep_spec.lua | 46 +++++++++++++++++++ lua/tests/fixtures/live_grep/a.txt | 14 ++++++ 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 lua/tests/automated/pickers/live_grep_spec.lua create mode 100644 lua/tests/fixtures/live_grep/a.txt diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 33a24bd83b..b353c8a6ac 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -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 @@ -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 diff --git a/lua/tests/automated/pickers/live_grep_spec.lua b/lua/tests/automated/pickers/live_grep_spec.lua new file mode 100644 index 0000000000..3471c9062f --- /dev/null +++ b/lua/tests/automated/pickers/live_grep_spec.lua @@ -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", + "abcdG", + { + 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) diff --git a/lua/tests/fixtures/live_grep/a.txt b/lua/tests/fixtures/live_grep/a.txt new file mode 100644 index 0000000000..abb3445f39 --- /dev/null +++ b/lua/tests/fixtures/live_grep/a.txt @@ -0,0 +1,14 @@ +abc +abc +abc +abc +abc + + +abcd +abcd +abcd +abcd +abcd + +abcde