From 7f02dedfcae6ba0e3a646c1367c908af9b3dbe1d Mon Sep 17 00:00:00 2001 From: Sriram Thaiyar Date: Wed, 22 Jun 2016 09:21:26 -0700 Subject: [PATCH] Fix quote escaping in ruby-toggle-string-quotes * lisp/progmodes/ruby-mode.el (ruby-toggle-string-quotes): Change logic to quote based on the current quote of the string. * test/lisp/progmodes/ruby-mode-tests.el (ruby-toggle-string-quotes-quotes-correctly): Add test. Copyright-paperwork-exempt: yes --- lisp/progmodes/ruby-mode.el | 2 +- test/lisp/progmodes/ruby-mode-tests.el | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index d75edbc84e..aa7c08c542 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -1799,7 +1799,7 @@ If the result is do-end block, it will always be multiline." (content (buffer-substring-no-properties (1+ min) (1- max)))) (setq content - (if (equal string-quote "\"") + (if (equal string-quote "'") (replace-regexp-in-string "\\\\\"" "\"" (replace-regexp-in-string "\\([^\\\\]\\)'" "\\1\\\\'" content)) (replace-regexp-in-string "\\\\'" "'" (replace-regexp-in-string "\\([^\\\\]\\)\"" "\\1\\\\\"" content)))) (let ((orig-point (point))) diff --git a/test/lisp/progmodes/ruby-mode-tests.el b/test/lisp/progmodes/ruby-mode-tests.el index 52126a3bdf..5dd34f8ee4 100644 --- a/test/lisp/progmodes/ruby-mode-tests.el +++ b/test/lisp/progmodes/ruby-mode-tests.el @@ -716,6 +716,17 @@ VALUES-PLIST is a list with alternating index and value elements." (ruby-backward-sexp) (should (= 2 (line-number-at-pos))))) +(ert-deftest ruby-toggle-string-quotes-quotes-correctly () + (let ((pairs + '(("puts 'foo\"\\''" . "puts \"foo\\\"'\"") + ("puts \"foo'\\\"\"" . "puts 'foo\\'\"'")))) + (dolist (pair pairs) + (ruby-with-temp-buffer (car pair) + (beginning-of-line) + (search-forward "foo") + (ruby-toggle-string-quotes) + (should (string= (buffer-string) (cdr pair))))))) + (ert-deftest ruby--insert-coding-comment-ruby-style () (with-temp-buffer (let ((ruby-encoding-magic-comment-style 'ruby)) -- 2.39.2