diff --git a/src/rewrite_clj/paredit.cljc b/src/rewrite_clj/paredit.cljc index fb6bf05a..e194afc6 100644 --- a/src/rewrite_clj/paredit.cljc +++ b/src/rewrite_clj/paredit.cljc @@ -110,22 +110,28 @@ (defn- string-node? [zloc] (= (some-> zloc z/node type) (type (nd/string-node " ")))) +(defn- remove-right-sibs + [zloc] + (u/remove-right-while zloc (constantly true))) + ;;***************************** ;; Paredit functions ;;***************************** (defn kill - "Kill all sibling nodes to the right of the current node in `zloc`. + "Returns `zloc` with the current node and all sibling nodes to the right removed. + Locates `zloc` to node left of deleted node, else if no left node removes current node via [[rewrite-clj.zip/remove*]]. + + Makes no automatic whitespace adjustments. - - `[1 2| 3 4] => [1 2|]`" + - `[1 |2 3 4] => [1| ]` + - `[1 2 |3 4] => [1 2| ]` + - `[|1 2 3 4] => |[]` + - `[ |1 2 3 4] => [| ]`" [zloc] - (let [left (z/left* zloc)] - (-> zloc - (u/remove-right-while (constantly true)) - z/remove* - (#(if left - (global-find-by-node % (z/node left)) - %))))) + (let [zloc (remove-right-sibs zloc)] + (or (u/remove-and-move-left zloc) + (z/remove* zloc)))) (defn- kill-in-string-node [zloc pos] (if (= (z/string zloc) "\"\"") diff --git a/test/rewrite_clj/paredit_test.cljc b/test/rewrite_clj/paredit_test.cljc index 77f4ca1c..667c9da4 100644 --- a/test/rewrite_clj/paredit_test.cljc +++ b/test/rewrite_clj/paredit_test.cljc @@ -17,15 +17,31 @@ (doseq [opts zipper-opts] (testing (zipper-opts-desc opts) (doseq [[s expected] - [["[1⊚ 2 3 4]" "[⊚1]"] - ["[1 2]⊚ ; useless comment" "⊚[1 2]"] + [["⊚1 2 3 4" "◬"] + [" ⊚1 2 3 4" "⊚ "] ["[⊚1 2 3 4]" "⊚[]"] + ["[ ⊚1 2 3 4]" "[⊚ ]"] ;; 3 spaces are parsed as one node + ["⊚[]" "◬"] + ["[1⊚ 2 3 4]" "[⊚1]"] + ["[1 ⊚2 3 4]" "[1⊚ ]"] + ["[1 2 ⊚3 4]" "[1 2⊚ ]"] + ["[1 2 3 ⊚4]" "[1 2 3⊚ ]"] + ["[1 2]⊚ ; some comment" "⊚[1 2]"] ["[⊚[1 2 3 4]]" "⊚[]"] ["[1 2 3 4]⊚ 2" "⊚[1 2 3 4]"] - ["⊚[1 2 3 4] 5" "◬"]]] - (let [zloc (th/of-locmarked-string s opts)] - (is (= s (th/root-locmarked-string zloc)) "(sanity) before changes") - (is (= expected (-> zloc pe/kill th/root-locmarked-string)))))))) + ["⊚[1 2 3 4] 5" "◬"] + ["[1 [2 3]⊚ 4 5]" "[1 ⊚[2 3]]"] + ["[1 [2 [3 [4]]]⊚ 5 6]" "[1 ⊚[2 [3 [4]]]]"] + ["[1\n[2⊚\n[3\n4]\n5]]" "[1\n[⊚2]]"] + ["[1\n[2\n[3 \n⊚ 4]\n5]]" "[1\n[2\n[3 ⊚\n]\n5]]"] + ["[ \n \n \n ⊚1 2 3 4]" "[ \n \n \n⊚ ]"] + ["[ ⊚\n \n 1 2 3 4]" "[⊚ ]"] + ["[ \n ⊚\n 1 2 3 4]" "[ \n⊚ ]"] ;; multiple spaces are a single node + ["[ \n⊚ \n 1 2 3 4]" "[ ⊚\n]"]]] + (testing s + (let [zloc (th/of-locmarked-string s opts)] + (is (= s (th/root-locmarked-string zloc)) "(sanity) before changes") + (is (= expected (-> zloc pe/kill th/root-locmarked-string))))))))) (deftest kill-at-pos-test ;; for this pos fn test, ⊚ in `s` represents character row/col for the `pos`