Skip to content

Commit ae84835

Browse files
authored
Merge copied strings in replacements (#495)
This makes logged replacements much easier to understand.
1 parent b544d84 commit ae84835

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

private/string-replacement.rkt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
[(list #false _) (values accumulated piece)]
104104
[(list (inserted-string s1) (inserted-string s2))
105105
(values accumulated (inserted-string (string-append s1 s2)))]
106+
[(list (copied-string start1 end1) (copied-string start2 end2))
107+
#:when (equal? end1 start2)
108+
(values accumulated (copied-string start1 end2))]
106109
[(list _ _) (values (cons previous accumulated) piece)])))
107110
(define new-span (transduce content-list (mapping replacement-string-span) #:into into-sum))
108111
(define max-end
@@ -134,9 +137,20 @@
134137
#:contents initial-pieces))
135138
(check-equal? (string-replacement-contents replacement) expected-pieces))
136139

137-
(test-case "should not merge copied pieces"
140+
(test-case "should merge adjacent copied pieces"
138141
(define initial-pieces
139142
(list (copied-string 2 5) (copied-string 5 7) (copied-string 7 9)))
143+
(define expected-pieces (list (copied-string 2 9)))
144+
(define replacement
145+
(string-replacement
146+
#:start 0
147+
#:end 10
148+
#:contents initial-pieces))
149+
(check-equal? (string-replacement-contents replacement) expected-pieces))
150+
151+
(test-case "should not merge non-adjacent copied pieces"
152+
(define initial-pieces
153+
(list (copied-string 2 5) (copied-string 6 8) (copied-string 10 12)))
140154
(define replacement
141155
(string-replacement
142156
#:start 0

0 commit comments

Comments
 (0)