Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(asyncjob): interrupt long time job #2834

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Marskey
Copy link
Contributor

@Marskey Marskey commented Dec 28, 2023

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?

  • 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:

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

@jamestrew
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants