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 prev-line (previous-line tbox posn))
(define current-line (line tbox posn))
(define prev-indent (or (line-indent tbox prev-line) 0))
(define current-indent
(cond
[(left-bracket?
(line-first-visible-char tbox prev-line))
(+ prev-indent indent-width)]
[(right-bracket?
(line-first-visible-char tbox current-line))
(- prev-indent indent-width)]
[else prev-indent]))
(and (exact-positive-integer? current-indent)
current-indent))
(define prev-indent (line-indent tbox prev-line))
(cond
[(left-bracket?
(line-first-visible-char tbox prev-line))
(+ prev-indent indent-width)]
[(right-bracket?
(line-first-visible-char tbox current-line))
(- prev-indent indent-width)]
[else prev-indent]))
(provide
(contract-out
[indent-jsonic (((is-a?/c text%))
(exact-nonnegative-integer?) . ->* .
(or/c exact-positive-integer? #f))]))
exact-nonnegative-integer?)]))
(module+ test
(require rackunit)
@ -45,4 +42,4 @@ HERE
)
(check-equal?
(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 prev-line (previous-line tbox posn))
(define current-line (line tbox posn))
(define prev-indent (or (line-indent tbox prev-line) 0))
(define current-indent
(cond
[(left-bracket?
(line-first-visible-char tbox prev-line))
(+ prev-indent indent-width)]
[(right-bracket?
(line-first-visible-char tbox current-line))
(- prev-indent indent-width)]
[else prev-indent]))
(and (exact-positive-integer? current-indent) current-indent))
(define prev-indent (line-indent tbox prev-line))
(cond
[(left-bracket?
(line-first-visible-char tbox prev-line))
(+ prev-indent indent-width)]
[(right-bracket?
(line-first-visible-char tbox current-line))
(- prev-indent indent-width)]
[else prev-indent]))
(provide
(contract-out
[indent-jsonic (((is-a?/c text%))
(exact-nonnegative-integer?) . ->* .
(or/c exact-nonnegative-integer? #f))]))
exact-nonnegative-integer?)]))
(module+ test
(require rackunit)
@ -44,5 +42,4 @@ HERE
)
(check-equal?
(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)
(let ([lsv (line-start-visible text line)])
(and lsv ; could be #f
(let ([li (- (line-start-visible text line) (line-start text line))])
(and (positive? li) li))))))
(- (line-start-visible text line) (line-start text line))))))
(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 2) 2)
(check-equal? (line-indent t 3) #f))
@ -198,10 +197,9 @@
(send indented-t get-text))
(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"))])
(define len (length (takef (string->list line) space-char?)))
(and (exact-positive-integer? len) len)))
(length (takef (string->list line) space-char?))))
(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%)]
[line-idx (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
[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.
}

Loading…
Cancel
Save