You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.1 KiB
Racket
38 lines
1.1 KiB
Racket
#lang at-exp br
|
|
(require br/indent)
|
|
(provide indent-jsonic)
|
|
|
|
(define indent-width 2)
|
|
|
|
(define (indent-jsonic tb [this-pos 0])
|
|
(define this-line (line tb this-pos))
|
|
(define prev-line (previous-line tb this-pos))
|
|
(define prev-indent (or (line-indent tb prev-line) 0))
|
|
(define this-indent
|
|
(cond
|
|
;; if this line begins with }, outdent.
|
|
[((char tb (line-start-visible tb this-line)) . char=? . #\})
|
|
(- prev-indent indent-width)]
|
|
;; if last line begins with {, indent.
|
|
[((char tb (line-start-visible tb prev-line)) . char=? . #\{)
|
|
(+ prev-indent indent-width)]
|
|
;; otherwise use previous indent
|
|
[else prev-indent]))
|
|
(and (exact-positive-integer? this-indent) this-indent))
|
|
|
|
(module+ test
|
|
(require rackunit)
|
|
(define test-str #<<here
|
|
#lang br/demo/jsonic
|
|
{
|
|
"string": @$(string-append "foo" "bar")$@,
|
|
{
|
|
"array": @$(range 5)$@,
|
|
"object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@
|
|
}
|
|
// "bar" :
|
|
}
|
|
here
|
|
)
|
|
(check-equal? (str->indents (test-indenter indent-jsonic test-str))
|
|
(map (λ(x) (* x indent-width)) '(0 0 1 1 2 2 1 1 0)))) |