improve indenter

dev-srcloc
Matthew Butterick 8 years ago
parent 3fca4ac779
commit 5e4ed4a89e

@ -1,24 +1,19 @@
#lang br #lang br
(require br/indent)
(provide indenter) (provide indenter)
(define indent-width 2) (define indent-width 2)
(define (indenter drr-editor start-pos) (define (indenter text start-pos)
(define line-first-pos ;; if line begins with }:
(for*/first ([pos (in-naturals start-pos)] ;; outdent to the matching {
[c (in-value (send drr-editor get-character pos))] ;; indent to match the previous line
#:when (not (char-blank? c))) (define the-line (line text start-pos))
pos)) (define line-last-pos (line-end text the-line))
(define line-last-pos
(send drr-editor find-newline 'forward line-first-pos))
(define open-braces (define open-braces
(for/sum ([pos (in-range 0 line-last-pos)]) (- (count-char text #\{ 0 line-last-pos)
(case (send drr-editor get-character pos) (count-char text #\} 0 line-last-pos)))
[(#\{) 1] (define line-first-pos (line-start text the-line))
[(#\}) -1] (define first-char (char text line-first-pos))
[else 0])))
(define first-char
(send drr-editor get-character line-first-pos))
(and (positive? open-braces) (and (positive? open-braces)
(* indent-width (* indent-width
(if (first-char . char=? . #\{) (if (first-char . char=? . #\{)

Loading…
Cancel
Save