From 5542b62f9c8b81e7966be9dbf5dd6284f61eab50 Mon Sep 17 00:00:00 2001 From: Lee Read Date: Wed, 19 Feb 2025 14:54:20 -0500 Subject: [PATCH] paredit: split: support ops after update (#364) Contributes to #256 --- src/rewrite_clj/paredit.cljc | 31 +++++++++++++++++++++--------- test/rewrite_clj/paredit_test.cljc | 15 +++++++++++---- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/rewrite_clj/paredit.cljc b/src/rewrite_clj/paredit.cljc index cccb858..69c279d 100644 --- a/src/rewrite_clj/paredit.cljc +++ b/src/rewrite_clj/paredit.cljc @@ -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) diff --git a/test/rewrite_clj/paredit_test.cljc b/test/rewrite_clj/paredit_test.cljc index f53393a..d5087cf 100644 --- a/test/rewrite_clj/paredit_test.cljc +++ b/test/rewrite_clj/paredit_test.cljc @@ -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`