correct contract on indenter

v6.3-exception
Matthew Butterick 5 years ago
parent bac7990c43
commit 1d2b75456e

@ -8,23 +8,20 @@
(define (indent-jsonic tbox [posn 0]) (define (indent-jsonic tbox [posn 0])
(define prev-line (previous-line tbox posn)) (define prev-line (previous-line tbox posn))
(define current-line (line tbox posn)) (define current-line (line tbox posn))
(define prev-indent (or (line-indent tbox prev-line) 0)) (define prev-indent (line-indent tbox prev-line))
(define current-indent (cond
(cond [(left-bracket?
[(left-bracket? (line-first-visible-char tbox prev-line))
(line-first-visible-char tbox prev-line)) (+ prev-indent indent-width)]
(+ prev-indent indent-width)] [(right-bracket?
[(right-bracket? (line-first-visible-char tbox current-line))
(line-first-visible-char tbox current-line)) (- prev-indent indent-width)]
(- prev-indent indent-width)] [else prev-indent]))
[else prev-indent]))
(and (exact-positive-integer? current-indent)
current-indent))
(provide (provide
(contract-out (contract-out
[indent-jsonic (((is-a?/c text%)) [indent-jsonic (((is-a?/c text%))
(exact-nonnegative-integer?) . ->* . (exact-nonnegative-integer?) . ->* .
(or/c exact-positive-integer? #f))])) exact-nonnegative-integer?)]))
(module+ test (module+ test
(require rackunit) (require rackunit)
@ -45,4 +42,4 @@ HERE
) )
(check-equal? (check-equal?
(string-indents (apply-indenter indent-jsonic test-str)) (string-indents (apply-indenter indent-jsonic test-str))
'(#f #f 2 2 2 4 6 6 4 2 2 #f))) '(0 0 2 2 2 4 6 6 4 2 2 0)))

@ -8,22 +8,20 @@
(define (indent-jsonic tbox [posn 0]) (define (indent-jsonic tbox [posn 0])
(define prev-line (previous-line tbox posn)) (define prev-line (previous-line tbox posn))
(define current-line (line tbox posn)) (define current-line (line tbox posn))
(define prev-indent (or (line-indent tbox prev-line) 0)) (define prev-indent (line-indent tbox prev-line))
(define current-indent (cond
(cond [(left-bracket?
[(left-bracket? (line-first-visible-char tbox prev-line))
(line-first-visible-char tbox prev-line)) (+ prev-indent indent-width)]
(+ prev-indent indent-width)] [(right-bracket?
[(right-bracket? (line-first-visible-char tbox current-line))
(line-first-visible-char tbox current-line)) (- prev-indent indent-width)]
(- prev-indent indent-width)] [else prev-indent]))
[else prev-indent]))
(and (exact-positive-integer? current-indent) current-indent))
(provide (provide
(contract-out (contract-out
[indent-jsonic (((is-a?/c text%)) [indent-jsonic (((is-a?/c text%))
(exact-nonnegative-integer?) . ->* . (exact-nonnegative-integer?) . ->* .
(or/c exact-nonnegative-integer? #f))])) exact-nonnegative-integer?)]))
(module+ test (module+ test
(require rackunit) (require rackunit)
@ -44,5 +42,4 @@ HERE
) )
(check-equal? (check-equal?
(string-indents (apply-indenter indent-jsonic test-str)) (string-indents (apply-indenter indent-jsonic test-str))
'(#f #f 2 2 2 4 6 6 4 2 2 #f))) '(0 0 2 2 2 4 6 6 4 2 2 0)))

@ -149,11 +149,10 @@
(and (valid-line? text line) (and (valid-line? text line)
(let ([lsv (line-start-visible text line)]) (let ([lsv (line-start-visible text line)])
(and lsv ; could be #f (and lsv ; could be #f
(let ([li (- (line-start-visible text line) (line-start text line))]) (- (line-start-visible text line) (line-start text line))))))
(and (positive? li) li))))))
(module+ test (module+ test
(check-equal? (line-indent t 0) #f) (check-equal? (line-indent t 0) 0)
(check-equal? (line-indent t 1) 1) (check-equal? (line-indent t 1) 1)
(check-equal? (line-indent t 2) 2) (check-equal? (line-indent t 2) 2)
(check-equal? (line-indent t 3) #f)) (check-equal? (line-indent t 3) #f))
@ -198,10 +197,9 @@
(send indented-t get-text)) (send indented-t get-text))
(define/contract (string-indents str) (define/contract (string-indents str)
(string? . -> . (listof (or/c exact-positive-integer? #f))) (string? . -> . (listof exact-nonnegative-integer?))
(for/list ([line (in-list (string-split str "\n"))]) (for/list ([line (in-list (string-split str "\n"))])
(define len (length (takef (string->list line) space-char?))) (length (takef (string->list line) space-char?))))
(and (exact-positive-integer? len) len)))
(module+ test (module+ test
(check-equal? (string-indents t-str) '(#f 1 2))) (check-equal? (string-indents t-str) '(0 1 2)))

@ -620,7 +620,7 @@ Convenient notation for @racket[(char textbox (line-end-visible textbox line-idx
[textbox (is-a?/c text%)] [textbox (is-a?/c text%)]
[line-idx (or/c exact-nonnegative-integer? #f)]) [line-idx (or/c exact-nonnegative-integer? #f)])
(or/c exact-nonnegative-integer? #f)]{ (or/c exact-nonnegative-integer? #f)]{
Get the length of the indent of line @racket[line-idx] in @racket[textbox] (or @racket[#f] the line has no indent). Get the length of the indent of line @racket[line-idx] in @racket[textbox].
} }
@ -634,7 +634,7 @@ Apply @racket[indenter-proc] to the text in @racket[textbox-or-str] and return a
@defproc[(string-indents @defproc[(string-indents
[str string?]) [str string?])
(listof (or/c exact-positive-integer? #f))]{ (listof exact-nonnegative-integer?)]{
Lists the indents at the beginning of each line in @racket[str]. Useful for unit testing. Lists the indents at the beginning of each line in @racket[str]. Useful for unit testing.
} }

Loading…
Cancel
Save