Skip to content

Commit

Permalink
paredit: split: support ops after update
Browse files Browse the repository at this point in the history
Contributes to #256
  • Loading branch information
lread committed Feb 19, 2025
1 parent 753b744 commit 281b1c4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
31 changes: 22 additions & 9 deletions src/rewrite_clj/paredit.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -686,25 +686,38 @@
zloc)))

(defn split
"Split current s-sexpression in two at given node `zloc`
- `[1 2 |3 4 5] => [1 2 3] [4 5]`"
"Return `zloc` with parent sequence split into to two sequences at current node.
Location is retained. If split would result in empty seq, returns `zloc` unchanged.
- `[1 2 |3 4 5] => [1 2 |3] [4 5]`
- `{|:a 1 :b 2} => {|:a} {:1 :b 2}`
- `#{:a |:b :c} => #{:a |:b} #{:c}`
- `(foo |bar baz boo) => (foo |:bar) (baz boop)`
- `[1 2 3 4 |5] => [1 2 3 4 |5]` unchanged"
[zloc]
(let [parent-loc (z/up zloc)]
(if-not parent-loc
zloc
(let [t (z/tag parent-loc)
lefts (reverse (remove-first-if-ws (rest (nodes-by-dir (z/right zloc) z/left*))))
rights (remove-first-if-ws (nodes-by-dir (z/right zloc) z/right*))]

lefts (-> zloc
z/right
(nodes-by-dir z/left*)
rest
remove-first-if-ws
reverse)
rights (-> zloc
z/right
(nodes-by-dir z/right*)
remove-first-if-ws)]
(if-not (and (seq lefts) (seq rights))
zloc
(-> parent-loc
(z/insert-left (create-seq-node t lefts))
(z/insert-left (create-seq-node t rights))
z/remove
(#(or (global-find-by-node % (z/node zloc))
(global-find-by-node % (last lefts))))))))))
rz/remove-and-move-left
z/left
z/down
z/rightmost))))))

(defn- split-string [zloc pos]
(let [bounds (-> zloc z/node meta)
Expand Down
15 changes: 11 additions & 4 deletions test/rewrite_clj/paredit_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,21 @@
(testing (zipper-opts-desc opts)
(doseq [[s expected]
[["[⊚1 2]" "[⊚1] [2]"]
["[1 2 ⊚3 4 5]" "[1 2 ⊚3] [4 5]"]
["[1 ⊚2 3 4]" "[1 ⊚2] [3 4]"]
["[1 2⊚ 3 4]" "[1 ⊚2] [3 4]"]
["[⊚1]" "[⊚1]"] ;; no-op
["[⊚1 2 3 4 5]" "[⊚1] [2 3 4 5]"]
["[⊚1]" "[⊚1]"] ;; no-op
["[1 2 3 4 ⊚5]" "[1 2 3 4 ⊚5]"] ;; no-op
["{⊚:a 1 :b 2}" "{⊚:a} {1 :b 2}"]
["(foo ⊚bar baz boop)" "(foo ⊚bar) (baz boop)"]
["#{:a ⊚:b :c}" "#{:a ⊚:b} #{:c}"]
["[⊚1 ;dill\n]" "[⊚1 ;dill\n]"] ;; no-op
["\n[1 ;dill\n ⊚2 ;dall\n 3 ;jalla\n]" "\n[1 ;dill\n ⊚2 ;dall\n] [3 ;jalla\n]"]]]
(let [zloc (th/of-locmarked-string s opts)]
(is (= s (th/root-locmarked-string zloc)) "(sanity) string before")
(is (= expected (-> zloc pe/split th/root-locmarked-string)) "string after"))))))
(testing s
(let [zloc (th/of-locmarked-string s opts)]
(is (= s (th/root-locmarked-string zloc)) "(sanity) string before")
(is (= expected (-> zloc pe/split th/root-locmarked-string)) "string after")))))))

(deftest split-at-pos-test
;; for this pos fn test, ⊚ in `s` represents character row/col the the `pos`
Expand Down

0 comments on commit 281b1c4

Please sign in to comment.