]> code.delx.au - gnu-emacs-elpa/commitdiff
Closes #271: 3rd try, protect escapes after collecting backquote elisp
authorJoao Tavora <joaotavora@gmail.com>
Fri, 3 Aug 2012 10:03:11 +0000 (11:03 +0100)
committerJoao Tavora <joaotavora@gmail.com>
Fri, 3 Aug 2012 10:03:11 +0000 (11:03 +0100)
* Add more tests
* Now only need to escape backslashes in mirror/field
  transformations

yasnippet-tests.el
yasnippet.el

index 394d5721df36e147b6d110af1f960b9505345ecc..2870afb38669e2b5206e80ebbce5d701d41d823f 100644 (file)
@@ -97,7 +97,8 @@
 ;;                      "brother from another mother!"))))
 
 \f
-;;; Snippet expansion
+;;; Snippet expansion and character escaping
+;;; Thanks to @zw963 (Billy) for the testing
 ;;;
 (ert-deftest escape-dollar ()
   (with-temp-buffer
       (set-mark 4)
       (goto-char 7)
       (yas-expand-snippet snippet)
-      (ert-simulate-command `(yas-mock-insert "bbb"))
-      (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
+      (should (string= (yas--buffer-contents) "aaa${1:bbb}ccc")))))
+
+(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")))))
 
 \f
 ;;; Misc tests
 ;;;
-
 (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!"
   :expected-result :failed
index 5876c918675d9ef3dd196254275aa30c87779ed9..605b61a6df1d41af4f963519c0b60e62bc70ca95 100644 (file)
@@ -3762,13 +3762,13 @@ Meant to be called in a narrowed buffer, does various passes"
     ;; Reset the yas--dollar-regions
     ;;
     (setq yas--dollar-regions nil)
-    ;; protect escaped characters
-    ;;
-    (yas--protect-escapes)
     ;; replace all backquoted expressions
     ;;
     (goto-char parse-start)
     (yas--save-backquotes)
+    ;; protect escaped characters
+    ;;
+    (yas--protect-escapes)
     ;; parse fields with {}
     ;;
     (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
 with their evaluated value into `yas--backquote-markers-and-strings'"
   (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))
       (setq transformed (yas--eval-lisp (yas--read-lisp current-string)))
       (goto-char (match-beginning 0))