Skip to content

Commit

Permalink
(feature): Support accessing snippets for all modes
Browse files Browse the repository at this point in the history
Closes #4

+ Make consult-yasnippet read the template in the function body, instead
  of the interactive statement.
+ Change the function signature of consult-yasnippet to an optional
  argument that triggers the reading of snippets from all snippet
  tables.
+ Add consult-yasnippet--annotate, which suffixes each candidate with
  name of the snippet table it came from.
  • Loading branch information
mohkale committed Nov 21, 2021
1 parent 5a053c0 commit cad6901
Showing 1 changed file with 52 additions and 24 deletions.
76 changes: 52 additions & 24 deletions consult-yasnippet.el
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

;;; Code:

(require 'map)
(require 'consult)
(require 'yasnippet)

Expand Down Expand Up @@ -89,25 +90,39 @@ the state of the current buffer to before any snippets were previewed."
(setcdr region (- (point-max) orig-offset))))
(redisplay)))))))

(defun consult-yasnippet--candidates ()
"Retrieve the list of available snippets in the current buffer."
(unless (bound-and-true-p yas-minor-mode)
(error "`yas-minor-mode' not enabled in current buffer"))

(defun consult-yasnippet--candidates (templates &optional include-table)
"Convert TEMPLATES into candidates for completing-read.
If TEMPLATES is ommitted, it defaults to all the available
templates in the current buffer. When INCLUDE-TABLE is non-nil
then each candidate string will be prefixed with the snippet
table."
(mapcar
(lambda (template)
(cons (concat (yas--template-name template)
(concat
" ["
(propertize (or (yas--template-key template)
(and (functionp 'yas--template-regexp-key)
(yas--template-regexp-key template)))
'face 'consult-key)
"]"))
(cons (concat
(when include-table
(propertize (concat (yas--table-name (yas--template-table template))
" ")
'invisible t))
(yas--template-name template)
" ["
(propertize (or (yas--template-key template)
(and (functionp 'yas--template-regexp-key)
(yas--template-regexp-key template)))
'face 'consult-key)
"]")
template))
(yas--all-templates (yas--get-snippet-tables))))

(defun consult-yasnippet--read-template ()
templates))

(defun consult-yasnippet--annotate (candidates)
(lambda (cand)
(when-let ((template (cdr (assoc cand candidates)))
(table-name (yas--table-name (yas--template-table template))))
(concat
" "
(propertize " " 'display `(space :align-to (- right ,(+ 1 (length table-name)))))
table-name))))

(defun consult-yasnippet--read-template (&optional all-templates)
"Backend implementation of `consult-yasnippet'.
This starts a `completing-read' session with all the snippets in the current
Expand All @@ -116,12 +131,22 @@ replacing the active region with the snippet expansion.
This function doesn't actually expand the snippet, it only reads and then
returns a snippet template from the user."
(unless (bound-and-true-p yas-minor-mode)
(error "`consult-yasnippet' can only be called while `yas-minor-mode' is active"))

(barf-if-buffer-read-only)

(let* ((buffer-undo-list t)) ; Prevent querying user (and showing previews) from updating the undo-history
(let* ((buffer-undo-list t) ; Prevent querying user (and showing previews) from updating the undo-history
(candidates
(consult-yasnippet--candidates
(if all-templates
(yas--all-templates (map-values yas--tables))
(yas--all-templates (yas--get-snippet-tables)))
all-templates)))
(consult--read
(consult-yasnippet--candidates)
candidates
:prompt "Choose a snippet: "
:annotate (consult-yasnippet--annotate candidates)
:lookup 'consult--lookup-cdr
:require-match t
:state (consult-yasnippet--preview)
Expand All @@ -137,16 +162,19 @@ using `yas--visit-snippet-file-1'."
(yas--visit-snippet-file-1 template))

;;;###autoload
(defun consult-yasnippet (template)
(defun consult-yasnippet (&optional all-templates)
"Interactively select and expand the yasnippet template TEMPLATE.
When called interactively this command presents a completing read interface
containing all currently available snippet expansions, with live previews for
each snippet. Once selected a chosen snippet will be expanded at point using
`yas-expand-snippet'."
(interactive (list (consult-yasnippet--read-template)))
(yas-expand-snippet (yas--template-content template)
nil nil
(yas--template-expand-env template)))
`yas-expand-snippet'.
With ARG select snippets from all snippet tables, not just the current one."
(interactive "P")
(when-let ((template (consult-yasnippet--read-template all-templates)))
(yas-expand-snippet (yas--template-content template)
nil nil
(yas--template-expand-env template))))

(provide 'consult-yasnippet)
;;; consult-yasnippet.el ends here

0 comments on commit cad6901

Please sign in to comment.