/
remove.clj
47 lines (42 loc) · 1.51 KB
/
remove.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns ^:no-doc rewrite-clj.zip.remove
(:refer-clojure :exclude [remove])
(:require [rewrite-clj.zip
[move :as m]
[whitespace :as ws]]
[rewrite-clj.custom-zipper
[core :as z]
[utils :as u]]))
(defn- remove-trailing-space
"Remove all whitespace following a given node."
[zloc]
(u/remove-right-while zloc ws/whitespace?))
(defn- remove-preceding-space
"Remove all whitespace preceding a given node."
[zloc]
(u/remove-left-while zloc ws/whitespace?))
(defn remove
"Remove value at the given zipper location. Returns the first non-whitespace
node that would have preceded it in a depth-first walk. Will remove whitespace
appropriately.
[1 2 3] => [1 3]
[1 2] => [1]
[1 2] => [2]
[1] => []
[ 1 ] => []
[1 [2 3] 4] => [1 [2 3]]
[1 [2 3] 4] => [[2 3] 4]
If a node is located rightmost, both preceding and trailing spaces are removed,
otherwise only trailing spaces are touched. This means that a following element
(no matter whether on the same line or not) will end up in the same position
(line/column) as the removed one, _unless_ a comment lies between the original
node and the neighbour."
[zloc]
{:pre [zloc]
:post [%]}
(->> (-> (if (or (m/rightmost? zloc)
(m/leftmost? zloc))
(remove-preceding-space zloc)
zloc)
(remove-trailing-space)
z/remove)
(ws/skip-whitespace z/prev)))