Skip to content
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
24 changes: 15 additions & 9 deletions src/rewrite_clj/paredit.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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) "\"\"")
Expand Down
28 changes: 22 additions & 6 deletions test/rewrite_clj/paredit_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Loading