br/indent adjustments

dev-srcloc
Matthew Butterick 8 years ago
parent 2f177edb5d
commit 093ac69a6a

@ -121,6 +121,14 @@
(check-equal? (line-start-visible t 2) 10) (check-equal? (line-start-visible t 2) 10)
(check-equal? (line-start-visible t 3) #f)) (check-equal? (line-start-visible t 3) #f))
(define/contract (line-first-visible-char text line)
((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c char? #f))
(char text (line-start-visible text line)))
(define/contract (line-last-visible-char text line)
((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (or/c char? #f))
(char text (line-end-visible text line)))
(define/contract (line-end-visible text line) (define/contract (line-end-visible text line)
((is-a?/c text%) (or/c exact-nonnegative-integer? #f) . -> . (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
@ -185,9 +193,10 @@
(send indented-t get-text)) (send indented-t get-text))
(define/contract (string-indents str) (define/contract (string-indents str)
(string? . -> . (listof exact-nonnegative-integer?)) (string? . -> . (listof (or/c exact-positive-integer? #f)))
(for/list ([line (in-list (string-split str "\n"))]) (for/list ([line (in-list (string-split str "\n"))])
(length (takef (string->list line) space-char?)))) (define len (length (takef (string->list line) space-char?)))
(and (exact-positive-integer? len) len)))
(module+ test (module+ test
(check-equal? (string-indents t-str) '(0 1 2))) (check-equal? (string-indents t-str) '(#f 1 2)))

@ -3,32 +3,33 @@
(provide indent-jsonic) (provide indent-jsonic)
(define indent-width 2) (define indent-width 2)
(define (left-bracket? c) (member c (list #\{ #\[)))
(define (left-bracket? c) (member c '(#\{ #\[))) (define (right-bracket? c) (member c (list #\} #\])))
(define (right-bracket? c) (member c '(#\} #\])))
;; 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/contract (indent-jsonic textbox [tbpos 0]) (define/contract (indent-jsonic tbox [posn 0])
((is-a?/c text%) exact-nonnegative-integer? . -> . ((is-a?/c text%) exact-nonnegative-integer? . -> .
(or/c exact-nonnegative-integer? #f)) (or/c exact-nonnegative-integer? #f))
(define this-line (line textbox tbpos)) (define prev-line (previous-line tbox posn))
(define prev-line (previous-line textbox tbpos)) (define current-line (line tbox posn))
(define prev-indent (or (line-indent textbox prev-line) 0)) (define prev-indent (or (line-indent tbox prev-line) 0))
(define this-indent (define current-indent
(cond (cond
[(left-bracket? (char textbox (line-start-visible textbox prev-line))) [(left-bracket?
(line-first-visible-char tbox prev-line))
(+ prev-indent indent-width)] (+ prev-indent indent-width)]
[(right-bracket? (char textbox (line-start-visible textbox this-line))) [(right-bracket?
(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? this-indent) this-indent)) (and (exact-positive-integer? current-indent) current-indent))
(module+ test (module+ test
(require rackunit) (require rackunit)
(define test-str #<<here (define test-str #<<HERE
#lang br/demo/jsonic #lang jsonic
{ {
"value", "value",
"string": "string":
@ -40,6 +41,9 @@
] ]
// "bar" // "bar"
} }
here HERE
) )
(display (apply-indenter indent-jsonic test-str))) (check-equal?
(string-indents (apply-indenter indent-jsonic test-str))
'(#f #f 2 2 2 4 6 6 4 2 2 #f)))

Loading…
Cancel
Save