]> code.delx.au - gnu-emacs-elpa/commitdiff
Test and fix problem with mirror+autofill
authorNoam Postavsky <github.10.npostavs@spamgourmet.com>
Fri, 29 Apr 2016 01:16:30 +0000 (21:16 -0400)
committerNoam Postavsky <github.10.npostavs@spamgourmet.com>
Fri, 29 Apr 2016 01:16:30 +0000 (21:16 -0400)
cc-mode fill paragraph narrows to the paragraph being filled.  This
causes problems if there is a mirror needing to be updated outside of
the current paragraph.

* yasnippet-tests.el: New test.
* yasnippet.el (yas--update-mirrors): Widen while updating the mirrors.

yasnippet-tests.el
yasnippet.el

index 844cd943ba85b63fe40e8b86b31ee32d870f9868..e5c72df618f92553b05dec5cd270a22f86216f52 100644 (file)
     (should (string= (yas--buffer-contents)
                      "bla from another BLA"))))
 
+(ert-deftest mirror-with-transformation-and-autofill ()
+  "Test interaction of autofill with mirror transforms"
+  (let ((words "one two three four five")
+        filled-words)
+    (with-temp-buffer
+      (c-mode)      ; In `c-mode' filling comments works by narrowing.
+      (yas-minor-mode +1)
+      (setq fill-column 10)
+      (auto-fill-mode +1)
+      (yas-expand-snippet "/* $0\n */")
+      (yas-mock-insert words)
+      (setq filled-words (delete-and-extract-region (point-min) (point-max)))
+      (yas-expand-snippet "/* $1\n */\n$2$2")
+      (should (string= (yas--buffer-contents)
+                       "/* \n */\n"))
+      (yas-mock-insert words)
+      (should (string= (yas--buffer-contents)
+                       (concat filled-words "\n"))))))
+
+
 (ert-deftest primary-field-transformation ()
   (with-temp-buffer
     (yas-minor-mode 1)
index 24cb653080b345e783c30bd1287a2cf317ab738e..93b21f51233ebef69ee7a0202c38ebd351e338a4 100644 (file)
@@ -4192,42 +4192,44 @@ When multiple expressions are found, only the last one counts."
 
 (defun yas--update-mirrors (snippet)
   "Update all the mirrors of SNIPPET."
-  (save-excursion
-    (dolist (field-and-mirror
-             (sort
-              ;; make a list of ((F1 . M1) (F1 . M2) (F2 . M3) (F2 . M4) ...)
-              ;; where F is the field that M is mirroring
-              ;;
-              (cl-mapcan #'(lambda (field)
-                             (mapcar #'(lambda (mirror)
-                                         (cons field mirror))
-                                     (yas--field-mirrors field)))
-                         (yas--snippet-fields snippet))
-              ;; then sort this list so that entries with mirrors with parent
-              ;; fields appear before. This was important for fixing #290, and
-              ;; luckily also handles the case where a mirror in a field causes
-              ;; another mirror to need reupdating
-              ;;
-              #'(lambda (field-and-mirror1 field-and-mirror2)
-                  (> (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)))
-        ;; before updating a mirror with a parent-field, maybe advance
-        ;; its start (#290)
-        ;;
-        (when parent-field
-          (yas--advance-start-maybe mirror (yas--fom-start parent-field)))
-        ;; update this mirror
-        ;;
-        (yas--mirror-update-display mirror field)
-        ;; `yas--place-overlays' is needed if the active field and
-        ;; protected overlays have been changed because of insertions
-        ;; in `yas--mirror-update-display'
-        ;;
-        (when (eq field (yas--snippet-active-field snippet))
-          (yas--place-overlays snippet field))))))
+  (save-restriction
+    (widen)
+    (save-excursion
+     (dolist (field-and-mirror
+              (sort
+               ;; make a list of ((F1 . M1) (F1 . M2) (F2 . M3) (F2 . M4) ...)
+               ;; where F is the field that M is mirroring
+               ;;
+               (cl-mapcan #'(lambda (field)
+                              (mapcar #'(lambda (mirror)
+                                          (cons field mirror))
+                                      (yas--field-mirrors field)))
+                          (yas--snippet-fields snippet))
+               ;; then sort this list so that entries with mirrors with parent
+               ;; fields appear before. This was important for fixing #290, and
+               ;; luckily also handles the case where a mirror in a field causes
+               ;; another mirror to need reupdating
+               ;;
+               #'(lambda (field-and-mirror1 field-and-mirror2)
+                   (> (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)))
+         ;; before updating a mirror with a parent-field, maybe advance
+         ;; its start (#290)
+         ;;
+         (when parent-field
+           (yas--advance-start-maybe mirror (yas--fom-start parent-field)))
+         ;; update this mirror
+         ;;
+         (yas--mirror-update-display mirror field)
+         ;; `yas--place-overlays' is needed if the active field and
+         ;; protected overlays have been changed because of insertions
+         ;; in `yas--mirror-update-display'
+         ;;
+         (when (eq field (yas--snippet-active-field snippet))
+           (yas--place-overlays snippet field)))))))
 
 (defun yas--mirror-update-display (mirror field)
   "Update MIRROR according to FIELD (and mirror transform)."