mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-15 22:13:04 +00:00
refactor: do away with snippet--form-[mirror/field]-p, use pcase and cleaner loops
This commit is contained in:
parent
9927e08d89
commit
c5276642b8
65
snippet.el
65
snippet.el
@ -17,7 +17,6 @@
|
||||
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
;;; Commentary:
|
||||
|
||||
;;
|
||||
@ -29,12 +28,6 @@
|
||||
|
||||
;;; the define-snippet macro and its helpers
|
||||
;;;
|
||||
|
||||
|
||||
(defun snippet--form-field-p (form)
|
||||
(and (consp form) (eq (car form) 'field)))
|
||||
(defun snippet--form-mirror-p (form)
|
||||
(and (consp form) (eq (car form) 'mirror)))
|
||||
(defun snippet--form-make-field-sym (field-name &optional parent-field-sym)
|
||||
(make-symbol (format "field-%s%s" field-name
|
||||
(if parent-field-sym
|
||||
@ -80,16 +73,17 @@ iterated depth-first, resulting in a flattened list."
|
||||
|
||||
for (prev-form form next-form) on `(nil ,@forms)
|
||||
|
||||
for (sym childrenp) = (cond ((snippet--form-field-p form)
|
||||
for (sym childrenp) = (pcase form
|
||||
(`(field ,name . ,rest)
|
||||
(list (snippet--form-make-field-sym
|
||||
(second form)
|
||||
name
|
||||
parent-field-sym)
|
||||
(listp (third form))))
|
||||
((snippet--form-mirror-p form)
|
||||
(listp (car rest))))
|
||||
(`(mirror ,name . ,_)
|
||||
(incf snippet--form-mirror-sym-idx)
|
||||
(list (snippet--form-make-mirror-sym
|
||||
snippet--form-mirror-sym-idx
|
||||
(second form)
|
||||
name
|
||||
parent-field-sym))))
|
||||
|
||||
append (cond (sym
|
||||
@ -150,23 +144,28 @@ I would need these somewhere in the let* form
|
||||
|
||||
|
||||
(defun snippet--first-field-sym (tuples)
|
||||
(first (cl-find-if #'snippet--form-field-p tuples :key #'second)))
|
||||
(first (first (snippet--field-tuples tuples))))
|
||||
|
||||
(defun snippet--field-tuples (tuples)
|
||||
(cl-remove-if-not #'(lambda (form)
|
||||
(and (consp form)
|
||||
(eq 'field (car form))))
|
||||
tuples :key #'cadr))
|
||||
|
||||
(defun snippet--init-field-and-mirror-forms (tuples)
|
||||
(let* ((field-mirrors (make-hash-table))
|
||||
;; we first collect `snippet--make-mirror' forms. When
|
||||
;; collecting them, we populate the `field-mirrors' table...
|
||||
;;
|
||||
(tuples (cl-remove 'string-or-function tuples :key #'car))
|
||||
(make-mirror-forms
|
||||
(loop for (sym form parent-sym) in tuples
|
||||
when (snippet--form-mirror-p form)
|
||||
(loop for (sym (type name transform) parent-sym) in tuples
|
||||
when (eq 'mirror type)
|
||||
collect (let ((source-sym nil))
|
||||
(loop for (sym-b form-b) in tuples
|
||||
(loop for (sym-b (type-b name-b)) in tuples
|
||||
when (and
|
||||
(snippet--form-field-p form-b)
|
||||
(eq (second form)
|
||||
(second form-b)))
|
||||
(eq 'field type-b)
|
||||
(eq name name-b))
|
||||
do
|
||||
(setq source-sym sym-b)
|
||||
(puthash source-sym
|
||||
@ -174,32 +173,30 @@ I would need these somewhere in the let* form
|
||||
field-mirrors))
|
||||
field-mirrors))
|
||||
(unless source-sym
|
||||
(error "mirror %s mentions unknown field"
|
||||
form))
|
||||
(error "mirror mentions unknown field %s"
|
||||
name))
|
||||
`((,sym (snippet--make-mirror))
|
||||
(snippet--init-mirror
|
||||
,sym
|
||||
,source-sym
|
||||
,(snippet--start-marker-name sym)
|
||||
,(snippet--end-marker-name sym)
|
||||
,(snippet--transform-lambda (third form))
|
||||
,(snippet--transform-lambda transform)
|
||||
,parent-sym)))))
|
||||
;; so that we can now create `snippet--make-field' forms with
|
||||
;; complete lists of mirror symbols.
|
||||
;;
|
||||
(make-field-forms
|
||||
(loop with field-tuples = (cl-remove-if-not #'snippet--form-field-p
|
||||
tuples
|
||||
:key #'second)
|
||||
(loop with field-tuples = (snippet--field-tuples tuples)
|
||||
for ((prev-sym)
|
||||
(sym form parent-sym)
|
||||
(sym (_type name _value) parent-sym)
|
||||
(next-sym)) on `(nil ,@field-tuples)
|
||||
when sym
|
||||
|
||||
collect `((,sym (snippet--make-field))
|
||||
(snippet--init-field
|
||||
,sym
|
||||
,(second form)
|
||||
,name
|
||||
,(snippet--start-marker-name sym)
|
||||
,(snippet--end-marker-name sym)
|
||||
,parent-sym
|
||||
@ -230,15 +227,15 @@ I would need these somewhere in the let* form
|
||||
|
||||
,@(loop
|
||||
for (sym form) in sym-tuples
|
||||
collect (cond ((snippet--form-field-p form)
|
||||
`(snippet--insert-field ,sym ,(if (stringp
|
||||
(third form))
|
||||
(third form))))
|
||||
((snippet--form-mirror-p form)
|
||||
collect (pcase form
|
||||
(`(field ,_ ,text)
|
||||
`(snippet--insert-field ,sym ,(if (stringp text)
|
||||
text)))
|
||||
(`(mirror . ,_)
|
||||
`(snippet--insert-mirror ,sym))
|
||||
((stringp form)
|
||||
((pred stringp)
|
||||
`(insert ,form))
|
||||
((snippet--function-p form)
|
||||
((pred functionp)
|
||||
`(insert (funcall ,form)))))
|
||||
|
||||
(setq snippet--field-overlay
|
||||
|
Loading…
x
Reference in New Issue
Block a user