elken / yasnippet-capf

Completion-At-Point Extension for YASnippet
GNU General Public License v3.0
40 stars 8 forks source link

Completion at point not filling the snippet when yasnippet-capf-lookup-by is name #24

Closed zauberen closed 1 month ago

zauberen commented 1 month ago

The problem is that if the key and name are not the same, when the capf runs yas-expand on exit nothing happens.

I am using yasnippet-snippets for my snippets, so in java I have a snippet with a name of println and a key of pr. If I type in pr and call yas-expand, the snippet works. If I type in println and call yas-expand it does not.

I have a fix to this, will open a PR later today.

elken commented 1 month ago

I can't remember hitting this so I'm not convinced there's an issue but I'll try and look into it when I can.

zauberen commented 1 month ago

Sure, I've thrown together a minimal configuration to reproduce the bug in emacs 29.4 if that helps.

Steps:

  1. Launch emacs with this configuration in a clean init dir emacs --init-dir .emacs.d.test
  2. Switch to the scratch buffer
  3. Type in Com then M-x yasnippet-capf. This just completes the name of the snippet, reproducing the bug.
  4. You can tell yasnippet-capf works with keys in this test by completing a snippet where the key = the name, such as defun which works as expected.
;;; init.el --- Test emacs config
;;; Commentary:
;;; Minimal test environment with package.el
;;; Code:

;; Package management ;;
(require 'package)
(setq package-selected-packages
      '( use-package ))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(eval-when-compile
  (package-install-selected-packages t)
  (require 'use-package))
;; Add fido to make m-x completion easier
(fido-mode)

(use-package yasnippet-snippets
  :ensure t)
(use-package yasnippet
  :ensure t
  :after yasnippet-snippets
  :config
  (yas-global-mode 1))
(use-package cape
  :ensure t)
(use-package corfu
  :ensure t
  :after cape
  :config
  (global-corfu-mode))
(use-package yasnippet-capf
  :after cape
  :ensure t
  :config
  (add-to-list 'completion-at-point-functions #'yasnippet-capf)
  (setq yasnippet-capf-lookup-by 'name))
;;; init.el ends here
zauberen commented 1 month ago

The configuration for the test snippet can be found here: https://github.com/AndreaCrotti/yasnippet-snippets/blob/1bf034887c4048c38266842686b7f9c8384f72e7/snippets/emacs-lisp-mode/word-or-region

This snippet works fine with yas-insert-snippet or yas-expand on word-or-region and it works in my PR.

elken commented 1 month ago

Type in Com then M-x yasnippet-capf.

Ah, so that's the issue. If you want to call something from M-x then just call yas-expand itself. I can repro this but that's exactly why it shouldn't be used. I can't get it to trigger any other way using your init, but I'll merge it anyway.