Skip to content

Commit 5e433c8

Browse files
committed
fix search for containing text region
closes #42 closes #43
1 parent 0fae71a commit 5e433c8

File tree

1 file changed

+110
-5
lines changed

1 file changed

+110
-5
lines changed

gui-lib/scribble/private/indentation.rkt

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@
7272
(define (paragraph-indentation txt pos width)
7373
(define pos-para (send txt position-paragraph pos))
7474

75-
;; do this to ensure the colorer is sync'd, since
76-
;; classify position returns bogus results if it isn't
77-
(let ([bw (send txt backward-containing-sexp pos 0)])
78-
(send txt forward-match (or bw pos) (send txt last-position)))
75+
(wait-for-colorer txt pos)
7976

8077
(define-values (start-position end-position)
8178
(find-paragraph-boundaries txt pos))
@@ -107,6 +104,13 @@
107104

108105
(send txt end-edit-sequence)))
109106

107+
;; do this to ensure the colorer is sync'd, since
108+
;; classify position returns bogus results if it isn't
109+
;; this should ensure the colorer is sync up to `pos`
110+
(define (wait-for-colorer txt pos)
111+
(let ([bw (send txt backward-containing-sexp pos 0)])
112+
(send txt forward-match (or bw pos) (send txt last-position))))
113+
110114
(define (find-paragraph-boundaries txt insertion-pos)
111115

112116
;; move back one position in the case that
@@ -135,7 +139,44 @@
135139
(define containing-start (send txt find-up-sexp pos))
136140
(define pos-para (send txt position-paragraph pos))
137141
(cond
138-
[(not containing-start) #f]
142+
[(not containing-start)
143+
;; we know there is no sexp outside us here
144+
(wait-for-colorer txt (min (+ pos 1) (send txt last-position)))
145+
(cond
146+
[(and (= pos (send txt paragraph-end-position pos-para))
147+
(begin
148+
;; when we are the end of a paragraph, it might be "morally"
149+
;; text, but the colorerer can't tell us that without a
150+
;; character actually being there to classify
151+
;; so add one and check it
152+
(send txt insert " " pos pos)
153+
(wait-for-colorer txt pos)
154+
(begin0
155+
(is-text? txt pos)
156+
(send txt delete pos (+ pos 1)))))
157+
pos]
158+
[else
159+
;; at this point, we might be at the end of the word
160+
;; `hello` in something like `@hello[there]`
161+
;; so scan backwards to see if the first paren we find
162+
;; is an `@` and, in that case, go one before it and try again
163+
(let loop ([pos (if (is-open? txt pos)
164+
(- pos 1)
165+
pos)])
166+
(define cp (send txt classify-position pos))
167+
(cond
168+
[(not (= (send txt position-paragraph pos) pos-para))
169+
#f]
170+
[(member cp '(symbol keyword))
171+
(if (= pos 0)
172+
#f
173+
(loop (- pos 1)))]
174+
[(equal? cp 'parenthesis)
175+
(if (and (> pos 0)
176+
(is-text? txt (- pos 1)))
177+
(- pos 1)
178+
#f)]
179+
[else #f]))])]
139180
[(= (send txt position-paragraph containing-start) pos-para)
140181
(loop containing-start)]
141182
[else
@@ -156,6 +197,11 @@
156197
;; finds the spot we should stop at by looking at the sexp
157198
;; structure of the program text.
158199
;;
200+
;; we are looking for an enclosing sexp that's in "racket"
201+
;; mode, i.e. something like @f[(f x @emph{no, really!})]
202+
;; if we start inside the `no really!` part, then we don't
203+
;; want to reflow past that call to `f`.
204+
;;
159205
;; #f means no limit
160206
(define-values (start-sexp-boundary end-sexp-boundary)
161207
(let ([first-container (send txt find-up-sexp pos)])
@@ -220,6 +266,11 @@
220266
(values start-position end-position)])]
221267
[else (values #f #f)]))
222268

269+
(define (is-open? txt pos)
270+
(define cp (send txt classify-position pos))
271+
(and (equal? cp 'parenthesis)
272+
(member (send txt get-character pos) '(#\( #\{ #\[))))
273+
223274
(define (empty-para? txt para)
224275
(for/and ([x (in-range (send txt paragraph-start-position para)
225276
(send txt paragraph-end-position para))])
@@ -1052,6 +1103,60 @@
10521103
"\n"
10531104
"aa bb cc\n"))
10541105

1106+
(let ([t (new racket:text%)])
1107+
(insert-them
1108+
t
1109+
"#lang scribble/manual\n"
1110+
"\n"
1111+
(string-append
1112+
"Abcd abcd abcd abcd abcd @racket[hello] abcd abcd abcd abcd. Abcd abcd abcd abcd abcd"
1113+
" abcd abcd abcd abcd abcd abcd abcd.\n"))
1114+
(paragraph-indentation t 57 60)
1115+
(check-equal? (send t get-text)
1116+
(string-append
1117+
"#lang scribble/manual\n"
1118+
"\n"
1119+
"Abcd abcd abcd abcd abcd @racket[hello] abcd abcd abcd abcd.\n"
1120+
"Abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd.\n")))
1121+
1122+
(let ([t (new racket:text%)])
1123+
(insert-them
1124+
t
1125+
"#lang scribble/manual\n"
1126+
"\n"
1127+
"@emph{\n"
1128+
" @emph{\n"
1129+
" @emph{\n"
1130+
" @emph{\n"
1131+
" @emph{\n"
1132+
" blah blah blah blah blah blah blah blah blah blah blah}}}}}")
1133+
(paragraph-indentation t (send t last-position) 60)
1134+
(check-equal? (send t get-text)
1135+
(string-append
1136+
"#lang scribble/manual\n"
1137+
"\n"
1138+
"@emph{ @emph{ @emph{ @emph{ @emph{ blah blah blah blah blah\n"
1139+
" blah blah blah blah blah blah}}}}}")))
1140+
1141+
(let ([t (new racket:text%)])
1142+
(insert-them
1143+
t
1144+
"#lang scribble/manual\n"
1145+
"\n"
1146+
"@emph{\n"
1147+
" @emph{\n"
1148+
" @emph{\n"
1149+
" @emph{\n"
1150+
" @emph{\n"
1151+
" blah blah blah blah blah blah blah blah blah blah blah}}}}}")
1152+
(paragraph-indentation t (- (send t last-position) 2) 60)
1153+
(check-equal? (send t get-text)
1154+
(string-append
1155+
"#lang scribble/manual\n"
1156+
"\n"
1157+
"@emph{ @emph{ @emph{ @emph{ @emph{ blah blah blah blah blah\n"
1158+
" blah blah blah blah blah blah}}}}}")))
1159+
10551160
(check-equal? (let ([t (new racket:text%)])
10561161
(send t insert "#lang scribble/base\n@a{b\n } \n")
10571162
(determine-spaces t 26))

0 commit comments

Comments
 (0)