unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Aaron Jensen <aaronjensen@gmail.com>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: 60321@debbugs.gnu.org
Subject: bug#60321: 29.0.60; ruby-mode indentation of hash or array as first arg in multiline method call
Date: Sat, 31 Aug 2024 17:54:05 -0700	[thread overview]
Message-ID: <CAHyO48wtUB_qypCM2K75DKEDN=WRLQrBWFjChhCdNrObG6D5AQ@mail.gmail.com> (raw)
In-Reply-To: <CAHyO48yqyVRktsBzqjePy+5s76GYSVW6SX3fFOPXKv9Pxc87-g@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 2732 bytes --]

Updated patch with more precise variables in the new test.


Aaron


On Sat, Aug 31, 2024 at 7:41 PM, Aaron Jensen <aaronjensen@gmail.com> wrote:

> I made an attempt at this (attached). It introduces a new variable:
>
> ruby-bracketed-args-indent
>
> It is set to t by default and the behavior will be as it was before this
> patch.
>
> If it is something other than t, it will cause enable indentation like
> this:
>
> update({
>   key => value,
>   other_key:
> }, {
>   key => value,
>   other_key:
> })
>
> update([
>   1,
>   2
> ], [
>   3,
>   4
> ])
>
> It does not handle cases such as:
>
> some_method({
>   key: :value
> },
> other_argument)
>
> It will indent other_argument to be aligned with the (. This could be
> elaborated further, but I contend that if people are formatting their code
> this way that they likely have rather idiosyncratic formatting requirements
> and they would be best left to do what they want manually.
>
> Thanks,
>
>
> Aaron
>
>
> On Mon, Dec 26, 2022 at 8:38 PM, Aaron Jensen <aaronjensen@gmail.com>
> wrote:
>
> On Mon, Dec 26, 2022 at 8:16 PM Dmitry Gutov <dgutov@yandex.ru> wrote:
>
> Vim's choice looks saner to my eye. Probably comes down to the choice of
> indentation algorithm, though.
>
> Agreed, though it's hard to pick which is more sane when all the options
> start with insanity.
>
> If I had to type it as above, I would probably indent it like this:
>
> and_in_a_method_call({
> no: :difference
> },
> foo,
> bar)
>
> But I can't imagine that would be easy to implement at all, so I wouldn't
> bother.
>
> The indentation logic itself might be not that difficult to write, but the
> fact that the expression will have to be reindented as soon as the method
> call grows a second argument (after the user types the comma?), makes it a
> hard sell usability-wise.
>
> Right, I think that's just more of the same thing... We are looking at
> ways of writing code that are out of the realm of reason. It's a challenge
> to define behavior when the behavior could very well be undefined. But, if
> we must define it, then what are our guiding principles? Not having to
> reindent preceding lines when adding a new line may be a very reasonable
> one. In that case, the only two options I could think of would be:
>
> and_in_a_method_call({
> no: :difference
> },
> foo,
> bar)
>
> or
>
> and_in_a_method_call({
> no: :difference
> },
> foo,
> bar)
>
> The difference being if we decide to dedent upon the last closing
> indent-requiring-token or the first.
>
> I think a reasonable rule of thumb for a human might be: "If you open N
> indents on one line, you must close N indents on one line". Any time you
> stray away from this, behavior becomes... not ideal.
>
> Aaron
>
>

[-- Attachment #1.2: Type: text/html, Size: 6598 bytes --]

[-- Attachment #2: 0001-Add-ruby-bracketed-argument-indentation-option.patch --]
[-- Type: application/octet-stream, Size: 3157 bytes --]

From 1b5826dda4ff8d014d37f90b22569e376db85296 Mon Sep 17 00:00:00 2001
From: Aaron Jensen <aaronjensen@gmail.com>
Date: Sat, 31 Aug 2024 19:31:20 -0400
Subject: [PATCH] Add ruby bracketed argument indentation option

* lisp/progmodes/ruby-mode.el (ruby-bracketed-args-indent),
(ruby-smie-rules): New option
* test/lisp/progmodes/ruby-mode-resources/ruby-bracketed-args-indent.rb:
* test/lisp/progmodes/ruby-mode-tests.el
  ("ruby-parenless-call-arguments-indent.rb"): New test case
---
 lisp/progmodes/ruby-mode.el                   | 23 +++++++++++++
 .../ruby-bracketed-args-indent.rb             | 32 +++++++++++++++++++
 test/lisp/progmodes/ruby-mode-tests.el        |  1 +
 3 files changed, 56 insertions(+)
 create mode 100644 test/lisp/progmodes/ruby-mode-resources/ruby-bracketed-args-indent.rb

diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index 3bcfa9ee7df..85cdce39937 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -472,6 +472,26 @@ ruby-parenless-call-arguments-indent
   :safe 'booleanp
   :version "29.1")
 
+(defcustom ruby-bracketed-args-indent t
+  "Non-nil to align the contents of bracketed arguments with the brackets.
+
+Example:
+
+  qux({
+       foo => bar
+     })
+
+Set it to nil to align to the beginning of the statement:
+
+  qux({
+    foo => bar
+  })
+
+Only has effect when `ruby-use-smie' is t."
+  :type 'boolean
+  :safe 'booleanp
+  :version "31.1")
+
 (defcustom ruby-deep-arglist t
   "Deep indent lists in parenthesis when non-nil.
 Also ignores spaces after parenthesis when `space'.
@@ -826,6 +846,9 @@ ruby-smie-rules
       ))
     (`(:before . ,(or "(" "[" "{"))
      (cond
+      ((and (not (eq ruby-bracketed-args-indent t))
+            (smie-rule-prev-p "," "(" "["))
+       (cons 'column (current-indentation)))
       ((and (equal token "{")
             (not (smie-rule-prev-p "(" "{" "[" "," "=>" "=" "return" ";" "do"))
             (save-excursion
diff --git a/test/lisp/progmodes/ruby-mode-resources/ruby-bracketed-args-indent.rb b/test/lisp/progmodes/ruby-mode-resources/ruby-bracketed-args-indent.rb
new file mode 100644
index 00000000000..ac7a73463bf
--- /dev/null
+++ b/test/lisp/progmodes/ruby-mode-resources/ruby-bracketed-args-indent.rb
@@ -0,0 +1,32 @@
+update({
+  key => value,
+  other_key:
+}, {
+  key => value,
+  other_key:
+})
+
+update([
+  1,
+  2
+], [
+  3,
+  4
+])
+
+update([{
+  key: "value"
+}, {
+  key: "value"
+}])
+
+update(arg1, {
+  foo: "bar"
+}, [
+  1,
+  2
+], arg2)
+
+# Local Variables:
+# ruby-bracketed-args-indent: nil
+# End:
diff --git a/test/lisp/progmodes/ruby-mode-tests.el b/test/lisp/progmodes/ruby-mode-tests.el
index 2b8506a7adc..c9cde791baa 100644
--- a/test/lisp/progmodes/ruby-mode-tests.el
+++ b/test/lisp/progmodes/ruby-mode-tests.el
@@ -992,6 +992,7 @@ "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")
+(ruby-deftest-indent "ruby-bracketed-args-indent.rb")
 
 (ert-deftest ruby--test-chained-indentation ()
   (with-temp-buffer
-- 
2.42.1


  reply	other threads:[~2024-09-01  0:54 UTC|newest]

Thread overview: 53+ 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
2024-08-31 23:41                                 ` Aaron Jensen
2024-09-01  0:54                                   ` Aaron Jensen [this message]
2024-09-01 16:36                                     ` Dmitry Gutov
2024-09-01 19:28                                       ` Aaron Jensen
2024-09-02  0:19                                         ` Dmitry Gutov
2024-09-02  0:49                                           ` Aaron Jensen
2024-09-02  1:10                                             ` Dmitry Gutov
2024-09-02  1:56                                               ` Aaron Jensen
2024-09-02 19:01                                                 ` Dmitry Gutov
2024-09-02 19:21                                                   ` 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
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='CAHyO48wtUB_qypCM2K75DKEDN=WRLQrBWFjChhCdNrObG6D5AQ@mail.gmail.com' \
    --to=aaronjensen@gmail.com \
    --cc=60321@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    /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).