Fix problems with auto-fill-mode interaction

* yasnippet-tests.el (auto-fill-with-multiparagraph): New test.
* yasnippet.el (yas--auto-fill): Don't snapshot markers outside of
current paragraph.
(yas--goto-saved-location, yas--snapshot-marker-location): Count
newlines as whitespace (even if mode marks them as non-whitespace
syntax, e.g., comment end).
(yas--snapshot-overlay-location, yas--restore-overlay-location): Don't
snapshot overlay position which is outside of BEG .. END.
This commit is contained in:
Noam Postavsky 2017-04-03 21:13:23 -04:00
parent 1babe81f7e
commit 394942130d
2 changed files with 30 additions and 12 deletions

View File

@ -91,6 +91,15 @@
(should (string= (yas--buffer-contents) (should (string= (yas--buffer-contents)
(concat filled-words "\n")))))) (concat filled-words "\n"))))))
(ert-deftest auto-fill-with-multiparagraph ()
"Test auto-fill protection on snippet spanning multiple paragraphs"
(with-temp-buffer
(yas-minor-mode +1)
(auto-fill-mode +1)
(yas-expand-snippet "foo$1\n\n$2bar")
(yas-mock-insert " ")
(ert-simulate-command '(yas-next-field-or-maybe-expand))
(should (looking-at "bar"))))
(ert-deftest primary-field-transformation () (ert-deftest primary-field-transformation ()
(with-temp-buffer (with-temp-buffer

View File

@ -3606,7 +3606,8 @@ field start. This hook does nothing if an undo is in progress."
(reoverlays nil)) (reoverlays nil))
(dolist (snippet snippets) (dolist (snippet snippets)
(dolist (m (yas--collect-snippet-markers snippet)) (dolist (m (yas--collect-snippet-markers snippet))
(push (yas--snapshot-marker-location m beg end) remarkers)) (when (and (<= beg m) (<= m end))
(push (yas--snapshot-marker-location m beg end) remarkers)))
(push (yas--snapshot-overlay-location (push (yas--snapshot-overlay-location
(yas--snippet-control-overlay snippet) beg end) (yas--snippet-control-overlay snippet) beg end)
reoverlays)) reoverlays))
@ -4129,15 +4130,21 @@ The returned value is a list of the form (MARKER REGEXP WS-COUNT)."
(nconc before (list marker) after) (nconc before (list marker) after)
"[[:space:]\n]*")) "[[:space:]\n]*"))
(progn (goto-char marker) (progn (goto-char marker)
(skip-syntax-forward " " end) (skip-chars-forward "[:space:]\n" end)
(- (point) marker))))) (- (point) marker)))))
(defun yas--snapshot-overlay-location (overlay beg end) (defun yas--snapshot-overlay-location (overlay beg end)
"Like `yas--snapshot-marker-location' for overlays. "Like `yas--snapshot-marker-location' for overlays.
The returned format is (OVERLAY (RE WS) (RE WS))." The returned format is (OVERLAY (RE WS) (RE WS)). Either of
(list overlay the (RE WS) lists may be nil if the start or end, respectively,
(cdr (yas--snapshot-marker-location (overlay-start overlay) beg end)) of the overlay is outside the range BEG .. END."
(cdr (yas--snapshot-marker-location (overlay-end overlay) beg end)))) (let ((obeg (overlay-start overlay))
(oend (overlay-end overlay)))
(list overlay
(when (and (<= beg obeg) (< obeg end))
(cdr (yas--snapshot-marker-location obeg beg end)))
(when (and (<= beg oend) (< oend end))
(cdr (yas--snapshot-marker-location oend beg end))))))
(defun yas--snapshot-overlay-line-location (overlay) (defun yas--snapshot-overlay-line-location (overlay)
"Return info for restoring OVERLAY's line based location. "Return info for restoring OVERLAY's line based location.
@ -4160,8 +4167,8 @@ Buffer must be narrowed to BEG..END used to create the snapshot info."
(lwarn '(yasnippet re-marker) :warning (lwarn '(yasnippet re-marker) :warning
"Couldn't find: %S" regexp) "Couldn't find: %S" regexp)
(goto-char (match-beginning 1)) (goto-char (match-beginning 1))
(skip-syntax-forward " ") (skip-chars-forward "[:space:]\n")
(skip-syntax-backward " " (- (point) ws-count)))) (skip-chars-backward "[:space:]\n" (- (point) ws-count))))
(defun yas--restore-marker-location (re-marker) (defun yas--restore-marker-location (re-marker)
"Restores marker based on info from `yas--snapshot-marker-location'. "Restores marker based on info from `yas--snapshot-marker-location'.
@ -4174,10 +4181,12 @@ Buffer must be narrowed to BEG..END used to create the snapshot info."
Buffer must be narrowed to BEG..END used to create the snapshot info." Buffer must be narrowed to BEG..END used to create the snapshot info."
(cl-destructuring-bind (overlay loc-beg loc-end) ov-locations (cl-destructuring-bind (overlay loc-beg loc-end) ov-locations
(move-overlay overlay (move-overlay overlay
(progn (apply #'yas--goto-saved-location loc-beg) (if (not loc-beg) (overlay-start overlay)
(point)) (apply #'yas--goto-saved-location loc-beg)
(progn (apply #'yas--goto-saved-location loc-end) (point))
(point))))) (if (not loc-end) (overlay-end overlay)
(apply #'yas--goto-saved-location loc-end)
(point)))))
(defun yas--restore-overlay-line-location (ov-locations) (defun yas--restore-overlay-line-location (ov-locations)