mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 13:13:03 +00:00
Don't use global variable for backquote expression locations
* yasnippet.el (yas--backquote-markers-and-strings): Remove. (yas--save-backquotes): Return list of backquote expressions. (yas--restore-backquotes): Receive it as a parameter. (yas--snippet-parse-create): Keep it in a local variable. * yasnippet-tests.el (yas-no-memory-of-bad-snippet): New test.
This commit is contained in:
parent
7c9edb5b34
commit
caf3dba320
@ -683,6 +683,17 @@ mapconcat #'(lambda (arg)
|
|||||||
(yas-mock-insert "bbb")
|
(yas-mock-insert "bbb")
|
||||||
(should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
|
(should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
|
||||||
|
|
||||||
|
(ert-deftest yas-no-memory-of-bad-snippet ()
|
||||||
|
"Check that expanding an incorrect has no influence on future expansions."
|
||||||
|
;; See https://github.com/joaotavora/yasnippet/issues/800.
|
||||||
|
(with-temp-buffer
|
||||||
|
(yas-minor-mode 1)
|
||||||
|
(should-error (yas-expand-snippet "```foo\n\n```"))
|
||||||
|
(erase-buffer) ; Bad snippet may leave wrong text.
|
||||||
|
;; But expanding the corrected snippet should work fine.
|
||||||
|
(yas-expand-snippet "\\`\\`\\`foo\n\n\\`\\`\\`")
|
||||||
|
(should (equal (buffer-string) "```foo\n\n```"))))
|
||||||
|
|
||||||
(defmacro yas--with-font-locked-temp-buffer (&rest body)
|
(defmacro yas--with-font-locked-temp-buffer (&rest body)
|
||||||
"Like `with-temp-buffer', but ensure `font-lock-mode'."
|
"Like `with-temp-buffer', but ensure `font-lock-mode'."
|
||||||
(declare (indent 0) (debug t))
|
(declare (indent 0) (debug t))
|
||||||
|
53
yasnippet.el
53
yasnippet.el
@ -4087,11 +4087,6 @@ next FOM. Works its way up recursively for parents of parents."
|
|||||||
"When expanding the snippet the \"parse-create\" functions add
|
"When expanding the snippet the \"parse-create\" functions add
|
||||||
cons cells to this var.")
|
cons cells to this var.")
|
||||||
|
|
||||||
(defvar yas--backquote-markers-and-strings nil
|
|
||||||
"List of (MARKER . STRING) marking where the values from
|
|
||||||
backquoted Lisp expressions should be inserted at the end of
|
|
||||||
expansion.")
|
|
||||||
|
|
||||||
(defvar yas--indent-markers nil
|
(defvar yas--indent-markers nil
|
||||||
"List of markers for manual indentation.")
|
"List of markers for manual indentation.")
|
||||||
|
|
||||||
@ -4100,15 +4095,16 @@ expansion.")
|
|||||||
necessary fields, mirrors and exit points.
|
necessary fields, mirrors and exit points.
|
||||||
|
|
||||||
Meant to be called in a narrowed buffer, does various passes"
|
Meant to be called in a narrowed buffer, does various passes"
|
||||||
(let ((parse-start (point)))
|
(let ((saved-quotes nil)
|
||||||
|
(parse-start (point)))
|
||||||
;; Avoid major-mode's syntax propertizing function, since we
|
;; Avoid major-mode's syntax propertizing function, since we
|
||||||
;; change the syntax-table while calling `scan-sexps'.
|
;; change the syntax-table while calling `scan-sexps'.
|
||||||
(let ((syntax-propertize-function nil))
|
(let ((syntax-propertize-function nil))
|
||||||
(setq yas--dollar-regions nil) ; Reset the yas--dollar-regions.
|
(setq yas--dollar-regions nil) ; Reset the yas--dollar-regions.
|
||||||
(yas--protect-escapes nil '(?`)) ; Protect just the backquotes.
|
(yas--protect-escapes nil '(?`)) ; Protect just the backquotes.
|
||||||
(goto-char parse-start)
|
(goto-char parse-start)
|
||||||
(yas--save-backquotes) ; Replace all backquoted expressions.
|
(setq saved-quotes (yas--save-backquotes)) ; `expressions`.
|
||||||
(yas--protect-escapes) ; Protect escaped characters.
|
(yas--protect-escapes) ; Protect escaped characters.
|
||||||
(goto-char parse-start)
|
(goto-char parse-start)
|
||||||
(yas--indent-parse-create) ; Parse indent markers: `$>'.
|
(yas--indent-parse-create) ; Parse indent markers: `$>'.
|
||||||
(goto-char parse-start)
|
(goto-char parse-start)
|
||||||
@ -4138,7 +4134,7 @@ Meant to be called in a narrowed buffer, does various passes"
|
|||||||
(get-register yas-wrap-around-region))
|
(get-register yas-wrap-around-region))
|
||||||
(insert (prog1 (get-register yas-wrap-around-region)
|
(insert (prog1 (get-register yas-wrap-around-region)
|
||||||
(set-register yas-wrap-around-region nil)))))
|
(set-register yas-wrap-around-region nil)))))
|
||||||
(yas--restore-backquotes) ; Restore backquoted expression values.
|
(yas--restore-backquotes saved-quotes) ; Restore `expression` values.
|
||||||
(goto-char parse-start)
|
(goto-char parse-start)
|
||||||
(yas--restore-escapes) ; Restore escapes.
|
(yas--restore-escapes) ; Restore escapes.
|
||||||
(yas--update-mirrors snippet) ; Update mirrors for the first time.
|
(yas--update-mirrors snippet) ; Update mirrors for the first time.
|
||||||
@ -4353,9 +4349,11 @@ With optional string TEXT do it in string instead of the buffer."
|
|||||||
changed-text))
|
changed-text))
|
||||||
|
|
||||||
(defun yas--save-backquotes ()
|
(defun yas--save-backquotes ()
|
||||||
"Save all the \"\\=`(lisp-expression)\\=`\"-style expressions
|
"Save all \"\\=`(lisp-expression)\\=`\"-style expressions.
|
||||||
with their evaluated value into `yas--backquote-markers-and-strings'."
|
Return a list of (MARKER . STRING) entires for each backquoted
|
||||||
(let* ((yas--snippet-buffer (current-buffer))
|
Lisp expression."
|
||||||
|
(let* ((saved-quotes nil)
|
||||||
|
(yas--snippet-buffer (current-buffer))
|
||||||
(yas--change-detected nil)
|
(yas--change-detected nil)
|
||||||
(detect-change (lambda (_beg _end)
|
(detect-change (lambda (_beg _end)
|
||||||
(when (eq (current-buffer) yas--snippet-buffer)
|
(when (eq (current-buffer) yas--snippet-buffer)
|
||||||
@ -4378,29 +4376,28 @@ with their evaluated value into `yas--backquote-markers-and-strings'."
|
|||||||
(insert "Y") ;; quite horrendous, I love it :)
|
(insert "Y") ;; quite horrendous, I love it :)
|
||||||
(set-marker marker (point))
|
(set-marker marker (point))
|
||||||
(insert "Y"))
|
(insert "Y"))
|
||||||
(push (cons marker transformed) yas--backquote-markers-and-strings)))))
|
(push (cons marker transformed) saved-quotes)))))
|
||||||
(when yas--change-detected
|
(when yas--change-detected
|
||||||
(lwarn '(yasnippet backquote-change) :warning
|
(lwarn '(yasnippet backquote-change) :warning
|
||||||
"`%s' modified buffer in a backquote expression.
|
"`%s' modified buffer in a backquote expression.
|
||||||
To hide this warning, add (yasnippet backquote-change) to `warning-suppress-types'."
|
To hide this warning, add (yasnippet backquote-change) to `warning-suppress-types'."
|
||||||
(if yas--current-template
|
(if yas--current-template
|
||||||
(yas--template-name yas--current-template)
|
(yas--template-name yas--current-template)
|
||||||
"Snippet")))))
|
"Snippet")))
|
||||||
|
saved-quotes))
|
||||||
|
|
||||||
(defun yas--restore-backquotes ()
|
(defun yas--restore-backquotes (saved-quotes)
|
||||||
"Replace markers in `yas--backquote-markers-and-strings' with their values."
|
"Replace markers in SAVED-QUOTES with their values.
|
||||||
(while yas--backquote-markers-and-strings
|
SAVED-QUOTES is the in format returned by `yas--save-backquotes'."
|
||||||
(let* ((marker-and-string (pop yas--backquote-markers-and-strings))
|
(cl-loop for (marker . string) in saved-quotes do
|
||||||
(marker (car marker-and-string))
|
(save-excursion
|
||||||
(string (cdr marker-and-string)))
|
(goto-char marker)
|
||||||
(save-excursion
|
(save-restriction
|
||||||
(goto-char marker)
|
(widen)
|
||||||
(save-restriction
|
(delete-char -1)
|
||||||
(widen)
|
(insert string)
|
||||||
(delete-char -1)
|
(delete-char 1))
|
||||||
(insert string)
|
(set-marker marker nil))))
|
||||||
(delete-char 1))
|
|
||||||
(set-marker marker nil)))))
|
|
||||||
|
|
||||||
(defun yas--scan-sexps (from count)
|
(defun yas--scan-sexps (from count)
|
||||||
(ignore-errors
|
(ignore-errors
|
||||||
|
Loading…
x
Reference in New Issue
Block a user