From cad690115f78660ff85cbe60631212cf22cccff2 Mon Sep 17 00:00:00 2001 From: Mohsin Kaleem Date: Sat, 20 Nov 2021 21:21:14 +0000 Subject: [PATCH] (feature): Support accessing snippets for all modes 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. --- consult-yasnippet.el | 76 ++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/consult-yasnippet.el b/consult-yasnippet.el index 09e6cb6..cd2262f 100644 --- a/consult-yasnippet.el +++ b/consult-yasnippet.el @@ -27,6 +27,7 @@ ;;; Code: +(require 'map) (require 'consult) (require 'yasnippet) @@ -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 @@ -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) @@ -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