Skip to content

Commit

Permalink
Use :block rule as baseline indentation for :stair rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bullfest committed Feb 21, 2023
1 parent 14c18e5 commit 65a9fc3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/cljstyle/format/indent.clj
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,15 @@
(defn- stair-indent
"Calculate how many spaces the node at this location should be indented as a
conditional block. Returns nil if the rule does not apply."
[zloc rule-key idx]
[zloc rule-key idx list-indent-size]
(when (indent-matches? rule-key (zl/form-symbol-full zloc))
(let [zloc-idx (index-of zloc)
leading-forms (if (some-> zloc (nth-form idx) first-form-in-line?)
0
idx)
indent (inner-indent zloc rule-key 0 nil)]
indent (if (zero? idx)
(block-indent zloc rule-key idx list-indent-size (inc idx))
(inner-indent zloc rule-key 0 nil))]
(if (even? (- zloc-idx leading-forms))
(+ indent indent-size)
indent))))
Expand Down Expand Up @@ -322,7 +324,7 @@
(let [[_ idx] rule]
(fn stair-indenter
[zloc]
(stair-indent zloc rule-key idx)))))
(stair-indent zloc rule-key idx list-indent-size)))))


(defn- unindent-line
Expand Down
14 changes: 14 additions & 0 deletions test/cljstyle/format/indent_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
"(assoc {}\n :foo bar\n :foo2 bar2)"))))


(defn space
"A string with n spaces"
[n]
(apply str (repeat n " ")))


(deftest stair-indentation
(let [indents {'cond [[:stair 0]]
'condp [[:stair 2]]
Expand All @@ -70,6 +76,14 @@
indent/reindent-lines {:indents indents}
"(cond\na? a\n b? b)"
"(cond\n a? a\n b? b)"))
(is (rule-reformatted?
indent/reindent-lines {:indents indents}
"(cond a? a\n b? b)"
(str "(cond a? a\n" (space 6) "b? b)")))
(is (rule-reformatted?
indent/reindent-lines {:indents indents}
"(cond a?\n a\n b?\n b)"
(str "(cond a?\n" (space 8) "a\n" (space 6) "b?\n" (space 8) "b)")))
(is (rule-reformatted?
indent/reindent-lines {:indents indents}
"(cond\na?\n a\nb?\n b)"
Expand Down

0 comments on commit 65a9fc3

Please sign in to comment.