From 2f177edb5d8984bb7d4f1a147de9d4553d81d111 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Tue, 20 Dec 2016 16:21:25 -0800 Subject: [PATCH] improve contracts --- beautiful-racket-lib/br/indent.rkt | 14 ++++++------ .../br/demo/jsonic-2/indenter.rkt | 22 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/beautiful-racket-lib/br/indent.rkt b/beautiful-racket-lib/br/indent.rkt index 54ae9a4..9415bb0 100644 --- a/beautiful-racket-lib/br/indent.rkt +++ b/beautiful-racket-lib/br/indent.rkt @@ -16,7 +16,7 @@ (define indent-width 2) (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))) (module+ test @@ -77,11 +77,11 @@ (check-equal? (next-line t 11) #f)) (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)))) (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) (send text line-start-position line))) @@ -92,7 +92,7 @@ (check-equal? (line-start t 3) #f)) (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) (send text line-end-position line))) @@ -110,7 +110,7 @@ pos)) (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 end (line-end text line)) (and start end (first-visible-char-pos text start end))) @@ -122,7 +122,7 @@ (check-equal? (line-start-visible t 3) #f)) (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 end+1 (line-start text line)) (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)) (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) (let ([lsv (line-start-visible text line)]) (and lsv ; could be #f diff --git a/beautiful-racket/br/demo/jsonic-2/indenter.rkt b/beautiful-racket/br/demo/jsonic-2/indenter.rkt index 38b75ed..426b2b6 100644 --- a/beautiful-racket/br/demo/jsonic-2/indenter.rkt +++ b/beautiful-racket/br/demo/jsonic-2/indenter.rkt @@ -1,5 +1,5 @@ #lang br -(require br/indent) +(require br/indent racket/gui/base) (provide indent-jsonic) (define indent-width 2) @@ -10,9 +10,11 @@ ;; if this line begins with } or ], outdent. ;; if last line begins with { or [, indent. ;; otherwise use previous indent -(define (indent-jsonic textbox [pos 0]) - (define this-line (line textbox pos)) - (define prev-line (previous-line textbox pos)) +(define/contract (indent-jsonic textbox [tbpos 0]) + ((is-a?/c text%) exact-nonnegative-integer? . -> . + (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 this-indent (cond @@ -28,14 +30,16 @@ (define test-str #<