Skip to content

Commit

Permalink
Closes #271: 2nd try, remove unescaping of backquote replacements
Browse files Browse the repository at this point in the history
* Fixes previous fix (big blunder, expansion unusable)
* Added more tests
* Fixed previously broken tests.
  • Loading branch information
joaotavora committed Aug 2, 2012
1 parent 12c21c6 commit 6aff6a0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
26 changes: 22 additions & 4 deletions yasnippet-tests.el
Expand Up @@ -153,7 +153,7 @@
(yas-expand-snippet "Look ma! `(yas/selected-text)`")
(should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!")))
(yas-exit-all-snippets)
(erase-buffer))))
(erase-buffer)))

(ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
(with-temp-buffer
Expand All @@ -176,15 +176,33 @@
(ert-deftest primary-field-transformation ()
(with-temp-buffer
(yas-minor-mode 1)
;; The rules here is: to output a literal `"' you need to escape
;; it with one backslash. You don't need to escape them in
;; embedded elisp.
(let ((snippet "${1:$$(upcase yas/text)}${1:$(concat \"bar\" yas/text)}"))
(yas-expand-snippet snippet)
(should (string= (yas--buffer-contents) "bar"))
(ert-simulate-command `(yas-mock-insert "foo"))
(should (string= (yas--buffer-contents) "FOObarFOO")))))

(ert-deftest example-for-issue-271 ()
(with-temp-buffer
(yas-minor-mode 1)
(let ((yas-selected-text "aaa")
(snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend"))
(yas-expand-snippet snippet)
(yas-next-field)
(ert-simulate-command `(yas-mock-insert "bbb"))
(should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))

(ert-deftest another-example-for-issue-271 ()
(with-temp-buffer
(yas-minor-mode 1)
(let ((snippet "\\${${1:1}:`yas/selected-text`}"))
(insert "aaabbbccc")
(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")))))


;;; Misc tests
;;;
Expand Down
38 changes: 19 additions & 19 deletions yasnippet.el
Expand Up @@ -3451,22 +3451,22 @@ considered when expanding the snippet."
(run-hooks 'yas-before-expand-snippet-hook)

;;
(let ((yas-selected-text (or yas-selected-text
(and (region-active-p)
(buffer-substring-no-properties (region-beginning)
(region-end)))))
(to-delete (and start
end
(buffer-substring-no-properties start end)))
(start (or start
(and (region-active-p)
(region-beginning))
(point)))
(end (or end
(and (region-active-p)
(region-end))
(point)))
snippet)
(let* ((yas-selected-text (or yas-selected-text
(and (region-active-p)
(buffer-substring-no-properties (region-beginning)
(region-end)))))
(start (or start
(and (region-active-p)
(region-beginning))
(point)))
(end (or end
(and (region-active-p)
(region-end))
(point)))
(to-delete (and start
end
(buffer-substring-no-properties start end)))
snippet)
(goto-char start)
(setq yas--indent-original-column (current-column))
;; Delete the region to delete, this *does* get undo-recorded.
Expand Down Expand Up @@ -3762,9 +3762,9 @@ Meant to be called in a narrowed buffer, does various passes"
;; Reset the yas--dollar-regions
;;
(setq yas--dollar-regions nil)
;; protect escaped quote, backquotes and backslashes
;; protect escaped characters
;;
(yas--protect-escapes nil `(?\\ ?` ?'))
(yas--protect-escapes)
;; replace all backquoted expressions
;;
(goto-char parse-start)
Expand Down Expand Up @@ -3927,7 +3927,7 @@ 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)
(delete-region (match-beginning 0) (match-end 0))
(setq transformed (yas--eval-lisp (yas--read-lisp (yas--restore-escapes current-string))))
(setq transformed (yas--eval-lisp (yas--read-lisp current-string)))
(goto-char (match-beginning 0))
(when transformed
(let ((marker (make-marker)))
Expand Down

0 comments on commit 6aff6a0

Please sign in to comment.