|
1 |
| -(ns ^{ :doc "Utility operations for nodes." |
| 1 | +(ns ^{ :doc "Utility Operations for Zippers" |
2 | 2 | :author "Yannick Scherer" }
|
3 | 3 | rewrite-clj.zip.utils
|
4 |
| - (:require [fast-zip.core :as z])) |
5 |
| - |
6 |
| -;; ## Prefix |
7 |
| - |
8 |
| -(defmulti prefix |
9 |
| - "Prefix the value of a zipper node with the given string. This supports multi-lined strings." |
10 |
| - (fn [zloc p] |
11 |
| - (when zloc |
12 |
| - (first (z/node zloc)))) |
13 |
| - :default nil) |
14 |
| - |
15 |
| -(defmethod prefix nil |
16 |
| - [zloc prefix] |
17 |
| - (throw (Exception. (str "Cannot prefix value of type: " (first (z/node zloc)))))) |
18 |
| - |
19 |
| -(defmethod prefix :token |
20 |
| - [zloc prefix] |
21 |
| - (let [v (second (z/node zloc)) |
22 |
| - v1 (cond (string? v) (str prefix v) |
23 |
| - (symbol? v) (symbol (namespace v) (str prefix (name v))) |
24 |
| - (keyword? v) (keyword (namespace v) (str prefix (name v))) |
25 |
| - :else (throw (Exception. (str "Cannot prefix token: " v))))] |
26 |
| - (z/replace zloc [:token v1]))) |
27 |
| - |
28 |
| -(defmethod prefix :multi-line |
29 |
| - [zloc prefix] |
30 |
| - (let [[v & rst] (rest (z/node zloc))] |
31 |
| - (z/replace zloc (vec (list* :multi-line (str prefix v) rst))))) |
32 |
| - |
33 |
| -;; ## Suffix |
34 |
| - |
35 |
| -(defmulti suffix |
36 |
| - "Suffix the value of a zipper node with the given string. This supports multi-lined strings." |
37 |
| - (fn [zloc p] |
38 |
| - (when zloc |
39 |
| - (first (z/node zloc)))) |
40 |
| - :default nil) |
41 |
| - |
42 |
| -(defmethod suffix nil |
43 |
| - [zloc suffix] |
44 |
| - (throw (Exception. (str "Cannot suffix value of type: " (first (z/node zloc)))))) |
45 |
| - |
46 |
| -(defmethod suffix :token |
47 |
| - [zloc suffix] |
48 |
| - (let [v (second (z/node zloc)) |
49 |
| - v1 (cond (string? v) (str v suffix) |
50 |
| - (symbol? v) (symbol (namespace v) (str (name v) suffix)) |
51 |
| - (keyword? v) (keyword (namespace v) (str (name v) suffix)) |
52 |
| - :else (throw (Exception. (str "Cannot suffix token: " v))))] |
53 |
| - (z/replace zloc [:token v1]))) |
54 |
| - |
55 |
| -(defmethod suffix :multi-line |
56 |
| - [zloc suffix] |
57 |
| - (let [[v & rst] (rest (z/node zloc))] |
58 |
| - (z/replace zloc (vec (list* :multi-line (str v suffix) rst))))) |
| 4 | + (:require [fast-zip.core :as z]) |
| 5 | + (:import [fast_zip.core ZipperPath ZipperLocation])) |
| 6 | + |
| 7 | +(defn remove-right |
| 8 | + "Remove right sibling of the current node (if there is one)." |
| 9 | + [^ZipperLocation zloc] |
| 10 | + (let [path ^ZipperPath (.path zloc)] |
| 11 | + (if (zero? (count (.r path))) |
| 12 | + zloc |
| 13 | + (ZipperLocation. |
| 14 | + (.branch? zloc) |
| 15 | + (.children zloc) |
| 16 | + (.make-node zloc) |
| 17 | + (.node zloc) |
| 18 | + (assoc path :r (clojure.core/next (.r path)) :changed? true))))) |
| 19 | + |
| 20 | +(defn remove-left |
| 21 | + "Remove left sibling of the current node (if there is one)." |
| 22 | + [^ZipperLocation zloc] |
| 23 | + (let [path ^ZipperPath (.path zloc)] |
| 24 | + (if (zero? (count (.l path))) |
| 25 | + zloc |
| 26 | + (ZipperLocation. |
| 27 | + (.branch? zloc) |
| 28 | + (.children zloc) |
| 29 | + (.make-node zloc) |
| 30 | + (.node zloc) |
| 31 | + (assoc path :l (pop (.l path)) :changed? true))))) |
| 32 | + |
| 33 | +(defn remove-and-move-left |
| 34 | + "Remove current node and move left. If current node is at the leftmost |
| 35 | + location, returns `nil`." |
| 36 | + [^ZipperLocation zloc] |
| 37 | + (let [path ^ZipperPath (.path zloc)] |
| 38 | + (when (pos? (count (.l path))) |
| 39 | + (ZipperLocation. |
| 40 | + (.branch? zloc) |
| 41 | + (.children zloc) |
| 42 | + (.make-node zloc) |
| 43 | + (peek (.l path)) |
| 44 | + (assoc path :l (pop (.l path)) :changed? true))))) |
| 45 | + |
| 46 | +(defn remove-and-move-right |
| 47 | + "Remove current node and move right. If current node is at the rightmost |
| 48 | + location, returns `nil`." |
| 49 | + [^ZipperLocation zloc] |
| 50 | + (let [path ^ZipperPath (.path zloc)] |
| 51 | + (when (pos? (count (.r path))) |
| 52 | + (ZipperLocation. |
| 53 | + (.branch? zloc) |
| 54 | + (.children zloc) |
| 55 | + (.make-node zloc) |
| 56 | + (first (.r path)) |
| 57 | + (assoc path :r (clojure.core/next (.r path)) :changed? true))))) |
0 commit comments