|
|
@ -1,37 +1,28 @@
|
|
|
|
#lang at-exp br
|
|
|
|
#lang at-exp br
|
|
|
|
(require br/indent sugar/debug)
|
|
|
|
(require br/indent)
|
|
|
|
(provide indenter)
|
|
|
|
(provide indenter)
|
|
|
|
(define indent-width 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (indenter text [this-pos 0])
|
|
|
|
(define indent-width 2)
|
|
|
|
;; if line begins with }:
|
|
|
|
|
|
|
|
;; outdent to the matching {
|
|
|
|
|
|
|
|
;; indent to match the previous line
|
|
|
|
|
|
|
|
(define this-line (line text this-pos))
|
|
|
|
|
|
|
|
(define this-line-end (line-end text this-line))
|
|
|
|
|
|
|
|
(define open-braces
|
|
|
|
|
|
|
|
(- (count-char text #\{ 0 this-line-end)
|
|
|
|
|
|
|
|
(count-char text #\} 0 this-line-end)))
|
|
|
|
|
|
|
|
(and (positive? open-braces)
|
|
|
|
|
|
|
|
(* indent-width
|
|
|
|
|
|
|
|
(if ((char text (line-start-visible text this-line)) . char=? . #\{)
|
|
|
|
|
|
|
|
(sub1 open-braces)
|
|
|
|
|
|
|
|
open-braces))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#|
|
|
|
|
(define (indenter tb [this-pos 0])
|
|
|
|
(define prev-line (previous-line text this-line))
|
|
|
|
(define this-line (line tb this-pos))
|
|
|
|
(define prev-indent (line-indent text prev-line))
|
|
|
|
(define prev-line (previous-line tb this-pos))
|
|
|
|
|
|
|
|
(define prev-indent (or (line-indent tb prev-line) 0))
|
|
|
|
|
|
|
|
(define this-indent
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
[((char text (line-start text this-line)) . char=? . #\})
|
|
|
|
;; if this line begins with }, outdent.
|
|
|
|
(and prev-indent (- prev-indent indent-width))]
|
|
|
|
[((char tb (line-start-visible tb this-line)) . char=? . #\})
|
|
|
|
[((char text (line-start text prev-line)) . char=? . #\{)
|
|
|
|
(- prev-indent indent-width)]
|
|
|
|
(+ (or prev-indent 0) 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]))
|
|
|
|
[else prev-indent]))
|
|
|
|
|#
|
|
|
|
(and (exact-positive-integer? this-indent) this-indent))
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(module+ test
|
|
|
|
(require rackunit)
|
|
|
|
(require rackunit)
|
|
|
|
(define test-str @string-append|{
|
|
|
|
(define test-str #<<here
|
|
|
|
#lang br/demo/jsonic
|
|
|
|
#lang br/demo/jsonic
|
|
|
|
{
|
|
|
|
{
|
|
|
|
"string": @$(string-append "foo" "bar")$@,
|
|
|
|
"string": @$(string-append "foo" "bar")$@,
|
|
|
@ -40,11 +31,9 @@
|
|
|
|
"object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@
|
|
|
|
"object": @$(hash "k1" "valstring" (format "~a" 42) (hash "k1" (range 10) "k2" 42))$@
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// "bar" :
|
|
|
|
// "bar" :
|
|
|
|
}}|)
|
|
|
|
}
|
|
|
|
(displayln test-str)
|
|
|
|
here
|
|
|
|
(define indented-str (test-indenter indenter test-str))
|
|
|
|
)
|
|
|
|
(displayln indented-str)
|
|
|
|
(displayln (test-indenter indenter test-str))
|
|
|
|
(define indented-str2 (test-indenter indenter indented-str))
|
|
|
|
(check-equal? (str->indents (test-indenter indenter test-str))
|
|
|
|
(displayln indented-str2)
|
|
|
|
(map (λ(x) (* x indent-width)) '(0 0 1 1 2 2 1 1 0))))
|
|
|
|
(define indented-str3 (test-indenter indenter indented-str2))
|
|
|
|
|
|
|
|
(displayln indented-str3))
|
|
|
|
|