fix indentation newline insertion

closes #41
This commit is contained in:
Robby Findler 2016-08-31 18:05:23 -05:00
parent 01c665e6e9
commit 0fae71ad0c

View File

@ -229,18 +229,27 @@
;; it chooses to break right after a {; the result accounts for that. ;; it chooses to break right after a {; the result accounts for that.
(define (break-paragraphs txt start-position end-position width) (define (break-paragraphs txt start-position end-position width)
(define δ 0) (define δ 0)
(define (break-line pos is-whitespace?)
(cond
[is-whitespace?
(send txt delete pos (+ pos 1))]
[else
(set! δ (+ δ 1))])
(send txt insert "\n" pos pos))
(let para-loop ([para (send txt position-paragraph start-position)] (let para-loop ([para (send txt position-paragraph start-position)]
[first-legal-pos-in-para start-position]) [first-legal-pos-in-para start-position])
(define para-start (send txt paragraph-start-position para)) (define para-start (send txt paragraph-start-position para))
(let char-loop ([pos (or first-legal-pos-in-para para-start)] (let char-loop ([pos (or first-legal-pos-in-para para-start)]
[previous-whitespace #f]) [previous-candidate #f]
[previous-candidate-is-whitespace? #f])
(cond (cond
[(and previous-whitespace (> (- pos para-start) width)) [(and previous-candidate (> (- pos para-start) width))
(send txt delete previous-whitespace (+ previous-whitespace 1)) (break-line previous-candidate previous-candidate-is-whitespace?)
(send txt insert "\n" previous-whitespace previous-whitespace)
(para-loop (+ para 1) #f)] (para-loop (+ para 1) #f)]
[(= pos end-position) [(= pos end-position)
(when (equal? previous-whitespace (- pos 1)) (when (equal? previous-candidate (- pos 1))
(send txt delete (- pos 1) pos))] (send txt delete (- pos 1) pos))]
[else [else
(define is-whitespace? (char-whitespace? (send txt get-character pos))) (define is-whitespace? (char-whitespace? (send txt get-character pos)))
@ -252,16 +261,16 @@
(equal? #\{ (send txt get-character (- pos 1))))))) (equal? #\{ (send txt get-character (- pos 1)))))))
(cond (cond
[(and linebreak-candidate? (> (- pos para-start) width)) [(and linebreak-candidate? (> (- pos para-start) width))
(cond (break-line pos is-whitespace?)
[is-whitespace?
(send txt delete pos (+ pos 1))]
[else
(set! δ (+ δ 1))])
(send txt insert "\n" pos pos)
(para-loop (+ para 1) #f)] (para-loop (+ para 1) #f)]
[else [else
(char-loop (+ pos 1) (char-loop (+ pos 1)
(if linebreak-candidate? pos previous-whitespace))])]))) (if linebreak-candidate?
pos
previous-candidate)
(if linebreak-candidate?
is-whitespace?
previous-candidate-is-whitespace?))])])))
(+ end-position δ)) (+ end-position δ))
;; the colorer classifies nearly all text as 'text but ;; the colorer classifies nearly all text as 'text but
@ -976,7 +985,18 @@
" kkk lll mmm nnn ooo ppp qqq\n" " kkk lll mmm nnn ooo ppp qqq\n"
" rrr sss ttt uuu vvv}}]\n")) " rrr sss ttt uuu vvv}}]\n"))
(check-equal? (let ([t (new racket:text%)])
(insert-them t
"#lang scribble/base\n"
"\n"
"jflkda fkfjdkla f fjdklsa @figure-ref{looping-constructs-sample}.\n")
(paragraph-indentation t 60 60)
(send t get-text))
(string-append
"#lang scribble/base\n"
"\n"
"jflkda fkfjdkla f fjdklsa @figure-ref{\n"
" looping-constructs-sample}.\n"))
(check-equal? (let ([t (new racket:text%)]) (check-equal? (let ([t (new racket:text%)])
(send t insert "#lang scribble/base\n\ntest1\n test2\n\t\ttest3\n") (send t insert "#lang scribble/base\n\ntest1\n test2\n\t\ttest3\n")