From: Dmitry Gutov <dgutov@yandex.ru>
To: Aaron Jensen <aaronjensen@gmail.com>
Cc: 60186@debbugs.gnu.org
Subject: bug#60186: 29.0.60; ruby-mode indentation of multi-line expressions
Date: Wed, 28 Dec 2022 23:24:06 +0200 [thread overview]
Message-ID: <118143a7-0703-2538-9853-7009064e7bca@yandex.ru> (raw)
In-Reply-To: <44ccf2d5-789f-f122-34f9-25871fdf1283@yandex.ru>
[-- Attachment #1: Type: text/plain, Size: 568 bytes --]
On 28/12/2022 14:47, Dmitry Gutov wrote:
> I guess that particular trend started with ruby-method-params-indent,
> where I haven't managed to choose better names for the var, or the values.
Semantics aside (I suppose we could go back and revise the naming a
little later), could you test this new revision of the patch?
I think I got the implementation simple enough now.
The number of options has grown, though:
(setq ruby-after-operator-indent nil
ruby-block-indent nil
ruby-method-call-indent nil
ruby-parenless-call-arguments-indent nil)
[-- Attachment #2: ruby-simplified-indent-v6.diff --]
[-- Type: text/x-patch, Size: 10761 bytes --]
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index a4aa61905e4..021dda1a3e1 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -285,6 +285,92 @@ ruby-method-params-indent
:safe (lambda (val) (or (memq val '(t nil)) (numberp val)))
:version "29.1")
+(defcustom ruby-block-indent t
+ "Non-nil to align the body of a block to the statement's start.
+
+The body and the closer will be aligned to the column where the
+statement containing the block starts. Example:
+
+ foo.bar
+ .each do
+ baz
+ end
+
+If nil, it will be aligned instead to the beginning of the line
+containing the block's opener:
+
+ foo.bar
+ .each do
+ baz
+ end
+
+Only has effect when `ruby-use-smie' is t."
+ :type 'boolean
+ :safe 'booleanp)
+
+(defcustom ruby-after-operator-indent t
+ "Non-nil to use structural indentation after binary operators.
+
+The code will be aligned to the implicit parent expression,
+according to the operator precedence:
+
+ qux = 4 + 5 *
+ 6 +
+ 7
+
+Set it to nil to align to the beginning of the statement:
+
+ qux = 4 + 5 *
+ 6 +
+ 7
+
+Only has effect when `ruby-use-smie' is t."
+ :type 'boolean
+ :safe 'booleanp)
+
+(defcustom ruby-method-call-indent t
+ "Non-nil to use the structural indentation algorithm.
+
+The method call will be aligned to the implicit parent
+expression, according to the operator precedence:
+
+ foo = subject
+ .update(
+ 1
+ )
+
+Set it to nil to align to the beginning of the statement:
+
+ foo = subject
+ .update(
+ 1
+ )
+
+Only has effect when `ruby-use-smie' is t."
+ :type 'boolean
+ :safe 'booleanp)
+
+(defcustom ruby-parenless-call-arguments-indent t
+ "Non-nil to align arguments in a parenless call vertically.
+
+Example:
+
+ qux :+,
+ bar,
+ :[]=,
+ bar
+
+Set it to nil to align to the beginning of the statement:
+
+ qux :+,
+ bar,
+ :[]=,
+ bar
+
+Only has effect when `ruby-use-smie' is t."
+ :type 'boolean
+ :safe 'booleanp)
+
(defcustom ruby-deep-arglist t
"Deep indent lists in parenthesis when non-nil.
Also ignores spaces after parenthesis when `space'.
@@ -416,6 +502,7 @@ ruby-smie-grammar
'((right "=")
(right "+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^="
"<<=" ">>=" "&&=" "||=")
+ (right "?")
(nonassoc ".." "...")
(left "&&" "||")
(nonassoc "<=>")
@@ -608,10 +695,10 @@ ruby-smie--backward-token
"def=")
(t tok)))))))
-(defun ruby-smie--indent-to-stmt ()
+(defun ruby-smie--indent-to-stmt (&optional offset)
(save-excursion
(smie-backward-sexp ";")
- (cons 'column (smie-indent-virtual))))
+ (cons 'column (+ (smie-indent-virtual) (or offset 0)))))
(defun ruby-smie--indent-to-stmt-p (keyword)
(or (eq t ruby-align-to-stmt-keywords)
@@ -642,7 +729,9 @@ ruby-smie-rules
(forward-comment -1)
(not (eq (preceding-char) ?:))))
;; Curly block opener.
- (ruby-smie--indent-to-stmt))
+ (if ruby-block-indent
+ (ruby-smie--indent-to-stmt)
+ (cons 'column (current-indentation))))
((smie-rule-hanging-p)
;; Treat purely syntactic block-constructs as being part of their parent,
;; when the opening token is hanging and the parent is not an
@@ -677,13 +766,20 @@ ruby-smie-rules
(unless (or (eolp) (forward-comment 1))
(cons 'column (current-column)))))
('(:before . " @ ")
- (if (or (eq ruby-method-params-indent t)
- (not (smie-rule-parent-p "def" "def=")))
- (save-excursion
- (skip-chars-forward " \t")
- (cons 'column (current-column)))
- (smie-rule-parent (or ruby-method-params-indent 0))))
- ('(:before . "do") (ruby-smie--indent-to-stmt))
+ (cond
+ ((and (not ruby-parenless-call-arguments-indent)
+ (not (smie-rule-parent-p "def" "def=")))
+ (ruby-smie--indent-to-stmt ruby-indent-level))
+ ((or (eq ruby-method-params-indent t)
+ (not (smie-rule-parent-p "def" "def=")))
+ (save-excursion
+ (skip-chars-forward " \t")
+ (cons 'column (current-column))))
+ (t (smie-rule-parent (or ruby-method-params-indent 0)))))
+ ('(:before . "do")
+ (if ruby-block-indent
+ (ruby-smie--indent-to-stmt)
+ (cons 'column (current-indentation))))
('(:before . ".")
(if (smie-rule-sibling-p)
(when ruby-align-chained-calls
@@ -696,8 +792,10 @@ ruby-smie-rules
(not (smie-rule-bolp)))))
(cons 'column (current-column)))
(smie-backward-sexp ".")
- (cons 'column (+ (current-column)
- ruby-indent-level))))
+ (if ruby-method-call-indent
+ (cons 'column (+ (current-column)
+ ruby-indent-level))
+ (ruby-smie--indent-to-stmt ruby-indent-level))))
(`(:before . ,(or "else" "then" "elsif" "rescue" "ensure"))
(smie-rule-parent))
(`(:before . ,(or "when" "in"))
@@ -708,16 +806,22 @@ ruby-smie-rules
"<=>" ">" "<" ">=" "<=" "==" "===" "!=" "<<" ">>"
"+=" "-=" "*=" "/=" "%=" "**=" "&=" "|=" "^=" "|"
"<<=" ">>=" "&&=" "||=" "and" "or"))
- (and (smie-rule-parent-p ";" nil)
- (smie-indent--hanging-p)
- ruby-indent-level))
+ (cond
+ ((not ruby-after-operator-indent)
+ (ruby-smie--indent-to-stmt ruby-indent-level))
+ ((and (smie-rule-parent-p ";" nil)
+ (smie-indent--hanging-p))
+ ruby-indent-level)))
(`(:before . "=")
(save-excursion
(and (smie-rule-parent-p " @ ")
(goto-char (nth 1 (smie-indent--parent)))
(smie-rule-prev-p "def=")
(cons 'column (+ (current-column) ruby-indent-level -3)))))
- (`(:after . ,(or "?" ":")) ruby-indent-level)
+ (`(:after . ,(or "?" ":"))
+ (if ruby-after-operator-indent
+ ruby-indent-level
+ (ruby-smie--indent-to-stmt ruby-indent-level)))
(`(:before . ,(guard (memq (intern-soft token) ruby-alignable-keywords)))
(when (not (ruby--at-indentation-p))
(if (ruby-smie--indent-to-stmt-p token)
@@ -725,7 +829,10 @@ ruby-smie-rules
(cons 'column (current-column)))))
('(:before . "iuwu-mod")
(smie-rule-parent ruby-indent-level))
- ))
+ (`(:before . ",")
+ (and (not ruby-parenless-call-arguments-indent)
+ (smie-rule-parent-p " @ ")
+ (ruby-smie--indent-to-stmt ruby-indent-level)))))
(defun ruby--at-indentation-p (&optional point)
(save-excursion
diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby-after-operator-indent.rb b/test/lisp/progmodes/ruby-mode-resources/ruby-after-operator-indent.rb
new file mode 100644
index 00000000000..25cd8736f97
--- /dev/null
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby-after-operator-indent.rb
@@ -0,0 +1,29 @@
+4 +
+ 5 +
+ 6 +
+ 7
+
+qux = 4 + 5 *
+ 6 +
+ 7
+
+foo = obj.bar { |m| tee(m) } +
+ obj.qux { |m| hum(m) }
+
+foo.
+ bar
+ .baz
+
+qux = foo.fee ?
+ bar :
+ tee
+
+# Endless methods.
+class Bar
+ def foo(abc) = bar +
+ baz
+end
+
+# Local Variables:
+# ruby-after-operator-indent: nil
+# End:
diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby-block-indent.rb b/test/lisp/progmodes/ruby-mode-resources/ruby-block-indent.rb
new file mode 100644
index 00000000000..32882814b7e
--- /dev/null
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby-block-indent.rb
@@ -0,0 +1,33 @@
+foo
+ .asdasd
+ .proc do |**args|
+ p(**args)
+ end
+
+foo
+ .asdasd
+ .proc { |**args|
+ p(**args)
+ }
+
+bar.foo do
+ bar
+end
+
+bar.foo(tee) do
+ bar
+end
+
+bar.foo(tee) {
+ bar
+}
+
+x.foo do
+ foo
+end.bar do
+ bar
+end
+
+# Local Variables:
+# ruby-block-indent: nil
+# End:
diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby-method-call-indent.rb b/test/lisp/progmodes/ruby-mode-resources/ruby-method-call-indent.rb
new file mode 100644
index 00000000000..1a8285ee919
--- /dev/null
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby-method-call-indent.rb
@@ -0,0 +1,15 @@
+foo2 =
+ subject.
+ update(
+ 2
+ )
+
+foo3 =
+ subject
+ .update(
+ 2
+ )
+
+# Local Variables:
+# ruby-method-call-indent: nil
+# End:
diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby-parenless-call-arguments-indent.rb b/test/lisp/progmodes/ruby-mode-resources/ruby-parenless-call-arguments-indent.rb
new file mode 100644
index 00000000000..58e08810c4c
--- /dev/null
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby-parenless-call-arguments-indent.rb
@@ -0,0 +1,23 @@
+method arg1,
+ method2 arg2,
+ arg3, [
+ arg4,
+ arg5
+ ]
+
+zzz = method (a + b),
+ c, :d => :e,
+ f: g
+
+return render json: {
+ errors: { base: [message] },
+ copying: copying
+ },
+ status: 400
+
+foo(a,
+ b)
+
+# Local Variables:
+# ruby-parenless-call-arguments-indent: nil
+# End:
diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby.rb b/test/lisp/progmodes/ruby-mode-resources/ruby.rb
index 6a69d9db78a..bfae948b259 100644
--- a/test/lisp/progmodes/ruby-mode-resources/ruby.rb
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby.rb
@@ -226,6 +226,7 @@ def begin
foo.
bar
+ .baz
# https://github.com/rails/rails/blob/17f5d8e062909f1fcae25351834d8e89967b645e/activesupport/lib/active_support/time_with_zone.rb#L206
foo # comment intended to confuse the tokenizer
@@ -380,6 +381,18 @@ def bar
i + 1
end
+m1 = foo
+ .asdasd
+ .proc do |**args|
+ p(**args)
+end
+
+m2 = foo
+ .asdasd
+ .proc { |**args|
+ p(**args)
+}
+
bar.foo do
bar
end
@@ -398,6 +411,12 @@ def bar
end
end
+x.foo do
+ foo
+end.bar do
+ bar
+end
+
foo |
bar
@@ -540,5 +559,9 @@ def baz.full_name = "#{bar} 3"
end
# Local Variables:
+# ruby-after-operator-indent: t
+# ruby-block-indent: t
+# ruby-method-call-indent: t
# ruby-method-params-indent: t
+# ruby-parenless-call-arguments-indent: t
# End:
diff --git a/test/lisp/progmodes/ruby-mode-tests.el b/test/lisp/progmodes/ruby-mode-tests.el
index 560f780285a..5c81cc31cc1 100644
--- a/test/lisp/progmodes/ruby-mode-tests.el
+++ b/test/lisp/progmodes/ruby-mode-tests.el
@@ -956,7 +956,11 @@ ruby-deftest-indent
(kill-buffer buf)))))
(ruby-deftest-indent "ruby.rb")
+(ruby-deftest-indent "ruby-after-operator-indent.rb")
+(ruby-deftest-indent "ruby-block-indent.rb")
+(ruby-deftest-indent "ruby-method-call-indent.rb")
(ruby-deftest-indent "ruby-method-params-indent.rb")
+(ruby-deftest-indent "ruby-parenless-call-arguments-indent.rb")
(ert-deftest ruby--test-chained-indentation ()
(with-temp-buffer
next prev parent reply other threads:[~2022-12-28 21:24 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-19 2:54 bug#60186: 29.0.60; ruby-mode indentation of multi-line expressions Aaron Jensen
2022-12-20 2:12 ` Dmitry Gutov
2022-12-20 2:17 ` Dmitry Gutov
2022-12-20 4:48 ` Aaron Jensen
2022-12-20 5:56 ` Aaron Jensen
2022-12-20 15:53 ` Dmitry Gutov
2022-12-20 16:19 ` Dmitry Gutov
2022-12-20 17:31 ` Dmitry Gutov
2022-12-21 1:34 ` Aaron Jensen
2022-12-20 20:05 ` Aaron Jensen
2022-12-21 22:48 ` Dmitry Gutov
2022-12-22 2:31 ` Aaron Jensen
2022-12-22 21:21 ` Dmitry Gutov
2022-12-23 4:12 ` Aaron Jensen
2022-12-23 22:26 ` Dmitry Gutov
2022-12-24 0:17 ` Aaron Jensen
2022-12-24 22:47 ` Dmitry Gutov
2022-12-25 0:12 ` Aaron Jensen
2022-12-25 21:23 ` Dmitry Gutov
2022-12-25 21:29 ` bug#60321: 29.0.60; ruby-mode indentation of hash or array as first arg in multiline method call Dmitry Gutov
2022-12-25 23:46 ` Aaron Jensen
2022-12-27 1:16 ` Dmitry Gutov
2022-12-27 1:38 ` Aaron Jensen
2022-12-25 0:14 ` bug#60186: 29.0.60; ruby-mode indentation of multi-line expressions Aaron Jensen
2022-12-25 21:29 ` Dmitry Gutov
2022-12-27 1:28 ` Dmitry Gutov
2022-12-27 1:47 ` Aaron Jensen
2022-12-27 15:56 ` Dmitry Gutov
2022-12-27 16:34 ` Aaron Jensen
2022-12-27 23:04 ` Dmitry Gutov
2022-12-28 0:38 ` Aaron Jensen
2022-12-28 1:02 ` Dmitry Gutov
2022-12-28 3:47 ` Aaron Jensen
2022-12-28 12:47 ` Dmitry Gutov
2022-12-28 21:24 ` Dmitry Gutov [this message]
2022-12-29 22:59 ` Aaron Jensen
2022-12-30 15:02 ` Dmitry Gutov
2022-12-30 18:00 ` Aaron Jensen
2022-12-30 18:16 ` Aaron Jensen
2022-12-30 22:07 ` Dmitry Gutov
2022-12-31 1:11 ` Aaron Jensen
2023-01-22 3:02 ` Dmitry Gutov
2023-01-22 5:15 ` Aaron Jensen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=118143a7-0703-2538-9853-7009064e7bca@yandex.ru \
--to=dgutov@yandex.ru \
--cc=60186@debbugs.gnu.org \
--cc=aaronjensen@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).