-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rkt
115 lines (101 loc) · 3.45 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
;; The main file for fmt
#lang racket/base
(define-logger fmt)
(require racket/contract/base)
(provide
(contract-out
[program-format
(->* (string?)
(#:formatter-map formatter-map/c
#:source any/c
#:width (or/c exact-nonnegative-integer? +inf.0)
#:limit (or/c exact-nonnegative-integer? +inf.0)
#:max-blank-lines (or/c exact-nonnegative-integer? +inf.0)
#:indent exact-nonnegative-integer?)
(and/c string? immutable?))])
empty-formatter-map
compose-formatter-map
pretty-print*
pretty-format*
formatter-map/c
(all-from-out "core.rkt")
(all-from-out "params.rkt")
(all-from-out "common.rkt")
(all-from-out "conventions.rkt"))
(require racket/string
racket/contract
racket/format
racket/match
(except-in pretty-expressive flatten)
"common.rkt"
"core.rkt"
"read.rkt"
"realign.rkt"
"params.rkt"
"conventions.rkt")
(define formatter-map/c (-> (or/c string? #f) (or/c procedure? #f)))
(define (program-format program-source
#:formatter-map [formatter-map empty-formatter-map]
#:source [source #f]
#:width [width (current-width)]
#:limit [limit (current-limit)]
#:max-blank-lines [max-blank-lines (current-max-blank-lines)]
#:indent [indent (current-indent)])
(define doc (realign (read-all program-source source max-blank-lines)))
(match-define-values [(list s (info tainted? _)) _ real _]
(time-apply
(λ ()
(pretty-format/factory/info
(pretty-doc doc (compose-formatter-map formatter-map standard-formatter-map))
(cost-factory
(match-lambda**
[((list b1 h1 c1) (list b2 h2 c2))
(cond
[(= b1 b2)
(cond
[(= h1 h2) (<= c1 c2)]
[else (< h1 h2)])]
[else (< b1 b2)])])
(match-lambda**
[((list b1 h1 c1) (list b2 h2 c2))
(list (+ b1 b2) (+ h1 h2) (+ c1 c2))])
(λ (c l)
(define stop (+ c l))
(cond
[(> stop width)
(define start (max width c))
(define a (- start width))
(define b (- stop start))
(list (* b (+ (* 2 a) b)) 0 0)]
[else (list 0 0 0)]))
(λ (i) (list 0 1 0))
limit)
#:offset indent))
'()))
(define all-lines (string-split s "\n"))
(log-message
fmt-logger
'debug
'fmt
(format "([duration ~a] [lines ~a] [tainted? ~a])"
(exact->inexact (/ real 1000))
(length all-lines)
(if tainted? "true" "false"))
#f
#f)
(string->immutable-string
(string-join (for/list ([line (in-list all-lines)])
(string-trim line #:left? #f))
"\n")))
(define ((compose-formatter-map . fs) x)
(for/or ([f (in-list fs)])
(f x)))
(define (empty-formatter-map _x) #f)
(define (pretty-format* x
#:width [width (current-width)]
#:formatter-map [formatter-map empty-formatter-map])
(program-format (~s x) #:formatter-map formatter-map #:width width))
(define (pretty-print* x
#:width [width (current-width)]
#:formatter-map [formatter-map empty-formatter-map])
(display (pretty-format* x #:formatter-map formatter-map #:width width)))