Skip to content

Commit 4e00b3e

Browse files
committed
remove a rundant (and incorrect) computation and reuse the correct one
related to racket/drracket#177
1 parent c8c87f5 commit 4e00b3e

2 files changed

Lines changed: 77 additions & 27 deletions

File tree

gui-lib/framework/private/text-search.rkt

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,21 @@
266266
(queue-callback
267267
(λ ()
268268
(when searching-str
269-
(define start-pos (get-focus-editor-start-position))
270-
(define (how-many-to-add k)
271-
(if (search-result-compare <= (car k) start-pos) 1 0))
272-
(define count
273-
(+ (if to-replace-highlight
274-
(how-many-to-add to-replace-highlight)
275-
0)
276-
(for/sum ([(k v) (in-hash search-bubble-table)])
277-
(how-many-to-add k))))
278-
(update-before-caret-search-hit-count count))
269+
(update-before-caret-search-hit-count
270+
(calculate-before-caret-search-hit-count)))
279271
(set! search-position-callback-running? #f))
280272
#f)))
281273

274+
(define/private (calculate-before-caret-search-hit-count)
275+
(define start-pos (get-focus-editor-start-position))
276+
(define (how-many-to-add k)
277+
(if (search-result-compare <= (car k) start-pos) 1 0))
278+
(+ (if to-replace-highlight
279+
(how-many-to-add to-replace-highlight)
280+
0)
281+
(for/sum ([(k v) (in-hash search-bubble-table)])
282+
(how-many-to-add k))))
283+
282284
(define/private (get-focus-editor-start-position)
283285
(let loop ([txt this])
284286
(define focus (send txt get-focus-snip))
@@ -402,15 +404,14 @@
402404
(define new-search-bubbles '())
403405
(define new-replace-bubble #f)
404406
(define first-hit (do-search 0))
405-
406-
(define-values (this-search-hit-count this-before-caret-search-hit-count)
407+
408+
;; TODO (maybe-queue-search-position-update)
409+
(define this-search-hit-count
407410
(cond
408411
[first-hit
409412
(define sp (get-focus-editor-start-position))
410413
(let loop ([bubble-start first-hit]
411-
[search-hit-count 0]
412-
[before-caret-search-hit-count
413-
(if (search-result-compare < first-hit sp) 1 0)])
414+
[search-hit-count 0])
414415
(maybe-pause)
415416
(define bubble-end (search-result+ bubble-start (string-length searching-str)))
416417
(define bubble (cons bubble-start (string-length searching-str)))
@@ -427,23 +428,15 @@
427428

428429
(define next (do-search bubble-end))
429430

430-
(define next-before-caret-search-hit-count
431-
(if (and next (search-result-compare < next sp))
432-
(+ 1 before-caret-search-hit-count)
433-
before-caret-search-hit-count))
434431
(cond
435432
[next
436433
;; start a new one if there is another hit
437-
(loop next
438-
(+ search-hit-count 1)
439-
next-before-caret-search-hit-count)]
434+
(loop next (+ search-hit-count 1))]
440435
[else
441-
(values (+ search-hit-count 1)
442-
before-caret-search-hit-count)]))]
443-
[else (values 0 0)]))
436+
(+ search-hit-count 1)]))]
437+
[else 0]))
444438

445439
(set! search-hit-count this-search-hit-count)
446-
(set! before-caret-search-hit-count this-before-caret-search-hit-count)
447440

448441
(maybe-pause)
449442

@@ -460,7 +453,8 @@
460453
(highlight-hit search-bubble)])
461454
(maybe-pause))
462455

463-
(update-yellow)
456+
(update-yellow)
457+
(set! before-caret-search-hit-count (calculate-before-caret-search-hit-count))
464458
(end-edit-sequence)]
465459
[else
466460
(begin-edit-sequence #t #f)

gui-test/framework/tests/text.rkt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,62 @@
332332
(send t set-searching-state "b" #f #f)))
333333
(list 1 1))
334334

335+
(check-equal?
336+
(run-search-test
337+
(λ (t)
338+
(send t insert " a b a b a")
339+
(send t set-position 0 0)
340+
(send t set-searching-state "a" #f #f)))
341+
(list 0 3))
342+
343+
(check-equal?
344+
(run-search-test
345+
(λ (t)
346+
(send t insert " a b a b a")
347+
(send t set-position 1 1)
348+
(send t set-searching-state "a" #f #f)))
349+
(list 1 3))
350+
351+
(check-equal?
352+
(run-search-test
353+
(λ (t)
354+
(send t insert " a b a b a")
355+
(send t set-position 2 2)
356+
(send t set-searching-state "a" #f #f)))
357+
(list 1 3))
358+
359+
(check-equal?
360+
(run-search-test
361+
(λ (t)
362+
(send t insert " a b a b a")
363+
(send t set-position 3 3)
364+
(send t set-searching-state "a" #f #f)))
365+
(list 1 3))
366+
367+
(check-equal?
368+
(run-search-test
369+
(λ (t)
370+
(send t insert " a b a b a")
371+
(send t set-position 4 4)
372+
(send t set-searching-state "a" #f #f)))
373+
(list 1 3))
374+
375+
(check-equal?
376+
(run-search-test
377+
(λ (t)
378+
(send t insert " a b a b a")
379+
(send t set-position 5 5)
380+
(send t set-searching-state "a" #f #f)))
381+
(list 2 3))
382+
383+
(check-equal?
384+
(run-search-test
385+
(λ (t)
386+
(send t insert " a b a b a")
387+
(send t set-position 2 2)
388+
(send t set-searching-state "a" #f #f)))
389+
(list 1 3))
390+
335391
(check-equal?
336392
(run-search-test
337393
(λ (t)

0 commit comments

Comments
 (0)