Skip to content

Commit dddf0a5

Browse files
juszczaknNick
authored andcommitted
add :extra-configs option, pull in additional :extra-indents
Allows pulling in :extra-indents from other configs. extra-configs are respected in order listed, so later configs overwrite earlier ones. :extra-indents in the base config override everything, allowing users to overrule indents pulled in that they don't agree with. :extra-configs file paths are expected to be relative to the base config.
1 parent f3ba8d4 commit dddf0a5

File tree

10 files changed

+90
-2
lines changed

10 files changed

+90
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ pom.xml.asc
99
.nrepl-port
1010
reports
1111
.cpcache
12+
.clj-kondo
13+
.lsp

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ In order to load the standard configuration file from Leiningen, add the
284284
Paths can also be passed as command line arguments. If the path is
285285
`-`, then the input is STDIN, and the output STDOUT.
286286

287+
* `:extra-configs` -
288+
additional config files that will also be imported and merged into
289+
the base configuration. Is not recursive, so `:extra-configs` in
290+
imported configs will not be respected.
291+
292+
File paths are expected to be relative paths from the base config file.
293+
294+
Only certain keys from other configs will be respected:
295+
* `:extra-indents`
296+
287297
## License
288298

289299
Copyright © 2023 James Reeves

cljfmt/project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@
4444
:opts ["--static"
4545
"--libc=musl"
4646
"-H:CCompilerOption=-Wl,-z,stack-size=2097152"]}}
47-
:provided {:dependencies [[org.clojure/clojurescript "1.11.60"]]}})
47+
:provided {:dependencies [[org.clojure/clojurescript "1.11.60"]]}
48+
:dev {:resource-paths ["test_resources"]}})

cljfmt/src/cljfmt/config.clj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,33 @@
5959
(defn- directory? [path]
6060
(some-> path io/file .getAbsoluteFile .isDirectory))
6161

62+
(def ^:private extra-config-respected-keys [:extra-indents])
63+
64+
(defn- merge-keys
65+
[init coll ks]
66+
(reduce (fn [acc k]
67+
(update acc k merge (get coll k)))
68+
init ks))
69+
70+
(defn- merge-extra-configs
71+
[path config]
72+
(let [base-dir (-> path io/file ((fn [^java.io.File f] (.getParent f))))]
73+
;; extra-configs later in the list override earlier ones.
74+
(loop [confs (mapv #(io/file base-dir %) (:extra-configs config))
75+
econf {}]
76+
(if (empty? confs)
77+
;; A little confusing but: We're merging the base config's
78+
;; values into the extra-configs, and then back into the base
79+
;; config, so we allow the base config to have the final say.
80+
(merge config (merge-keys econf config extra-config-respected-keys))
81+
(let [;; Only merging select values from other configs.
82+
xtra (-> confs first read-config
83+
(select-keys extra-config-respected-keys))]
84+
(recur (rest confs) ;;(merge econf xtra)
85+
(reduce-kv (fn [acc k v]
86+
(update acc k merge v))
87+
econf xtra)))))))
88+
6289
(defn load-config
6390
"Load a configuration merged with a map of sensible defaults. May take
6491
an path to a config file, or to a directory to search. If no argument
@@ -70,4 +97,5 @@
7097
path)]
7198
(->> (some-> path read-config)
7299
(merge default-config)
100+
(merge-extra-configs path)
73101
(convert-legacy-keys)))))

cljfmt/test/cljfmt/config_test.clj

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
(ns cljfmt.config-test
22
(:require [cljfmt.config :as config]
3-
[clojure.test :refer [deftest is]]))
3+
[clojure.test :refer [deftest is testing]]
4+
[clojure.java.io :as io]))
45

56
(deftest test-convert-legacy-keys
67
(is (= {:indents {'foo [[:inner 0]]}}
78
(config/convert-legacy-keys {:indents {'foo [[:inner 0]]}})))
89
(is (= {:extra-indents {'foo [[:inner 0]]}}
910
(config/convert-legacy-keys {:legacy/merge-indents? true
1011
:indents {'foo [[:inner 0]]}}))))
12+
13+
(deftest test-load-config-extra-configs
14+
(testing ":extra-configs allows you to import :extra-indents"
15+
(is (= (config/load-config (io/resource "config_test/empty-cljfmt.edn"))
16+
(-> (config/load-config (io/resource "config_test/cljfmt.edn"))
17+
(dissoc :extra-configs)
18+
(assoc :extra-indents {})))
19+
(str "should only differ by `:extra-indents` (via :extra-configs)."
20+
" All other keys are ignored."))
21+
(is (= '{;; exists only in base config `test_resources/cljfmt.edn`
22+
com.unrelated/a [[:inner 0]]
23+
com.foo/a [[:inner 0]],
24+
;; exists only in `test_resources/extra1-cljfmt.edn`
25+
com.foo/b [[:inner 1]],
26+
;; overwritten in `test_resources/extra2-cljfmt.edn`
27+
com.foo/c [[:inner 2]],
28+
com.foo/d [[:inner 2]],
29+
;; exists only in `test_resources/extra2-cljfmt.edn`
30+
com.foo/e [[:inner 2]]}
31+
(:extra-indents (config/load-config (io/resource "config_test/cljfmt.edn"))))
32+
(str "should respect :extra-configs in order (later is higher-prio),"
33+
" with base highest prio."))))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{:extra-indents {com.foo/a [[:inner 0]]
2+
com.unrelated/a [[:inner 0]]}
3+
:extra-configs ["extra1-cljfmt.edn"
4+
"extra2-cljfmt.edn"]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{:parallel? true
2+
:paths ["x"]
3+
:sort-ns-references? true
4+
:extra-indents {;; exists in base
5+
com.foo/a [[:inner 1]]
6+
;; not included in later config
7+
com.foo/b [[:inner 1]]
8+
;; overridden in later configs
9+
com.foo/c [[:inner 1]]
10+
com.foo/d [[:inner 1]]}
11+
:extra-configs []}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{:parallel? false
2+
:paths ["y"]
3+
:sort-ns-references? false
4+
:extra-indents {com.foo/c [[:inner 2]]
5+
com.foo/d [[:inner 2]]
6+
com.foo/e [[:inner 2]]}
7+
:extra-configs []}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:extra-configs ["config.edn"]}

0 commit comments

Comments
 (0)