Skip to content

Commit ab998c9

Browse files
committed
add all-string-snips<%> and all-string-snips-mixin
This is pulled out of DrRacket, mostly just to be able to add test cases
1 parent d9fd94f commit ab998c9

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed

gui-doc/scribblings/framework/text.scrbl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,33 @@
477477
}
478478
}
479479

480+
@definterface[text:all-string-snips<%> ()]{
481+
@defmethod[(all-string-snips?) boolean?]{
482+
Returns @racket[#t] if all of the snips in the @racket[text%] object
483+
are @racket[string-snip%]s.
484+
485+
This method usually returns quickly, tracking changes to the editor
486+
to update internal state. But if a non-@racket[string-snip%] is deleted,
487+
then the next call to @method[text:all-string-snips<%> all-string-snips?]
488+
traverses the entire content to search to see if there are other
489+
non-@racket[string-snip%]s.
490+
}
491+
}
492+
493+
@defmixin[text:all-string-snips-mixin (text%) (text:all-string-snips<%>)]{
494+
@defmethod[#:mode augment (on-insert [start exact-nonnegative-integer?]
495+
[len exact-nonnegative-integer?]) void?]{
496+
Checks to see if there were any non-@racket[string-snip%]s inserted
497+
in the given range and, if so, updates the internal state.
498+
}
499+
500+
@defmethod[#:mode augment (after-delete [start exact-nonnegative-integer?]
501+
[len exact-nonnegative-integer?]) void?]{
502+
Checks to see if there were any non-@racket[string-snip%]s deleted
503+
in the given range and, if so, updates the internal state.
504+
}
505+
}
506+
480507
@definterface[text:searching<%> (editor:keymap<%> text:basic<%>)]{
481508
Any object matching this interface can be searched.
482509

gui-lib/framework/private/sig.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@
188188
nbsp->space<%>
189189
column-guide<%>
190190
normalize-paste<%>
191+
all-string-snips<%>
191192
delegate<%>
192193
wide-snip<%>
193194
searching<%>
@@ -229,6 +230,7 @@
229230
nbsp->space-mixin
230231
column-guide-mixin
231232
normalize-paste-mixin
233+
all-string-snips-mixin
232234
wide-snip-mixin
233235
delegate-mixin
234236
searching-mixin

gui-lib/framework/private/text.rkt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,6 +4494,62 @@ designates the character that triggers autocompletion
44944494
(define-struct saved-dc-state (smoothing pen brush font text-foreground-color text-mode))
44954495
(define padding-dc (new bitmap-dc% [bitmap (make-screen-bitmap 1 1)]))
44964496

4497+
(define all-string-snips<%>
4498+
(interface ()
4499+
all-string-snips?))
4500+
4501+
(define all-string-snips-mixin
4502+
(mixin ((class->interface text%)) (all-string-snips<%>)
4503+
(inherit find-first-snip find-snip)
4504+
4505+
(define/private (all-string-snips?/slow)
4506+
(let loop ([s (find-first-snip)])
4507+
(cond
4508+
[(not s) #t]
4509+
[(is-a? s string-snip%) (loop (send s next))]
4510+
[else #f])))
4511+
4512+
(define/augment (after-insert start end)
4513+
(inner (void) after-insert start end)
4514+
4515+
(when (equal? all-string-snips-state #t)
4516+
(let loop ([s (find-snip start 'after-or-none)]
4517+
[i start])
4518+
(cond
4519+
[(not s) (void)]
4520+
[(not (< i end)) (void)]
4521+
[(is-a? s string-snip%)
4522+
(define size (send s get-count))
4523+
(loop (send s next) (+ i size))]
4524+
[else (set! all-string-snips-state #f)]))))
4525+
4526+
(define/augment (on-delete start end)
4527+
(inner (void) on-delete start end)
4528+
(when (equal? all-string-snips-state #f)
4529+
(let loop ([s (find-snip start 'after-or-none)]
4530+
[i start])
4531+
(cond
4532+
[(not s) (void)]
4533+
[(not (< i end)) (void)]
4534+
[(is-a? s string-snip%)
4535+
(define size (send s get-count))
4536+
(loop (send s next) (+ i size))]
4537+
[else (set! all-string-snips-state 'dont-know)]))))
4538+
4539+
4540+
;; (or/c #t #f 'dont-know)
4541+
(define all-string-snips-state #t)
4542+
(define/public (all-string-snips?)
4543+
(cond
4544+
[(boolean? all-string-snips-state)
4545+
all-string-snips-state]
4546+
[else
4547+
(define all-string-snips? (all-string-snips?/slow))
4548+
(set! all-string-snips-state all-string-snips?)
4549+
all-string-snips?]))
4550+
4551+
(super-new)))
4552+
44974553
(define basic% (basic-mixin (editor:basic-mixin text%)))
44984554
(define line-spacing% (line-spacing-mixin basic%))
44994555
(define hide-caret/selection% (hide-caret/selection-mixin line-spacing%))

gui-lib/info.rkt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030

3131
(define pkg-authors '(mflatt robby))
3232

33-
(define version "1.10")
33+
(define version "1.11")

gui-test/framework/tests/text.rkt

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,98 @@
252252
(and (false? pos) (is-a? edit pasteboard%)))))))
253253

254254

255+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256+
;;
257+
;; all-string-snips<%>
258+
;;
259+
260+
(test
261+
'all-string-snips<%>.1
262+
(λ (x) (equal? x #t))
263+
(λ ()
264+
(queue-sexp-to-mred
265+
'(let ()
266+
(define t (new (text:all-string-snips-mixin text%)))
267+
(send t all-string-snips?)))))
268+
269+
(test
270+
'all-string-snips<%>.2
271+
(λ (x) (equal? x #t))
272+
(λ ()
273+
(queue-sexp-to-mred
274+
'(let ()
275+
(define t (new (text:all-string-snips-mixin text%)))
276+
(send t insert "xx")
277+
(send t all-string-snips?)))))
278+
279+
(test
280+
'all-string-snips<%>.3
281+
(λ (x) (equal? x #t))
282+
(λ ()
283+
(queue-sexp-to-mred
284+
'(let ()
285+
(define t (new (text:all-string-snips-mixin text%)))
286+
(send t insert "xx")
287+
(send t delete 0 1)
288+
(send t all-string-snips?)))))
289+
290+
(test
291+
'all-string-snips<%>.4
292+
(λ (x) (equal? x #t))
293+
(λ ()
294+
(queue-sexp-to-mred
295+
'(let ()
296+
(define t (new (text:all-string-snips-mixin text%)))
297+
(send t insert "xx")
298+
(send t delete 0 2)
299+
(send t all-string-snips?)))))
300+
301+
(test
302+
'all-string-snips<%>.5
303+
(λ (x) (equal? x #f))
304+
(λ ()
305+
(queue-sexp-to-mred
306+
'(let ()
307+
(define t (new (text:all-string-snips-mixin text%)))
308+
(send t insert (new snip%))
309+
(send t all-string-snips?)))))
310+
311+
(test
312+
'all-string-snips<%>.6
313+
(λ (x) (equal? x #t))
314+
(λ ()
315+
(queue-sexp-to-mred
316+
'(let ()
317+
(define t (new (text:all-string-snips-mixin text%)))
318+
(send t insert (new snip%))
319+
(send t delete 0 1)
320+
(send t all-string-snips?)))))
321+
322+
(test
323+
'all-string-snips<%>.7
324+
(λ (x) (equal? x #f))
325+
(λ ()
326+
(queue-sexp-to-mred
327+
'(let ()
328+
(define t (new (text:all-string-snips-mixin text%)))
329+
(send t insert (new snip%))
330+
(send t insert (new snip%))
331+
(send t delete 0 1)
332+
(send t all-string-snips?)))))
333+
334+
(test
335+
'all-string-snips<%>.8
336+
(λ (x) (equal? x #f))
337+
(λ ()
338+
(queue-sexp-to-mred
339+
'(let ()
340+
(define t (new (text:all-string-snips-mixin text%)))
341+
(send t insert (new snip%))
342+
(send t insert "abcdef")
343+
(send t insert (new snip%))
344+
(send t delete 2 4)
345+
(send t all-string-snips?)))))
346+
255347
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
256348
;;
257349
;; print-to-dc

0 commit comments

Comments
 (0)