Closes #271: 3rd try, protect escapes after collecting backquote elisp

* Add more tests
* Now only need to escape backslashes in mirror/field
  transformations
This commit is contained in:
Joao Tavora 2012-08-03 11:03:11 +01:00
parent 6aff6a0894
commit 197104b450
2 changed files with 31 additions and 9 deletions

View File

@ -97,7 +97,8 @@
;; "brother from another mother!")))) ;; "brother from another mother!"))))
;;; Snippet expansion ;;; Snippet expansion and character escaping
;;; Thanks to @zw963 (Billy) for the testing
;;; ;;;
(ert-deftest escape-dollar () (ert-deftest escape-dollar ()
(with-temp-buffer (with-temp-buffer
@ -200,15 +201,36 @@
(set-mark 4) (set-mark 4)
(goto-char 7) (goto-char 7)
(yas-expand-snippet snippet) (yas-expand-snippet snippet)
(ert-simulate-command `(yas-mock-insert "bbb")) (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
(should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
(ert-deftest string-match-with-subregexp-in-embedded-elisp ()
(with-temp-buffer
(yas-minor-mode 1)
;; the rule here is: To use regexps in embedded `(elisp)` expressions, write
;; it like you would normal elisp, i.e. no need to escape the backslashes.
(let ((snippet "`(if (string-match \"foo\\\\(ba+r\\\\)foo\" \"foobaaaaaaaaaarfoo\") \"ok\" \"fail\")`"))
(yas-expand-snippet snippet))
(should (string= (yas--buffer-contents) "ok"))))
(ert-deftest string-match-with-subregexp-in-mirror-transformations ()
(with-temp-buffer
(yas-minor-mode 1)
;; the rule here is: To use regexps in embedded `(elisp)` expressions,
;; escape backslashes once. i.e. to use \\( \\) constructs, write \\\\( \\\\).
(let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text) \"ok\" \"fail\")}"))
(yas-expand-snippet snippet)
(should (string= (yas--buffer-contents) "fail"))
(ert-simulate-command `(yas-mock-insert "foobaaar"))
(should (string= (yas--buffer-contents) "foobaaarfail"))
(ert-simulate-command `(yas-mock-insert "baz"))
(should (string= (yas--buffer-contents) "foobaaarbazok")))))
;;; Misc tests ;;; Misc tests
;;; ;;;
(ert-deftest protection-overlay-no-cheating () (ert-deftest protection-overlay-no-cheating ()
"Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline! "Protection overlays at the very end of the buffer are dealt
with by cheatingly inserting a newline!
TODO: correct this bug!" TODO: correct this bug!"
:expected-result :failed :expected-result :failed

View File

@ -3762,13 +3762,13 @@ Meant to be called in a narrowed buffer, does various passes"
;; Reset the yas--dollar-regions ;; Reset the yas--dollar-regions
;; ;;
(setq yas--dollar-regions nil) (setq yas--dollar-regions nil)
;; protect escaped characters
;;
(yas--protect-escapes)
;; replace all backquoted expressions ;; replace all backquoted expressions
;; ;;
(goto-char parse-start) (goto-char parse-start)
(yas--save-backquotes) (yas--save-backquotes)
;; protect escaped characters
;;
(yas--protect-escapes)
;; parse fields with {} ;; parse fields with {}
;; ;;
(goto-char parse-start) (goto-char parse-start)
@ -3925,7 +3925,7 @@ With optional string TEXT do it in string instead of the buffer."
"Save all the \"`(lisp-expression)`\"-style expression "Save all the \"`(lisp-expression)`\"-style expression
with their evaluated value into `yas--backquote-markers-and-strings'" with their evaluated value into `yas--backquote-markers-and-strings'"
(while (re-search-forward yas--backquote-lisp-expression-regexp nil t) (while (re-search-forward yas--backquote-lisp-expression-regexp nil t)
(let ((current-string (match-string 1)) transformed) (let ((current-string (match-string-no-properties 1)) transformed)
(delete-region (match-beginning 0) (match-end 0)) (delete-region (match-beginning 0) (match-end 0))
(setq transformed (yas--eval-lisp (yas--read-lisp current-string))) (setq transformed (yas--eval-lisp (yas--read-lisp current-string)))
(goto-char (match-beginning 0)) (goto-char (match-beginning 0))