sly icon indicating copy to clipboard operation
sly copied to clipboard

How to add yasnippet-capf to completion-at-point-functions?

Open kflak opened this issue 1 year ago • 7 comments

Hi,

I am trying to add yasnippet-capf to sly's completion-at-point-functions, to ensure that the snippets show up in completion-preview-mode, but anything I try, short of manually running (add-to-list 'completion-at-point-functions #'yasnippet-capf) in the lisp buffer fails.

So far I have tried

(use-package yasnippet-capf
  :ensure t
  :after cape
  :config
  (add-to-list 'completion-at-point-functions #'yasnippet-capf))

and

(add-hook 'lisp-mode-hook
          (lambda ()
            (add-to-list 'completion-at-point-functions #'yasnippet-capf)))

but whenever I get to a lisp buffer, the value of completions-at-point-functions is

(sly-complete-filename-maybe
 sly-complete-symbol)

Grateful for any pointers on how to accomplish this!

kflak avatar Feb 22 '25 10:02 kflak

Grateful for any pointers on how to accomplish this!

I haven't had time to look at your problem, but I'll just note the following:

FOO-hook and FOO-functions are hooks and need to be manipulated with hook-manipulating functions like add-hook, remove-hook, not add-to-list.

monnier avatar Feb 22 '25 14:02 monnier

Hmmm... I'm just using the strategy as suggested by the yasnippet-capf instructions, so not sure how to tweak that into a hook beyond just adding it to the lisp-hook?

kflak avatar Feb 22 '25 17:02 kflak

Hmmm... I'm just using the strategy as suggested by the yasnippet-capf instructions, so not sure how to tweak that into a hook beyond just adding it to the lisp-hook?

I suggest you bring it up to the yasnippet-capf maintainers.

monnier avatar Feb 22 '25 20:02 monnier

No need. I think you just have to add the hook to sly-mode-hook.

(with-eval-after-load 'sly
  (add-hook 'sly-mode-hook
            (lambda ()
              (add-hook 'completion-at-point-functions #'yasnippet-capf nil t))))

joaotavora avatar Feb 22 '25 22:02 joaotavora

(with-eval-after-load 'sly
  (add-hook 'sly-mode-hook
            (lambda ()
              (add-hook 'completion-at-point-functions #'yasnippet-capf nil t))))

That should do it, indeed, tho it's still worth telling the yasnippet-capf maintainer to fix their doc. BTW, I think you can skip the with-eval-after-load.

monnier avatar Feb 23 '25 03:02 monnier

Weird, still not working...

kflak avatar Feb 23 '25 05:02 kflak

Weird, still not working...

I see that sly--setup-completion does not follow my adivce (it uses setq-local rather than add/remove-hook to manipulate completion-at-point-functions), so indeed you have to be careful to do your add-hook after sly--setup-completion runs.

Maybe the patch below is in order. João?

diff --git a/lib/sly-completion.el b/lib/sly-completion.el
index 9a17a062b3..b253636dde 100644
--- a/lib/sly-completion.el
+++ b/lib/sly-completion.el
@@ -358,8 +358,8 @@ Intended to go into `completion-at-point-functions'"
 (defun sly--setup-completion ()
   ;; This one can be customized by a SLY user in `sly-mode-hook'
   ;;
-  (setq-local completion-at-point-functions '(sly-complete-filename-maybe
-                                              sly-complete-symbol))
+  (dolist (f '(sly-complete-symbol sly-complete-filename-maybe))
+    (add-hook 'completion-at-point-functions f nil t))
   (add-function :around (local 'completion-in-region-function)
                 #'sly--completion-in-region-function
                 '((name . sly--setup-completion))))

monnier avatar Feb 23 '25 05:02 monnier