Skip to content

Commit e2f7ef2

Browse files
committed
Use :block rule as baseline indentation for :stair rule
1 parent 14c18e5 commit e2f7ef2

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/cljstyle/format/indent.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,15 @@
287287
(defn- stair-indent
288288
"Calculate how many spaces the node at this location should be indented as a
289289
conditional block. Returns nil if the rule does not apply."
290-
[zloc rule-key idx]
290+
[zloc rule-key idx list-indent-size]
291291
(when (indent-matches? rule-key (zl/form-symbol-full zloc))
292292
(let [zloc-idx (index-of zloc)
293293
leading-forms (if (some-> zloc (nth-form idx) first-form-in-line?)
294294
0
295295
idx)
296-
indent (inner-indent zloc rule-key 0 nil)]
296+
indent (if (zero? idx)
297+
(block-indent zloc rule-key idx list-indent-size (inc idx))
298+
(inner-indent zloc rule-key 0 nil))]
297299
(if (even? (- zloc-idx leading-forms))
298300
(+ indent indent-size)
299301
indent))))
@@ -322,7 +324,7 @@
322324
(let [[_ idx] rule]
323325
(fn stair-indenter
324326
[zloc]
325-
(stair-indent zloc rule-key idx)))))
327+
(stair-indent zloc rule-key idx list-indent-size)))))
326328

327329

328330
(defn- unindent-line

test/cljstyle/format/indent_test.clj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
"(assoc {}\n :foo bar\n :foo2 bar2)"))))
6262

6363

64+
(defn space
65+
"A string with n spaces"
66+
[n]
67+
(apply str (repeat n " ")))
68+
69+
6470
(deftest stair-indentation
6571
(let [indents {'cond [[:stair 0]]
6672
'condp [[:stair 2]]
@@ -70,6 +76,14 @@
7076
indent/reindent-lines {:indents indents}
7177
"(cond\na? a\n b? b)"
7278
"(cond\n a? a\n b? b)"))
79+
(is (rule-reformatted?
80+
indent/reindent-lines {:indents indents}
81+
"(cond a? a\n b? b)"
82+
(str "(cond a? a\n" (space 6) "b? b)")))
83+
(is (rule-reformatted?
84+
indent/reindent-lines {:indents indents}
85+
"(cond a?\n a\n b?\n b)"
86+
(str "(cond a?\n" (space 8) "a\n" (space 6) "b?\n" (space 8) "b)")))
7387
(is (rule-reformatted?
7488
indent/reindent-lines {:indents indents}
7589
"(cond\na?\n a\nb?\n b)"

0 commit comments

Comments
 (0)