Skip to content

Commit

Permalink
Fix #36: Add newline after adding new element to top level map with a…
Browse files Browse the repository at this point in the history
…ssoc-in (#38)
  • Loading branch information
borkdude authored Mar 28, 2024
1 parent 9c35aba commit daa7c5e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
[rewrite-clj](https://github.com/clj-commons/rewrite-clj) with common operations
to update EDN while preserving whitespace and comments.

## 0.4.8

- Add newline after adding new element to top level map with `assoc-in`

## 0.4.7

- [#35](https://github.com/borkdude/rewrite-edn/issues/35): Bump rewrite-clj to 1.1.47
Expand Down
2 changes: 2 additions & 0 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:tasks
{test:clj (apply clojure "-M:test" *command-line-args*)}}
25 changes: 16 additions & 9 deletions src/borkdude/rewrite_edn/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

(defn indent [zloc key-count first-key-loc]
(let [current-loc (meta (z/node zloc))]
(if (or (= 1 key-count)
(not= (:row first-key-loc) (:row current-loc)))
(if (and first-key-loc
(or (= 1 key-count)
(not= (:row first-key-loc) (:row current-loc))))
(let [zloc (-> zloc
(z/insert-space-right (dec (dec (:col first-key-loc))))
z/insert-newline-right)]
Expand Down Expand Up @@ -119,7 +120,7 @@
(let [zloc (z/of-node zloc)
tag (z/tag zloc)]
(cond
(= tag :map)
(= :map tag)
(let [node (z/node zloc)
nil? (and (identical? :token (node/tag node))
(nil? (node/sexpr node)))
Expand Down Expand Up @@ -187,11 +188,15 @@
(z/root)
(update k f args))
(let [zloc (z/down zloc)
zloc (skip-right zloc)]
(loop [zloc zloc]
zloc (skip-right zloc)
first-key-loc (when-let [first-key (z/node zloc)]
(meta first-key))]
(loop [key-count 0
zloc zloc]
(if (z/rightmost? zloc)
(-> zloc
(z/insert-right (node/coerce k))
(indent key-count first-key-loc)
(z/right)
(z/insert-right (apply f (node/coerce nil) args))
(z/root))
Expand All @@ -200,16 +205,18 @@
(let [zloc (-> zloc (z/right) (skip-right))
zloc (z/replace zloc (node/coerce (apply f (z/node zloc) args)))]
(z/root zloc))
(recur (-> zloc
(recur (inc key-count)
(-> zloc
;; move over value to next key
(skip-right)
(z/right)
(skip-right))))))))))))

(defn update-in [forms keys f args]
(if (= 1 (count keys))
(update forms (first keys) f args)
(update forms (first keys) #(update-in % (rest keys) f args))))
(-> (if (= 1 (count keys))
(update forms (first keys) f args)
(update forms (first keys) #(update-in % (rest keys) f args)))
(mark-for-positional-recalc)))

(defn assoc-in [forms keys v]
(if (= 1 (count keys))
Expand Down
24 changes: 19 additions & 5 deletions test/borkdude/rewrite_edn_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
(str (r/assoc-in (r/parse-string "{:deps {foo/foo {:mvn/version \"0.1.0\"}}}")
[:deps 'foo/foo :mvn/version]
"0.2.0"))))
(is (= "{:a 1 :b {:c 1}}"
(is (= "{:a 1\n :b {:c 1}}"
(str (r/assoc-in (r/parse-string "{:a 1}") [:b :c] 1))))
(is (= (str "{:deps {foo {:mvn/version \"x\"}\n"
" bar {:mvn/version \"y\"}}}")
Expand Down Expand Up @@ -302,8 +302,8 @@
(r/conj [7 8])
str))))
(testing "Combine with update/update-in"
(is (= (str "{:a [1 2 3 4]"
" :b (2 1)"
(is (= (str "{:a [1 2 3 4]\n"
" :b (2 1)\n"
" :c #{1}}")
(-> "{:a [1 2 3]}"
(r/parse-string)
Expand All @@ -319,8 +319,22 @@
(r/parse-string)
(r/conj 1)))))
(testing "Update with fnil conj"
(is (= "{:a [1 2 3] :b [1]}"
(is (= "{:a [1 2 3]\n :b [1]}"
(-> "{:a [1 2 3]}"
(r/parse-string)
(r/update :b (r/fnil r/conj []) 1)
str)))))
str))))
(testing "issue 36, add newline with assoc-in"
(let [deps-str "
{:deps
{hiccup {:mvn/version \"1.0.4\"}}}
"]
(is (= "
{:deps
{hiccup {:mvn/version \"1.0.4\"}}
:aliases {:neil {}}}
"
(-> deps-str
r/parse-string
(r/assoc-in [:aliases :neil] {})
str))))))

0 comments on commit daa7c5e

Please sign in to comment.