Skip to content

Commit

Permalink
Closes #351: sort mirrors by nesting depth when updating
Browse files Browse the repository at this point in the history
  • Loading branch information
joaotavora committed Dec 25, 2012
1 parent 04970ab commit 74e8f43
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 7 additions & 0 deletions yasnippet-tests.el
Expand Up @@ -105,6 +105,13 @@
(should (string= (yas--buffer-contents)
"<%= f.submit \"Send\", :disable_with => 'Sending...' %>")))

(ert-deftest deep-nested-mirroring-issue-351 ()
(with-temp-buffer
(yas-minor-mode 1)
(yas-expand-snippet "${1:FOOOOOOO}${2:$1}${3:$2}${4:$3}")
(ert-simulate-command `(yas-mock-insert "abc"))
(should (string= (yas--buffer-contents) "abcabcabcabc"))))

;; (ert-deftest in-snippet-undo ()
;; (with-temp-buffer
;; (yas-minor-mode 1)
Expand Down
26 changes: 24 additions & 2 deletions yasnippet.el
Expand Up @@ -2923,7 +2923,8 @@ Use this in primary and mirror transformations to tget."
start end
(transform nil)
parent-field
next)
next
depth)

(defstruct (yas--exit (:constructor yas--make-exit (marker)))
marker
Expand Down Expand Up @@ -4155,6 +4156,26 @@ When multiple expressions are found, only the last one counts."
#'(lambda (r1 r2)
(>= (car r1) (car r2))))))

(defun yas--calculate-mirror-depth (mirror &optional traversed)
(let* ((parent (yas--mirror-parent-field mirror))
(parents-mirrors (and parent
(yas--field-mirrors parent))))
(or (yas--mirror-depth mirror)
(setf (yas--mirror-depth mirror)
(cond ((memq mirror traversed)
0)
((and parent parents-mirrors)
(1+ (reduce #'max
(mapcar #'(lambda (m)
(yas--calculate-mirror-depth m
(cons mirror
traversed)))
parents-mirrors))))
(parent
1)
(t
0))))))

(defun yas--update-mirrors (snippet)
"Updates all the mirrors of SNIPPET."
(save-excursion
Expand All @@ -4173,7 +4194,8 @@ When multiple expressions are found, only the last one counts."
;; another mirror to need reupdating
;;
#'(lambda (field-and-mirror1 field-and-mirror2)
(yas--mirror-parent-field (cdr field-and-mirror1)))))
(> (yas--calculate-mirror-depth (cdr field-and-mirror1))
(yas--calculate-mirror-depth (cdr field-and-mirror2))))))
(let* ((field (car field-and-mirror))
(mirror (cdr field-and-mirror))
(parent-field (yas--mirror-parent-field mirror)))
Expand Down

0 comments on commit 74e8f43

Please sign in to comment.