branch: master
commit 1bee3a33c77d1a61c331461750e01c4f6fa85417
Author: João Távora <[email protected]>
Commit: João Távora <[email protected]>
Fix #979: grok curly braces with LSP-style escaping
* yasnippet-tests.el (escaping-for-lsp-style-snippet-syntax): New
test.
* yasnippet.el (yas--field-parse-create): rework real-match-end-0
calculation.
---
yasnippet-tests.el | 14 ++++++++++++++
yasnippet.el | 9 ++++++++-
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/yasnippet-tests.el b/yasnippet-tests.el
index 6a306ee..f7ca2bb 100644
--- a/yasnippet-tests.el
+++ b/yasnippet-tests.el
@@ -781,6 +781,20 @@ mapconcat #'(lambda (arg)
(yas-expand-snippet "Look ma! ${1:`(yas-selected-text)`} OK?")
(should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
+(ert-deftest escaping-for-lsp-style-snippet-syntax ()
+ "See Github #979."
+ (should
+ (string= (with-temp-buffer
+ (yas-minor-mode 1)
+ (yas-expand-snippet
+ "Printf(${1:format string}, ${2:args ...interface{\\}})${0}")
+ (yas--buffer-contents))
+ (with-temp-buffer
+ (yas-minor-mode 1)
+ (yas-expand-snippet
+ "Printf(${1:format string}, ${2:args ...interface\\{\\}})${0}")
+ (yas--buffer-contents)))))
+
(ert-deftest insert-snippet-with-backslashes-in-active-field ()
;; This test case fails if `yas--inhibit-overlay-hooks' is not bound
;; in `yas-expand-snippet' (see Github #844).
diff --git a/yasnippet.el b/yasnippet.el
index 02886ca..d980bf1 100644
--- a/yasnippet.el
+++ b/yasnippet.el
@@ -4730,7 +4730,14 @@ When multiple expressions are found, only the last one
counts."
;;
(save-excursion
(while (re-search-forward yas--field-regexp nil t)
- (let* ((real-match-end-0 (yas--scan-sexps (1+ (match-beginning 0)) 1))
+ (let* ((brace-scan (yas--scan-sexps (1+ (match-beginning 0)) 1))
+ ;; if the `brace-scan' didn't reach a brace, we have a
+ ;; snippet with invalid escaping, probably a closing
+ ;; brace escaped with two backslashes (github#979). But
+ ;; be lenient, because we can.
+ (real-match-end-0 (if (eq ?} (char-before brace-scan))
+ brace-scan
+ (point)))
(number (and (match-string-no-properties 1)
(string-to-number (match-string-no-properties 1))))
(brand-new-field (and real-match-end-0