|
72 | 72 | (define (paragraph-indentation txt pos width)
|
73 | 73 | (define pos-para (send txt position-paragraph pos))
|
74 | 74 |
|
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) |
79 | 76 |
|
80 | 77 | (define-values (start-position end-position)
|
81 | 78 | (find-paragraph-boundaries txt pos))
|
|
107 | 104 |
|
108 | 105 | (send txt end-edit-sequence)))
|
109 | 106 |
|
| 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 | + |
110 | 114 | (define (find-paragraph-boundaries txt insertion-pos)
|
111 | 115 |
|
112 | 116 | ;; move back one position in the case that
|
|
135 | 139 | (define containing-start (send txt find-up-sexp pos))
|
136 | 140 | (define pos-para (send txt position-paragraph pos))
|
137 | 141 | (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]))])] |
139 | 180 | [(= (send txt position-paragraph containing-start) pos-para)
|
140 | 181 | (loop containing-start)]
|
141 | 182 | [else
|
|
156 | 197 | ;; finds the spot we should stop at by looking at the sexp
|
157 | 198 | ;; structure of the program text.
|
158 | 199 | ;;
|
| 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 | + ;; |
159 | 205 | ;; #f means no limit
|
160 | 206 | (define-values (start-sexp-boundary end-sexp-boundary)
|
161 | 207 | (let ([first-container (send txt find-up-sexp pos)])
|
|
220 | 266 | (values start-position end-position)])]
|
221 | 267 | [else (values #f #f)]))
|
222 | 268 |
|
| 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 | + |
223 | 274 | (define (empty-para? txt para)
|
224 | 275 | (for/and ([x (in-range (send txt paragraph-start-position para)
|
225 | 276 | (send txt paragraph-end-position para))])
|
|
1052 | 1103 | "\n"
|
1053 | 1104 | "aa bb cc\n"))
|
1054 | 1105 |
|
| 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 | + |
1055 | 1160 | (check-equal? (let ([t (new racket:text%)])
|
1056 | 1161 | (send t insert "#lang scribble/base\n@a{b\n } \n")
|
1057 | 1162 | (determine-spaces t 26))
|
|
0 commit comments