improve indenter

dev-srcloc
Matthew Butterick 8 years ago
parent 5b8d5ec2cb
commit d656b4cf7a

@ -2,8 +2,28 @@
(require racket/class describe)
(provide drracket-indenter)
(define (drracket-indenter txt pos)
(define i (char->integer (send txt get-character pos)))
(if (zero? i)
#f
i))
(define open-braces #f)
(define indent-width 2)
(define (drracket-indenter txt start-pos)
(define fresh-indent? (zero? start-pos))
(when fresh-indent? (set! open-braces 0))
(define first-pos-in-this-line
(for*/first ([pos (in-naturals start-pos)]
[c (in-value (send txt get-character pos))]
#:when (not (char-blank? c)))
pos))
(define last-pos-in-this-line
(send txt find-newline 'forward first-pos-in-this-line))
(set! open-braces
(+ open-braces
(for/sum ([pos (in-range first-pos-in-this-line last-pos-in-this-line)])
(case (send txt get-character pos)
[(#\{) 1]
[(#\}) -1]
[else 0]))))
(and (positive? open-braces)
(* indent-width
(if ((send txt get-character first-pos-in-this-line) . char=? . #\{)
(sub1 open-braces)
open-braces))))

@ -1,9 +1,9 @@
#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" :
}
#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" :
}

@ -1,6 +1,14 @@
#lang at-exp br/quicklang
(require "parser.rkt")
#|
Demonstrate:
+ color lexing
+ indentation
+ toolbar buttons
+ pinpoint errors
|#
(module+ reader
(define (read-syntax path port)
(define parse-tree (parse path (tokenize port)))

Loading…
Cancel
Save