|
|
@ -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)))
|
|
|
|
|
|
|
|
|
|
|
|