Properly prioritize private snippets #852

In the previous solution, if *any* private snippets existed, *all*
non-private snippets were hidden.
This commit is contained in:
Henrik Lissner 2018-09-03 02:15:10 +02:00
parent eaa10946f1
commit 596705dbda
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -1,21 +1,30 @@
;;; feature/snippets/autoload/snippets.el -*- lexical-binding: t; -*- ;;; feature/snippets/autoload/snippets.el -*- lexical-binding: t; -*-
(defun +snippets--remove-p (x y)
(and (equal (yas--template-key x) (yas--template-key y))
(file-in-directory-p (yas--template-get-file x) doom-emacs-dir)))
;;;###autoload ;;;###autoload
(defun +snippets-prompt-private (prompt choices &optional fn) (defun +snippets-prompt-private (prompt choices &optional fn)
"Prioritize private snippets (in `+snippets-dir') over built-in ones if there "Prioritize private snippets over built-in ones if there are multiple
are multiple choices." choices.
(when-let*
((choices There are two groups of snippets in Doom Emacs. The built in ones (under
(or (cl-loop for tpl in choices `doom-emacs-dir'; provided by Doom or its plugins) or your private snippets
for file = (yas--template-get-file tpl) (outside of `doom-eamcs-dir').
if (or (null file)
(file-in-directory-p file +snippets-dir)) If there are multiple snippets with the same key in either camp (but not both),
collect tpl) you will be prompted to select one.
choices)))
(if (cdr choices) If there are conflicting keys across the two camps, the built-in ones are
(let ((prompt-functions (remq '+snippets-prompt-private yas-prompt-functions))) ignored. This makes it easy to override built-in snippets with private ones."
(run-hook-with-args-until-success 'prompt-functions prompt choices fn)) (when (memq this-command '(yas-expand +snippets/expand-on-region))
(car choices)))) (let* ((gc-cons-threshold doom-gc-cons-upper-limit)
(choices (cl-remove-duplicates choices :test #'+snippets--remove-p)))
(if (cdr choices)
(let ((prompt-functions (remq '+snippets-prompt-private yas-prompt-functions)))
(run-hook-with-args-until-success 'prompt-functions prompt choices fn))
(car choices)))))
;;;###autoload ;;;###autoload
(defun +snippets/goto-start-of-field () (defun +snippets/goto-start-of-field ()