Indent multi-line output from mirrors

* yasnippet.el (yas--indent-region): Adapted and redesigned from
`yas--indent-according-to-mode'
(yas--indent-according-to-mode): Deleted.
(yas--indent): Use `yas--indent-region'.
(yas--real-line-beginning): Deleted.
(yas--update-mirrors): Always re-place active field overlay if it
exists.  Pass the snippet to `yas--mirror-update-display'.
(yas--mirror-update-display): Indent the mirror text after
inserting the reflection.
This commit is contained in:
João Távora 2016-03-04 17:34:40 +00:00 committed by Noam Postavsky
parent eeb4b62efb
commit eca2f995c1

View File

@ -3869,8 +3869,9 @@ Meant to be called in a narrowed buffer, does various passes"
(goto-char parse-start) (goto-char parse-start)
(yas--indent snippet))) (yas--indent snippet)))
(defun yas--indent-according-to-mode (snippet-markers) (defun yas--indent-region (from to snippet)
"Indent current line according to mode, preserving SNIPPET-MARKERS." "Indent the lines between FROM and TO with `indent-according-to-mode'.
The SNIPPET's markers are preserved."
;;; Apropos indenting problems.... ;;; Apropos indenting problems....
;; ;;
;; `indent-according-to-mode' uses whatever `indent-line-function' ;; `indent-according-to-mode' uses whatever `indent-line-function'
@ -3883,54 +3884,50 @@ Meant to be called in a narrowed buffer, does various passes"
;; `front-advance' property set to nil. ;; `front-advance' property set to nil.
;; ;;
;; This is why I have these `trouble-markers', they are the ones at ;; This is why I have these `trouble-markers', they are the ones at
;; they are the ones at the first non-whitespace char at the line ;; the first non-whitespace char at the line. After indentation
;; (i.e. at `yas--real-line-beginning'. After indentation takes place ;; takes place we should be at the correct to restore them. All
;; we should be at the correct to restore them to. All other ;; other non-trouble-markers should have been *pushed* and don't
;; non-trouble-markers have been *pushed* and don't need special ;; need special attention.
;; attention. (let* ((snippet-markers (yas--collect-snippet-markers snippet))
;; (to (set-marker (make-marker) to)))
(goto-char (yas--real-line-beginning)) (save-excursion
(let ((trouble-markers (remove-if-not #'(lambda (marker) (goto-char from)
(= marker (point)))
snippet-markers)))
(save-restriction (save-restriction
(widen) (widen)
(condition-case _ (cl-loop do
(back-to-indentation)
(let ((trouble-markers ; The markers at (point).
(cl-remove (point) snippet-markers :test #'/=)))
(unwind-protect
(indent-according-to-mode) (indent-according-to-mode)
(error (yas--message 3 "Warning: `yas--indent-according-to-mode' having problems running %s" indent-line-function) (dolist (marker trouble-markers)
nil))) (set-marker marker (point)))))
(mapc #'(lambda (marker) while (and (zerop (forward-line 1))
(set-marker marker (point))) (< (point) to)))))))
trouble-markers)))
(defvar yas--indent-original-column nil) (defvar yas--indent-original-column nil)
(defun yas--indent (snippet) (defun yas--indent (snippet)
(let ((snippet-markers (yas--collect-snippet-markers snippet))) ;; Look for those `$>'.
;; Look for those $>
(save-excursion (save-excursion
(while (re-search-forward "$>" nil t) (while (re-search-forward "$>" nil t)
(delete-region (match-beginning 0) (match-end 0)) (delete-region (match-beginning 0) (match-end 0))
(when (not (eq yas-indent-line 'auto)) (unless (eq yas-indent-line 'auto)
(yas--indent-according-to-mode snippet-markers)))) (yas--indent-region (line-beginning-position)
;; Now do stuff for 'fixed and 'auto (line-end-position)
snippet))))
;; Now do stuff for `fixed' and `auto'.
(save-excursion (save-excursion
(cond ((eq yas-indent-line 'fixed) (cond ((eq yas-indent-line 'fixed)
(while (and (zerop (forward-line)) (while (and (zerop (forward-line))
(zerop (current-column))) (zerop (current-column)))
(indent-to-column yas--indent-original-column))) (indent-to-column yas--indent-original-column)))
((eq yas-indent-line 'auto) ((eq yas-indent-line 'auto)
(let ((end (set-marker (make-marker) (point-max))) (let ((end (set-marker (make-marker) (point-max))))
(indent-first-line-p yas-also-auto-indent-first-line)) (unless yas-also-auto-indent-first-line
(while (and (zerop (if indent-first-line-p (forward-line 1))
(prog1 (yas--indent-region (line-beginning-position)
(forward-line 0) (point-max)
(setq indent-first-line-p nil)) snippet))))))
(forward-line 1)))
(not (eobp))
(<= (point) end))
(yas--indent-according-to-mode snippet-markers))))
(t
nil)))))
(defun yas--collect-snippet-markers (snippet) (defun yas--collect-snippet-markers (snippet)
"Make a list of all the markers used by SNIPPET." "Make a list of all the markers used by SNIPPET."
@ -3947,15 +3944,6 @@ Meant to be called in a narrowed buffer, does various passes"
(push (yas--exit-marker snippet-exit) markers))) (push (yas--exit-marker snippet-exit) markers)))
markers)) markers))
(defun yas--real-line-beginning ()
(let ((c (char-after (line-beginning-position)))
(n (line-beginning-position)))
(while (or (eql c ?\ )
(eql c ?\t))
(cl-incf n)
(setq c (char-after n)))
n))
(defun yas--escape-string (escaped) (defun yas--escape-string (escaped)
(concat "YASESCAPE" (format "%d" escaped) "PROTECTGUARD")) (concat "YASESCAPE" (format "%d" escaped) "PROTECTGUARD"))
@ -4228,15 +4216,14 @@ When multiple expressions are found, only the last one counts."
(yas--advance-start-maybe mirror (yas--fom-start parent-field))) (yas--advance-start-maybe mirror (yas--fom-start parent-field)))
;; update this mirror ;; update this mirror
;; ;;
(yas--mirror-update-display mirror field) (yas--mirror-update-display mirror field snippet)
;; `yas--place-overlays' is needed if the active field and ;; `yas--place-overlays' is needed since the active field and
;; protected overlays have been changed because of insertions ;; protected overlays might have been changed because of insertions
;; in `yas--mirror-update-display' ;; in `yas--mirror-update-display'.
;; (let ((active-field (yas--snippet-active-field snippet)))
(when (eq field (yas--snippet-active-field snippet)) (when active-field (yas--place-overlays snippet active-field))))))))
(yas--place-overlays snippet field)))))))
(defun yas--mirror-update-display (mirror field) (defun yas--mirror-update-display (mirror field snippet)
"Update MIRROR according to FIELD (and mirror transform)." "Update MIRROR according to FIELD (and mirror transform)."
(let* ((mirror-parent-field (yas--mirror-parent-field mirror)) (let* ((mirror-parent-field (yas--mirror-parent-field mirror))
@ -4255,7 +4242,11 @@ 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)))
(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."