greglook / cljstyle

A tool for formatting Clojure code
Eclipse Public License 1.0
293 stars 39 forks source link

Styling after the discard macro is ignored, even if it is not in the discarded area #55

Closed aknorsh closed 3 years ago

aknorsh commented 4 years ago

Target

(defn fuga
  [x]
  (+ 1 2)
)

(let [p 1]
  #_(+ 1 2))

(defn hoge
  [x]
  (+ 1 2)
)

Expected Result

(defn fuga
  [x]
  (+ 1 2))

(let [p 1]
  #_(+ 1 2))

(defn hoge
  [x]
  (+ 1 2))

Actual Result

Styling is ignored after #_ .

(defn fuga
  [x]
  (+ 1 2))

(let [p 1]
  #_(+ 1 2))

(defn hoge
  [x]
  (+ 1 2)
)
saitouena commented 4 years ago

This happens when a discarded form is a sub-expression of some form. I mean:

;; I just removed a surrounding let-form `(let [p 1] ...)` from aknorsh's example
#_(+ 1 2)

(defn hoge
  [x]
  (+ 1 2)
  )  ;; this ")" will be moved to the upper line by cljstyle

In this case, cljstyle works well. It seems to be weird.

greglook commented 3 years ago

Curious - I'll add some tests and see if I can figure out what's going on.

greglook commented 3 years ago

Huh, this was due to some incorrect handling logic in the edit-walk function. If the discarded form was the last one in the subform, then (z/right* zloc) returns nil, and the walk concludes there are no more items to visit. Instead, it should pop back up until it finds something to go right from, or hits the root again.