From 9a0842dffe0013f4cca4853278ac3eaf94c4d3fc Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Fri, 21 Jan 2022 04:59:39 +0200 Subject: [PATCH] ruby-toggle-block-space-before-parameters: New user option * lisp/progmodes/ruby-mode.el (ruby-toggle-block-space-before-parameters): New user option (bug#53321). (ruby-do-end-to-brace): Handle it. * test/lisp/progmodes/ruby-mode-tests.el (ruby-toggle-block-to-brace-no-space): New test. --- etc/NEWS | 4 ++++ lisp/progmodes/ruby-mode.el | 22 +++++++++++++++------- test/lisp/progmodes/ruby-mode-tests.el | 7 +++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index ae4bf7e4d3f..c73e34c6725 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -842,6 +842,10 @@ the Netscape web browser was released in February, 2008. This support has been obsolete since Emacs 25.1. The final version of the Galeon web browser was released in September, 2008. +** Ruby Mode + +*** New user option 'ruby-toggle-block-space-before-parameters'. + ** Miscellaneous --- diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 72631a6557f..eb54ffe05a8 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -325,6 +325,13 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." "Use `ruby-encoding-map' to set encoding magic comment if this is non-nil." :type 'boolean :group 'ruby) +(defcustom ruby-toggle-block-space-before-parameters t + "When non-nil, ensure space between the \"toggled\" curly and parameters. +This only affects the output of the command `ruby-toggle-block'." + :type 'boolean + :safe 'booleanp + :version "29.1") + ;;; SMIE support (require 'smie) @@ -1722,13 +1729,14 @@ See `add-log-current-defun-function'." (insert "}") (goto-char orig) (delete-char 2) - ;; Maybe this should be customizable, let's see if anyone asks. - (insert "{ ") - (setq beg-marker (point-marker)) - (when (looking-at "\\s +|") - (delete-char (- (match-end 0) (match-beginning 0) 1)) - (forward-char) - (re-search-forward "|" (line-end-position) t)) + (insert "{") + (if (looking-at "\\s +|") + (progn + (just-one-space (if ruby-toggle-block-space-before-parameters 1 0)) + (setq beg-marker (point-marker)) + (forward-char) + (re-search-forward "|" (line-end-position) t)) + (setq beg-marker (point-marker))) (save-excursion (skip-chars-forward " \t\n\r") (setq beg-pos (point)) diff --git a/test/lisp/progmodes/ruby-mode-tests.el b/test/lisp/progmodes/ruby-mode-tests.el index 23e13b94e6b..33fded5a59b 100644 --- a/test/lisp/progmodes/ruby-mode-tests.el +++ b/test/lisp/progmodes/ruby-mode-tests.el @@ -407,6 +407,13 @@ VALUES-PLIST is a list with alternating index and value elements." (ruby-toggle-block) (should (string= "foo { \"#{bar}\" }" (buffer-string))))) +(ert-deftest ruby-toggle-block-to-brace-no-space () + (ruby-with-temp-buffer "foo do |b|\n b + 2\nend" + (beginning-of-line) + (let (ruby-toggle-block-space-before-parameters) + (ruby-toggle-block)) + (should (string= "foo {|b| b + 2 }" (buffer-string))))) + (ert-deftest ruby-recognize-symbols-starting-with-at-character () (ruby-assert-face ":@abc" 3 font-lock-constant-face)) -- 2.39.5