Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use :block rule as baseline indentation for :stair rule #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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