From d656b4cf7aed2d8264f5673ad39e39fce45cff8a Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sun, 9 Oct 2016 13:53:43 -0700 Subject: [PATCH] improve indenter --- .../br/demo/jsonic/drracket-indenter.rkt | 30 +++++++++++++++---- .../br/demo/jsonic/jsonic-test.rkt | 18 +++++------ beautiful-racket/br/demo/jsonic/main.rkt | 8 +++++ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt b/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt index 883a8e1..33d454a 100644 --- a/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt +++ b/beautiful-racket/br/demo/jsonic/drracket-indenter.rkt @@ -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)) \ No newline at end of file +(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)))) diff --git a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt index d6082e3..642ab99 100644 --- a/beautiful-racket/br/demo/jsonic/jsonic-test.rkt +++ b/beautiful-racket/br/demo/jsonic/jsonic-test.rkt @@ -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" : - } \ No newline at end of file +#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" : +} \ No newline at end of file diff --git a/beautiful-racket/br/demo/jsonic/main.rkt b/beautiful-racket/br/demo/jsonic/main.rkt index ec42c92..9c27dd4 100644 --- a/beautiful-racket/br/demo/jsonic/main.rkt +++ b/beautiful-racket/br/demo/jsonic/main.rkt @@ -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)))