telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

fix(asyncjob): interrupt long time job

Open Marskey opened this issue 2 years ago • 1 comments

Description

fix new input query interrupts the old query when the job is not returning results fast enough. I must admit that my way of fixing was not appropriate and I'm open to ideas for any modifications.

for problem detail

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • [x] Test A
local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local sorters = require "telescope.sorters"
local startime
local prompt_bufnr

local picker = function(opts)
    opts = opts or {}
    local lookup_keys = {
        display = 1,
        ordinal = 1,
        value = 1,
    }

    local function gen_from_string(opts)
        local mt_string_entry = {
            __index = function(t, k)
                return rawget(t, rawget(lookup_keys, k))
            end,
        }

        return function(line)
            local endtime = vim.loop.hrtime()
            return setmetatable({
                line .. tostring((endtime - startime) / 1e9),
            }, mt_string_entry)
        end
    end

    -- spawn a bash shell in which echo after 5s 
    local p = pickers.new(opts, {
        prompt_title = "test",
        finder = finders.new_job(function(prompt)
            return { "bash", "-c", "sleep 5; echo input: \"" .. prompt .. "\" cost:" }
        end, gen_from_string()),
        sorter = sorters.highlighter_only(),
    })

    function p:_next_find_id()
        local find_id = self._find_id + 1
        self._find_id = find_id

        startime = vim.loop.hrtime()
        prompt_bufnr = self.prompt_bufnr
        return find_id
    end

    p:find()
end

picker()
vim.defer_fn(function ()
    vim.api.nvim_buf_set_lines(prompt_bufnr, 1, -1, 0, {"123"})
end, 1000)

-- The result should be around 5 seconds; it was about 10 seconds in the original version

Configuration:

  • Neovim version (nvim --version):0.9.4
  • Operating system and version:macOS 12.4

Checklist:

  • [x] My code follows the style guidelines of this project (stylua)
  • [x] I have performed a self-review of my own code
  • [x] I have commented my code, particularly in hard-to-understand areas
  • [x] I have made corresponding changes to the documentation (lua annotations)

Marskey avatar Dec 28 '23 13:12 Marskey

I gotta think about this one a bit.

Currently this approach breaks pickers like telescope-file-browser that has it's own finder wrapper. I don't think we have any of those in core, probably not a common thing in other telescope extensions either but something to consider.

jamestrew avatar Jan 04 '24 02:01 jamestrew