abo-abo / swiper

Ivy - a generic completion frontend for Emacs, Swiper - isearch with an overview, and more. Oh, man!
https://oremacs.com/swiper/
2.31k stars 338 forks source link

[Feature Reqeust] General Function to build async candidates #2110

Open cireu opened 5 years ago

cireu commented 5 years ago

We have counsel--async-command to call shell command for completion candidates, however this command doesn't suitable for generalized async work. E.g.url-retrieve.

I try a beta version of async callback to insert candidate to current ivy completing session

(defun ivy--async-callback (result)
  "Insert RESULT to current ivy completing session."
  (when (eq this-command (ivy-state-caller ivy-last))
    (let* ((len (length result))
           (ivy--prompt (format "%d++ %s"
                                len (ivy-state-prompt ivy-last))))
      (ivy--set-candidates (if ivy--all-candidates
                               (append ivy--all-candidates result)
                             result))
      (ivy--insert-minibuffer (ivy--format ivy--all-candidates)))))
abo-abo commented 5 years ago

Nice idea. And the code looks good. Can you post a full ivy-read demo?

I think it would be nice to:

cireu commented 5 years ago

introduce this function

My idea is to introduce such API.

(defun ivy-build-async-candidates (fetcher &optional awaiting-message)
  "Build a asynchronous candidates collection for ivy completing session.

FETCHER should accept `ivy--async-callback' as arguements, and call it
each time new candidates arrives with candidates, candidates should be
 a string or a list of strings.

AWAITING-MESSAGE will be shown when there's no candidates have been arrived.
"
  (error "Unimplemented!"))

But there are some problems I encountered and some behaviours I can't confirm.

  1. How to do the clean up? If a completing session was aborted manually (like C-g), callback should stop accepting and inserting candidates. Should we force user to pass :unwind to ivy-read to do this (like counsel--async-command based fucntions) or modified the definition of ivy-read to do the clean up automatically? Or someone have better idea.

  2. What kind of result should be called with ivy--async-callback, a list of strings or more?

maybe make it use a logic similar to counsel-async-filter-update-time if needed, have counsel--async-filter re-use this new function.

I think these can be easily done, if we can confirm the choive above.