Skip to content
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,13 @@ In order to load the standard configuration file from Leiningen, add the
Defaults to false.

* `:split-keypairs-over-multiple-lines?` -
true if cljfmt should break hashmaps onto multiple lines. This will
convert `{:a 1 :b 2}` to `{:a 1\n:b 2}`. Defaults to false.
true if cljfmt should break hashmaps so that each key and value pair
is on its own line. This will convert `{:a 1 :b 2}` to `{:a 1\n:b 2}`.
Defaults to false. See [the examples](docs/SPLIT-KEYPAIRS.md).

* `:split-keypairs-only-in-multiline-forms?` -
as above, but only for hashmaps that already span more than one line.
Hashmaps written on a single line are left alone. Defaults to false.

[indents.md]: docs/INDENTS.md
[community style recommendation]: https://guide.clojure.style/#one-space-indent
Expand Down
86 changes: 51 additions & 35 deletions cljfmt/src/cljfmt/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -377,31 +377,32 @@
(read-resource "cljfmt/blank_line_forms/clojure.clj"))

(def default-options
{:alias-map {}
:align-binding-columns? false
:align-map-columns? false
:align-single-column-lines? false
:aligned-forms default-aligned-forms
:max-column-alignment-gap nil
:max-column-alignment-width nil
:blank-line-forms blank-line-forms
:blank-lines-separate-alignment? false
:extra-aligned-forms {}
:extra-blank-line-forms {}
:extra-indents {}
:function-arguments-indentation :community
:indent-line-comments? false
:indentation? true
:indents default-indents
:normalize-newlines-at-file-end? false
:insert-missing-whitespace? true
:remove-blank-lines-in-forms? false
:remove-consecutive-blank-lines? true
:remove-multiple-non-indenting-spaces? false
:remove-surrounding-whitespace? true
:remove-trailing-whitespace? true
:sort-ns-references? false
:split-keypairs-over-multiple-lines? false})
{:alias-map {}
:align-binding-columns? false
:align-map-columns? false
:align-single-column-lines? false
:aligned-forms default-aligned-forms
:max-column-alignment-gap nil
:max-column-alignment-width nil
:blank-line-forms blank-line-forms
:blank-lines-separate-alignment? false
:extra-aligned-forms {}
:extra-blank-line-forms {}
:extra-indents {}
:function-arguments-indentation :community
:indent-line-comments? false
:indentation? true
:indents default-indents
:normalize-newlines-at-file-end? false
:insert-missing-whitespace? true
:remove-blank-lines-in-forms? false
:remove-consecutive-blank-lines? true
:remove-multiple-non-indenting-spaces? false
:remove-surrounding-whitespace? true
:remove-trailing-whitespace? true
:sort-ns-references? false
:split-keypairs-over-multiple-lines? false
:split-keypairs-only-in-multiline-forms? false})
Comment thread
sirmspencer marked this conversation as resolved.
Resolved

(defmulti ^:private indenter-fn
(fn [_sym _context [type & _args]] type))
Expand Down Expand Up @@ -468,12 +469,6 @@
(transform form edit-all #(should-indent? % opts)
#(indent-line % indenter context)))))

(defn- map-key? [zloc]
(and (z/map? (z/up zloc))
(even? (index-of zloc))
(not (uneval? zloc))
(not (z/whitespace-or-comment? zloc))))

(defn- preceded-by-line-break? [zloc]
(loop [previous (z/left* zloc)]
(cond
Expand All @@ -482,14 +477,33 @@
(z/whitespace-or-comment? previous)
(recur (z/left* previous)))))

(defn- map-key-without-line-break? [zloc]
(and (map-key? zloc) (z/left zloc) (not (preceded-by-line-break? zloc))))

(defn- insert-newline-left [zloc]
(z/insert-left* zloc (n/newlines 1)))

(defn- multiline? [zloc]
(includes? (z/string zloc) "\n"))

(defn- multiline-map? [zloc]
(and (z/map? zloc) (multiline? zloc)))

(defn- split-map-keypairs [zloc]
(if-some [child (z/down* zloc)]
(loop [child child, key? true]
(let [sexpr? (z/sexpr-able? child)
child (cond-> child
(and sexpr? key? (z/left child)
(not (preceded-by-line-break? child)))
insert-newline-left)]
(if-some [right (z/right* child)]
(recur right (cond-> key? sexpr? not))
(z/up child))))
zloc))

(defn split-keypairs-over-multiple-lines [form]
(transform form edit-all map-key-without-line-break? insert-newline-left))
(transform form edit-all z/map? split-map-keypairs))

(defn split-keypairs-only-in-multiline-forms [form]
(transform form edit-all multiline-map? split-map-keypairs))

(defn reindent
([form]
Expand Down Expand Up @@ -921,6 +935,8 @@
sort-ns-references)
(cond-> (:split-keypairs-over-multiple-lines? opts)
split-keypairs-over-multiple-lines)
(cond-> (:split-keypairs-only-in-multiline-forms? opts)
split-keypairs-only-in-multiline-forms)
(cond-> (:remove-consecutive-blank-lines? opts)
remove-consecutive-blank-lines)
(cond-> (:remove-surrounding-whitespace? opts)
Expand Down
3 changes: 3 additions & 0 deletions cljfmt/src/cljfmt/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
[nil "--[no-]split-keypairs-over-multiple-lines"
:default (:split-keypairs-over-multiple-lines? defaults)
:id :split-keypairs-over-multiple-lines?]
[nil "--[no-]split-keypairs-only-in-multiline-forms"
:default (:split-keypairs-only-in-multiline-forms? defaults)
:id :split-keypairs-only-in-multiline-forms?]
[nil "--[no-]read-clj-config-files"
:default (:read-clj-config-files? defaults)
:id :read-clj-config-files?]])
Expand Down
Loading
Loading