-
Notifications
You must be signed in to change notification settings - Fork 0
/
intenter.rkt
48 lines (45 loc) · 1.13 KB
/
intenter.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#lang br
(require br/indent racket/contract racket/gui/base)
(define indent-width 2)
(define (left-bracket? c) (member c (list #\{ #\[)))
(define (right-bracket? c) (member c (list #\} #\])))
(define (indent-jsonic tbox [posn 0])
(define prev-line (previous-line tbox posn))
(define current-line (line tbox posn))
(cond
[(not prev-line) 0]
[else
(define prev-indent (or (line-indent tbox prev-line) 0))
(cond
[(left-bracket?
(line-first-visible-char tbox prev-line))
(+ prev-indent indent-width)]
[(right-bracket?
(line-first-visible-char tbox current-line))
(- prev-indent indent-width)]
[else prev-indent])]))
(provide
(contract-out
[indent-jsonic (((is-a?/c text%))
(exact-nonnegative-integer?) . ->* .
exact-nonnegative-integer?)]))
(module+ test
(require rackunit)
(define test-str #<<HERE
#lang jsonic
{
"value": 42,
"string":
[
{
"array": @$(range 5)$@,
"object": @$(hash 'k1 "valstring")$@
}
]
// "bar"
}
HERE
)
(check-equal?
(string-indents (apply-indenter indent-jsonic test-str))
'(0 0 2 2 2 4 6 6 4 2 2 0)))