improve contracts

dev-srcloc
Matthew Butterick 8 years ago
parent 4f3e6ff4fc
commit 2f177edb5d

@ -16,7 +16,7 @@
(define indent-width 2) (define indent-width 2)
(define/contract (char text pos) (define/contract (char text pos)
((is-a?/c text%) exact-nonnegative-integer? . -> . char?) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c char? #f))
(and pos (send text get-character pos))) (and pos (send text get-character pos)))
(module+ test (module+ test
@ -77,11 +77,11 @@
(check-equal? (next-line t 11) #f)) (check-equal? (next-line t 11) #f))
(define/contract (valid-line? text line) (define/contract (valid-line? text line)
((is-a?/c text%) exact-nonnegative-integer? . -> . boolean?) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . boolean?)
(and line (<= 0 line (send text last-line)))) (and line (<= 0 line (send text last-line))))
(define/contract (line-start text line) (define/contract (line-start text line)
((is-a?/c text%) exact-nonnegative-integer? . -> . (or/c exact-nonnegative-integer? #f)) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c exact-nonnegative-integer? #f))
(and (valid-line? text line) (and (valid-line? text line)
(send text line-start-position line))) (send text line-start-position line)))
@ -92,7 +92,7 @@
(check-equal? (line-start t 3) #f)) (check-equal? (line-start t 3) #f))
(define/contract (line-end text line) (define/contract (line-end text line)
((is-a?/c text%) exact-nonnegative-integer? . -> . (or/c exact-nonnegative-integer? #f)) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c exact-nonnegative-integer? #f))
(and (valid-line? text line) (and (valid-line? text line)
(send text line-end-position line))) (send text line-end-position line)))
@ -110,7 +110,7 @@
pos)) pos))
(define/contract (line-start-visible text line) (define/contract (line-start-visible text line)
((is-a?/c text%) exact-nonnegative-integer? . -> . (or/c exact-nonnegative-integer? #f)) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c exact-nonnegative-integer? #f))
(define start (line-start text line)) (define start (line-start text line))
(define end (line-end text line)) (define end (line-end text line))
(and start end (first-visible-char-pos text start end))) (and start end (first-visible-char-pos text start end)))
@ -122,7 +122,7 @@
(check-equal? (line-start-visible t 3) #f)) (check-equal? (line-start-visible t 3) #f))
(define/contract (line-end-visible text line) (define/contract (line-end-visible text line)
((is-a?/c text%) exact-nonnegative-integer? . -> . (or/c exact-nonnegative-integer? #f)) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c exact-nonnegative-integer? #f))
(define start+1 (line-end text line)) ; start before newline (define start+1 (line-end text line)) ; start before newline
(define end+1 (line-start text line)) (define end+1 (line-start text line))
(and start+1 end+1 (first-visible-char-pos text (sub1 start+1) (sub1 end+1)))) (and start+1 end+1 (first-visible-char-pos text (sub1 start+1) (sub1 end+1))))
@ -134,7 +134,7 @@
(check-equal? (line-end-visible t 3) #f)) (check-equal? (line-end-visible t 3) #f))
(define/contract (line-indent text line) (define/contract (line-indent text line)
((is-a?/c text%) exact-nonnegative-integer? . -> . (or/c exact-nonnegative-integer? #f)) ((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c exact-nonnegative-integer? #f))
(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

@ -1,5 +1,5 @@
#lang br #lang br
(require br/indent) (require br/indent racket/gui/base)
(provide indent-jsonic) (provide indent-jsonic)
(define indent-width 2) (define indent-width 2)
@ -10,9 +10,11 @@
;; if this line begins with } or ], outdent. ;; if this line begins with } or ], outdent.
;; if last line begins with { or [, indent. ;; if last line begins with { or [, indent.
;; otherwise use previous indent ;; otherwise use previous indent
(define (indent-jsonic textbox [pos 0]) (define/contract (indent-jsonic textbox [tbpos 0])
(define this-line (line textbox pos)) ((is-a?/c text%) exact-nonnegative-integer? . -> .
(define prev-line (previous-line textbox pos)) (or/c exact-nonnegative-integer? #f))
(define this-line (line textbox tbpos))
(define prev-line (previous-line textbox tbpos))
(define prev-indent (or (line-indent textbox prev-line) 0)) (define prev-indent (or (line-indent textbox prev-line) 0))
(define this-indent (define this-indent
(cond (cond
@ -28,14 +30,16 @@
(define test-str #<<here (define test-str #<<here
#lang br/demo/jsonic #lang br/demo/jsonic
{ {
"string": @$(string-append "foo" "bar")$@, "value",
"string":
[
{ {
"array": @$(range 5)$@, "array": @$(range 5)$@,
"object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@ "object": @$(hash 'k1 "valstring")$@
} }
// "bar" : ]
// "bar"
} }
here here
) )
(check-equal? (string-indents (apply-indenter indent-jsonic test-str)) (display (apply-indenter indent-jsonic test-str)))
(map (λ(x) (* x indent-width)) '(0 0 1 1 2 2 1 1 0))))

Loading…
Cancel
Save