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

paredit: split: support ops after update #364

Merged
merged 1 commit into from
Feb 19, 2025
Merged
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
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
Loading