mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 13:13:03 +00:00
Fix whitespace lossage between mirrors
* yasnippet.el (yas--update-mirrors): Delay indentation until all mirrors are updated. (yas--mirror-update-display): Don't indent here. * yasnippet-tests.el (single-line-multi-mirror-indentation-2): New test.
This commit is contained in:
parent
0041efedf9
commit
e74f00e703
@ -244,6 +244,16 @@ $1 ------------------------")
|
|||||||
XXXXX ---------------- XXXXX ----
|
XXXXX ---------------- XXXXX ----
|
||||||
XXXXX ------------------------"))))
|
XXXXX ------------------------"))))
|
||||||
|
|
||||||
|
(ert-deftest single-line-multi-mirror-indentation-2 ()
|
||||||
|
"Like `single-line-multi-mirror-indentation' but 2 mirrors interleaved."
|
||||||
|
;; See also Github issue #768.
|
||||||
|
(with-temp-buffer
|
||||||
|
(c-mode)
|
||||||
|
(yas-minor-mode 1)
|
||||||
|
(yas-expand-snippet "${1:one} ${2:two};\n$1 $2_;\n$2 $1_;\n")
|
||||||
|
(should (string= (yas--buffer-contents)
|
||||||
|
"one two;\none two_;\ntwo one_;\n"))))
|
||||||
|
|
||||||
(ert-deftest indent-org-property ()
|
(ert-deftest indent-org-property ()
|
||||||
"Handling of `org-mode' property indentation, see `org-property-format'."
|
"Handling of `org-mode' property indentation, see `org-property-format'."
|
||||||
;; This is an interesting case because `org-indent-line' calls
|
;; This is an interesting case because `org-indent-line' calls
|
||||||
|
81
yasnippet.el
81
yasnippet.el
@ -4365,44 +4365,42 @@ When multiple expressions are found, only the last one counts."
|
|||||||
(save-restriction
|
(save-restriction
|
||||||
(widen)
|
(widen)
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(dolist (field-and-mirror
|
(cl-loop
|
||||||
(sort
|
for (field . mirror)
|
||||||
;; make a list of ((F1 . M1) (F1 . M2) (F2 . M3) (F2 . M4) ...)
|
in (cl-sort
|
||||||
;; where F is the field that M is mirroring
|
;; Make a list of (FIELD . MIRROR).
|
||||||
;;
|
(cl-mapcan (lambda (field)
|
||||||
(cl-mapcan #'(lambda (field)
|
(mapcar (lambda (mirror)
|
||||||
(mapcar #'(lambda (mirror)
|
(cons field mirror))
|
||||||
(cons field mirror))
|
(yas--field-mirrors field)))
|
||||||
(cl-sort
|
(yas--snippet-fields snippet))
|
||||||
(cl-copy-list
|
;; Then sort this list so that entries with mirrors with
|
||||||
(yas--field-mirrors field))
|
;; parent fields appear before. This was important for
|
||||||
#'<
|
;; fixing #290, and also handles the case where a mirror in
|
||||||
:key #'yas--mirror-start)))
|
;; a field causes another mirror to need reupdating.
|
||||||
(yas--snippet-fields snippet))
|
#'> :key (lambda (fm) (yas--calculate-mirror-depth (cdr fm))))
|
||||||
;; then sort this list so that entries with mirrors with parent
|
;; Before updating a mirror with a parent-field, maybe advance
|
||||||
;; fields appear before. This was important for fixing #290, and
|
;; its start (#290).
|
||||||
;; luckily also handles the case where a mirror in a field causes
|
do (let ((parent-field (yas--mirror-parent-field mirror)))
|
||||||
;; another mirror to need reupdating
|
(when parent-field
|
||||||
;;
|
(yas--advance-start-maybe mirror (yas--fom-start parent-field))))
|
||||||
#'(lambda (field-and-mirror1 field-and-mirror2)
|
;; Update this mirror.
|
||||||
(> (yas--calculate-mirror-depth (cdr field-and-mirror1))
|
do (yas--mirror-update-display mirror field snippet)
|
||||||
(yas--calculate-mirror-depth (cdr field-and-mirror2))))))
|
;; Delay indenting until we're done all mirrors. We must do
|
||||||
(let* ((field (car field-and-mirror))
|
;; this to avoid losing whitespace between fields that are
|
||||||
(mirror (cdr field-and-mirror))
|
;; still empty (i.e., they will be non-empty after updating).
|
||||||
(parent-field (yas--mirror-parent-field mirror)))
|
when (eq yas-indent-line 'auto)
|
||||||
;; before updating a mirror with a parent-field, maybe advance
|
collect (cons (yas--mirror-start mirror) (yas--mirror-end mirror))
|
||||||
;; its start (#290)
|
into indent-regions
|
||||||
;;
|
;; `yas--place-overlays' is needed since the active field and
|
||||||
(when parent-field
|
;; protected overlays might have been changed because of insertions
|
||||||
(yas--advance-start-maybe mirror (yas--fom-start parent-field)))
|
;; in `yas--mirror-update-display'.
|
||||||
;; update this mirror
|
do (let ((active-field (yas--snippet-active-field snippet)))
|
||||||
;;
|
(when active-field (yas--place-overlays snippet active-field)))
|
||||||
(yas--mirror-update-display mirror field snippet)
|
finally do
|
||||||
;; `yas--place-overlays' is needed since the active field and
|
(let ((yas--inhibit-overlay-hooks t))
|
||||||
;; protected overlays might have been changed because of insertions
|
(cl-loop for (beg . end) in (cl-sort indent-regions #'< :key #'car)
|
||||||
;; in `yas--mirror-update-display'.
|
do (yas--indent-region beg end snippet)))))))
|
||||||
(let ((active-field (yas--snippet-active-field snippet)))
|
|
||||||
(when active-field (yas--place-overlays snippet active-field))))))))
|
|
||||||
|
|
||||||
(defun yas--mirror-update-display (mirror field snippet)
|
(defun yas--mirror-update-display (mirror field snippet)
|
||||||
"Update MIRROR according to FIELD (and mirror transform)."
|
"Update MIRROR according to FIELD (and mirror transform)."
|
||||||
@ -4423,12 +4421,7 @@ When multiple expressions are found, only the last one counts."
|
|||||||
(set-marker (yas--mirror-end mirror) (point))
|
(set-marker (yas--mirror-end mirror) (point))
|
||||||
(yas--advance-start-maybe (yas--mirror-next mirror) (point))
|
(yas--advance-start-maybe (yas--mirror-next mirror) (point))
|
||||||
;; super-special advance
|
;; super-special advance
|
||||||
(yas--advance-end-of-parents-maybe mirror-parent-field (point)))
|
(yas--advance-end-of-parents-maybe mirror-parent-field (point))))))
|
||||||
(when (eq yas-indent-line 'auto)
|
|
||||||
(let ((yas--inhibit-overlay-hooks t))
|
|
||||||
(yas--indent-region (yas--mirror-start mirror)
|
|
||||||
(yas--mirror-end mirror)
|
|
||||||
snippet))))))
|
|
||||||
|
|
||||||
(defun yas--field-update-display (field)
|
(defun yas--field-update-display (field)
|
||||||
"Much like `yas--mirror-update-display', but for fields."
|
"Much like `yas--mirror-update-display', but for fields."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user