mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 21:13:04 +00:00
Refactor yas-next-field a bit
* yasnippet.el (yas--find-next-field): New function to find target field. (yas-next-field): Use it.
This commit is contained in:
parent
bfe14f19ac
commit
cc14db399c
38
yasnippet.el
38
yasnippet.el
@ -3063,38 +3063,36 @@ Otherwise delegate to `yas-next-field'."
|
|||||||
(yas-next-field))))
|
(yas-next-field))))
|
||||||
(yas-next-field)))
|
(yas-next-field)))
|
||||||
|
|
||||||
|
(defun yas--find-next-field (n snippet active)
|
||||||
|
"Return the Nth field after the ACTIVE one in SNIPPET."
|
||||||
|
(let ((live-fields (cl-remove-if
|
||||||
|
(lambda (field)
|
||||||
|
(and (not (eq field active))
|
||||||
|
(yas--field-probably-deleted-p snippet field)))
|
||||||
|
(yas--snippet-fields snippet))))
|
||||||
|
(if (>= n 0) (nth n (memq active live-fields))
|
||||||
|
(car (last (memq active (reverse live-fields)) (- n))))))
|
||||||
|
|
||||||
(defun yas-next-field (&optional arg)
|
(defun yas-next-field (&optional arg)
|
||||||
"Navigate to the ARGth next field.
|
"Navigate to the ARGth next field.
|
||||||
|
|
||||||
If there's none, exit the snippet."
|
If there's none, exit the snippet."
|
||||||
(interactive)
|
(interactive)
|
||||||
(let* ((arg (or arg
|
(unless arg (setq arg 1))
|
||||||
1))
|
(let* ((snippet (car (yas--snippets-at-point)))
|
||||||
(snippet (first (yas--snippets-at-point)))
|
|
||||||
(active-field (overlay-get yas--active-field-overlay 'yas--field))
|
(active-field (overlay-get yas--active-field-overlay 'yas--field))
|
||||||
(live-fields (remove-if #'(lambda (field)
|
(target-field (yas--find-next-field arg snippet active-field)))
|
||||||
(and (not (eq field active-field))
|
;; First check if we're moving out of a field with a transform.
|
||||||
(yas--field-probably-deleted-p snippet field)))
|
(when (and active-field (yas--field-transform active-field))
|
||||||
(yas--snippet-fields snippet)))
|
|
||||||
(active-field-pos (position active-field live-fields))
|
|
||||||
(target-pos (and active-field-pos (+ arg active-field-pos)))
|
|
||||||
(target-field (and target-pos (nth target-pos live-fields))))
|
|
||||||
;; First check if we're moving out of a field with a transform
|
|
||||||
;;
|
|
||||||
(when (and active-field
|
|
||||||
(yas--field-transform active-field))
|
|
||||||
(let* ((yas-moving-away-p t)
|
(let* ((yas-moving-away-p t)
|
||||||
(yas-text (yas--field-text-for-display active-field))
|
(yas-text (yas--field-text-for-display active-field))
|
||||||
(yas-modified-p (yas--field-modified-p active-field)))
|
(yas-modified-p (yas--field-modified-p active-field)))
|
||||||
;; primary field transform: exit call to field-transform
|
;; primary field transform: exit call to field-transform
|
||||||
(yas--eval-lisp (yas--field-transform active-field))))
|
(yas--eval-lisp (yas--field-transform active-field))))
|
||||||
;; Now actually move...
|
;; Now actually move...
|
||||||
(cond ((and target-pos (>= target-pos (length live-fields)))
|
(if target-field
|
||||||
(yas-exit-snippet snippet))
|
(yas--move-to-field snippet target-field)
|
||||||
(target-field
|
(yas-exit-snippet snippet))))
|
||||||
(yas--move-to-field snippet target-field))
|
|
||||||
(t
|
|
||||||
nil))))
|
|
||||||
|
|
||||||
(defun yas--place-overlays (snippet field)
|
(defun yas--place-overlays (snippet field)
|
||||||
"Correctly place overlays for SNIPPET's FIELD."
|
"Correctly place overlays for SNIPPET's FIELD."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user