From 197104b450dea3972418a52f56e643a9fd0dd531 Mon Sep 17 00:00:00 2001 From: Joao Tavora Date: Fri, 3 Aug 2012 11:03:11 +0100 Subject: [PATCH] Closes #271: 3rd try, protect escapes after collecting backquote elisp * Add more tests * Now only need to escape backslashes in mirror/field transformations --- yasnippet-tests.el | 32 +++++++++++++++++++++++++++----- yasnippet.el | 8 ++++---- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/yasnippet-tests.el b/yasnippet-tests.el index 394d572..2870afb 100644 --- a/yasnippet-tests.el +++ b/yasnippet-tests.el @@ -97,7 +97,8 @@ ;; "brother from another mother!")))) -;;; Snippet expansion +;;; Snippet expansion and character escaping +;;; Thanks to @zw963 (Billy) for the testing ;;; (ert-deftest escape-dollar () (with-temp-buffer @@ -200,15 +201,36 @@ (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"))))) ;;; 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 diff --git a/yasnippet.el b/yasnippet.el index 5876c91..605b61a 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -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))