clojure-mode icon indicating copy to clipboard operation
clojure-mode copied to clipboard

Alignment bug with comments/forms following a closing delimiter

Open yuhan0 opened this issue 6 years ago • 5 comments

Expected behavior

M-x clojure-align on the following forms should be a no-op:

{:a [123
     456
     789] :b 10}

(cond foo  {:bar 1  ; initialize the Bar
            :baz 2} ; and the Baz
      quux {})

Actual behavior

The second "column" is pushed farther and farther to the right every time clojure-align is called.

{:a [123
     456
     789] :b 10}

=>

{:a       [123
           456
           789] :b 10}

=>

{:a             [123
                 456
                 789] :b 10}

=>

{:a                   [123
                       456
                       789] :b 10}

Steps to reproduce the problem

Execute clojure-align on any hashmap or function defined in clojure-cond-align-forms, which has a symbol or comment following a closing delimiter that is on a different line from its opening delimiter:

;; this is fine
{:a [1 2] ; comment
 }

;; this is also fine
{:a [[1
      2] ; comment
     3]
 }

;; but not this
{:a [1
     2
     3] ; comment
 }

The offending delimiter must belong to the second or greater "column" of the alignment

;; This is alright
(are [x y z] (= (count y) x (inc z))
  [100
   200
   300] 3 2)

;; This is not
(are [x y z] (= x (count y) (inc z))
  3 [100
     200
     300] 2)

Environment & Version information

clojure-mode version information

Include here the version string displayed by M-x clojure-mode-display-version. Here's an example:

clojure-mode (version 5.10.0)

Emacs version

26.2

Operating system

macOS 10.14

yuhan0 avatar May 04 '19 10:05 yuhan0

@Malabarba is our master of alignment. :-) Perhaps he has an idea why this is happening?

bbatsov avatar May 08 '19 15:05 bbatsov

I experienced a similar issue with long keys destructuring (see screencast):

(let [{:keys [one two three
              four five six
              seven eight nine] :as whole} bigstuff])

Setting clojure-align-separator to 'group fixed it, apparently without changing any other alignment behavior from default (although I didn't test it extensively quite yet)

skuro avatar Jun 24 '19 12:06 skuro

FWIW, @skuro's suggestion works for me too. Tested on:

(are [x y z] (= x (count y) (inc z))
  3 [100
     200
     300] 2)

As reported by @yuhan0.

manuel-uberti avatar Jun 24 '19 12:06 manuel-uberti

Changing clojure-align-separator to 'group did help with this bug. However the behavior is slightly different, it doesn't align across multi-line expressions:

{:x    [123]
 :abcd [123
        456]
 :long-var-name [789]}

yuhan0 avatar Jun 25 '19 09:06 yuhan0

Hi everyone.

I can verify all of the bugs and solutions mentioned. Sadly all I can say is that it's either a bug in clojure--search-whitespace-after-next-sexp or an Emacs bug. =/

The Emacs alignment engine is pretty dense and I remember when I first implemented this feature in clojure-mode it was very hit-or-miss.

Anyone who wants to help is invited to read through the docs for align-region and for align-rules-list and try to determine if there's a better way to do this. `

Malabarba avatar Jul 29 '19 22:07 Malabarba