Skip to content

Commit

Permalink
Fix fields following $0 + region insertion
Browse files Browse the repository at this point in the history
When inserting text due to a  non-nil yas-wrap-around-region and the
snippet had fields occuring later in the buffer than $0, the insertion
of the text was shifting the location of later fields, thus invalidating
the calculation of yas--dollar-regions.

* yasnippet.el (yas--simple-mirror-parse-create): Don't insert text fo
yas-wrap-around-region here.
(yas--snippet-parse-create): Do it here, after deleting
`yas--dollar-regions`, instead.
  • Loading branch information
npostavs committed May 1, 2016
1 parent db4cd31 commit beb2ba4
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions yasnippet.el
Expand Up @@ -3862,7 +3862,22 @@ Meant to be called in a narrowed buffer, does various passes"
(yas--calculate-adjacencies snippet)
;; Delete $-constructs
;;
(save-restriction (widen) (yas--delete-regions yas--dollar-regions))
(save-restriction
(widen)
(yas--delete-regions yas--dollar-regions))
;; Make sure to do this insertion *after* deleting the dollar
;; regions, otherwise we invalidate the calculated positions of
;; all the fields following $0.
(let ((exit (yas--snippet-exit snippet)))
(goto-char (if exit (yas--exit-marker exit) (point-max))))
(when (eq yas-wrap-around-region 'cua)
(setq yas-wrap-around-region ?0))
(cond ((and yas-wrap-around-region yas-selected-text)
(insert yas-selected-text))
((and (characterp yas-wrap-around-region)
(get-register yas-wrap-around-region))
(insert (prog1 (get-register yas-wrap-around-region)
(set-register yas-wrap-around-region nil)))))
;; restore backquoted expression values
;;
(yas--restore-backquotes)
Expand Down Expand Up @@ -4131,21 +4146,10 @@ When multiple expressions are found, only the last one counts."
(while (re-search-forward yas--simple-mirror-regexp nil t)
(let ((number (string-to-number (match-string-no-properties 1))))
(cond ((zerop number)

(setf (yas--snippet-exit snippet)
(yas--make-exit (yas--make-marker (match-end 0))))
(save-excursion
(goto-char (match-beginning 0))
(when (eq yas-wrap-around-region 'cua)
(setq yas-wrap-around-region ?0))
(cond ((and yas-wrap-around-region yas-selected-text)
(insert yas-selected-text))
((and (characterp yas-wrap-around-region)
(get-register yas-wrap-around-region))
(insert (prog1 (get-register yas-wrap-around-region)
(set-register yas-wrap-around-region nil)))))
(push (cons (point) (yas--exit-marker (yas--snippet-exit snippet)))
yas--dollar-regions)))
(push (cons (match-beginning 0) (yas--exit-marker (yas--snippet-exit snippet)))
yas--dollar-regions))
(t
(let ((field (yas--snippet-find-field snippet number)))
(if field
Expand Down

0 comments on commit beb2ba4

Please sign in to comment.