Skip to content

Commit 3737b06

Browse files
author
Yannick Scherer
committed
Merge pull request #34 from eraserhd/whitespace_aware_insertion
fix insertion at leftmost/rightmost position.
2 parents d9f630c + a5015ee commit 3737b06

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/rewrite_clj/zip/insert.clj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
[move-fn insert-fn prefix zloc item]
1515
(let [item-node (node/coerce item)
1616
next-node (move-fn zloc)]
17-
(->> (if (or (not next-node) (ws/whitespace? next-node))
18-
(concat [item-node] prefix)
19-
(concat [space item-node] prefix))
17+
(->> (concat
18+
(if (and next-node (not (ws/whitespace? next-node)))
19+
[space])
20+
[item-node]
21+
(if (not (ws/whitespace? zloc))
22+
prefix))
2023
(reduce insert-fn zloc))))
2124

2225
(defn insert-right

test/rewrite_clj/zip/insert_test.clj

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,29 @@
1010
(fact "about whitespace-aware insertion."
1111
(let [elements (->> (base/of-string
1212
(format ?fmt "1 2 3 4"))
13-
(iterate m/next))
13+
(iterate ?m))
1414
loc (nth elements ?n)
1515
loc' (?f loc 'x)]
1616
(base/tag loc) => (base/tag loc')
1717
(base/root-string loc') => ?s))
18-
?fmt ?n ?f ?s
19-
"[%s]" 0 insert-right "[1 2 3 4] x"
20-
"[%s]" 1 insert-right "[1 x 2 3 4]"
21-
"[%s]" 2 insert-right "[1 2 x 3 4]"
22-
"[%s]" 3 insert-right "[1 2 3 x 4]"
23-
"[%s]" 4 insert-right "[1 2 3 4 x]"
24-
"[%s]" 0 insert-left "x [1 2 3 4]"
25-
"[%s]" 1 insert-left "[x 1 2 3 4]"
26-
"[%s]" 2 insert-left "[1 x 2 3 4]"
27-
"[%s]" 3 insert-left "[1 2 x 3 4]"
28-
"[%s]" 4 insert-left "[1 2 3 x 4]"
29-
"[%s]" 0 insert-child "[x 1 2 3 4]"
30-
"[%s]" 0 append-child "[1 2 3 4 x]"
31-
"[ %s]" 0 insert-child "[x 1 2 3 4]"
32-
"[%s ]" 0 append-child "[1 2 3 4 x]")
18+
?fmt ?m ?n ?f ?s
19+
"[%s]" m/next 0 insert-right "[1 2 3 4] x"
20+
"[%s]" m/next 1 insert-right "[1 x 2 3 4]"
21+
"[%s]" m/next 2 insert-right "[1 2 x 3 4]"
22+
"[%s]" m/next 3 insert-right "[1 2 3 x 4]"
23+
"[%s]" m/next 4 insert-right "[1 2 3 4 x]"
24+
"[%s]" m/next 0 insert-left "x [1 2 3 4]"
25+
"[%s]" m/next 1 insert-left "[x 1 2 3 4]"
26+
"[%s]" m/next 2 insert-left "[1 x 2 3 4]"
27+
"[%s]" m/next 3 insert-left "[1 2 x 3 4]"
28+
"[%s]" m/next 4 insert-left "[1 2 3 x 4]"
29+
"[%s]" m/next 0 insert-child "[x 1 2 3 4]"
30+
"[%s]" m/next 0 append-child "[1 2 3 4 x]"
31+
"[ %s]" m/next 0 insert-child "[x 1 2 3 4]"
32+
"[%s ]" m/next 0 append-child "[1 2 3 4 x]"
33+
"[%s]" z/next 2 insert-right "[1 x 2 3 4]"
34+
"\n[%s]" z/leftmost 1 insert-left "x\n[1 2 3 4]"
35+
"\n[%s]" z/leftmost 1 insert-right "\nx [1 2 3 4]")
3336

3437
(tabular
3538
(fact "about different node types that allow insertion."

0 commit comments

Comments
 (0)