-
Notifications
You must be signed in to change notification settings - Fork 7
/
raco.rkt
148 lines (135 loc) · 4.42 KB
/
raco.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
;; An entry point of raco fmt
#lang racket/base
; don't run this file for testing:
(module test racket/base)
(require racket/cmdline
racket/file
racket/match
racket/string
(rename-in "main.rkt"
[current-width :current-width]
[current-max-blank-lines :current-max-blank-lines]
[current-indent :current-indent]
[current-limit :current-limit]))
(define current-width
(make-parameter
(:current-width)
(λ (n)
(define as-num (string->number n))
(cond
[(and as-num (exact-nonnegative-integer? as-num))
as-num]
[else (raise-user-error
'width
"must be a natural number, given: ~a"
n)]))))
(define current-limit
(make-parameter
(:current-limit)
(λ (n)
(define as-num (string->number n))
(cond
[(and as-num (exact-nonnegative-integer? as-num))
as-num]
[else (raise-user-error
'width
"must be a natural number, given: ~a"
n)]))))
(define current-max-blank-lines
(make-parameter
(:current-max-blank-lines)
(λ (n)
(define as-num (string->number n))
(cond
[(and as-num (or (exact-nonnegative-integer? as-num) (= as-num +inf.0)))
as-num]
[else (raise-user-error
'max-blank-lines
"must be either a natural number or +inf.0, given: ~a"
n)]))))
(define current-indent
(make-parameter
(:current-indent)
(λ (n)
(define as-num (string->number n))
(cond
[(and as-num (exact-nonnegative-integer? as-num))
as-num]
[else (raise-user-error
'indent
"must be either a natural number, given: ~a"
n)]))))
(define current-config (make-parameter "-"))
(define current-in-place? (make-parameter #f))
(define filenames
(command-line
#:once-each
[("--width")
w
"page width limit -- must be a natural number (default: 102)"
(current-width w)]
[("--limit")
W
"computation width limit -- must be a natural number (default: 120)"
(current-limit W)]
[("--max-blank-lines")
n
"max consecutive blank lines -- must be a natural number or +inf.0 (default: 1)"
(current-max-blank-lines n)]
[("--indent")
n
"indentation level for subsequent lines -- must be a natural number (default: 0)"
(current-indent n)]
[("--config")
conf
"configuration file -- must be a path or - (.fmt.rkt if exists or else standard config) or -standard (standard config)"
(current-config conf)]
[("-i")
"Modifies the file in-place"
(current-in-place? #t)]
#:args args
args))
(define the-map
(case (current-config)
[("-") (cond
[(file-exists? ".fmt.rkt")
(dynamic-require ".fmt.rkt" 'the-formatter-map (λ () empty-formatter-map))]
[else empty-formatter-map])]
[("-standard") empty-formatter-map]
[else (dynamic-require (current-config) 'the-formatter-map (λ () empty-formatter-map))]))
(define (do-format-normal s)
(program-format s
#:formatter-map the-map
#:width (current-width)
#:limit (current-limit)
#:max-blank-lines (current-max-blank-lines)))
(define (do-format s)
;; A hack to preserve the first three lines that DrRacket hackily
;; recognizes and omits
;;
;; See https://github.com/sorawee/fmt/issues/39
(match (string-split s "\n")
[(list first-line second-line third-line rest-lines ...)
#:when (and (string-prefix? first-line ";;")
(string-prefix? second-line ";;")
(string-prefix? third-line "#reader"))
(string-join
(list first-line
second-line
third-line
(do-format-normal (string-join rest-lines "\n")))
"\n")]
[_ (do-format-normal s)]))
(match filenames
['() (displayln (do-format (string-join (for/list ([line (in-lines)]) line) "\n")))]
[_
(for ([filename (in-list filenames)])
;; use file->lines to handle CRLF on Windows
(define original (string-join (file->lines filename) "\n"))
(define out (do-format original))
(case (current-in-place?)
[(#f) (displayln out)]
[(#t) (unless (equal? original out)
(with-output-to-file filename
#:exists 'must-truncate
(λ () (displayln out))))]))])