Skip to content

Commit 58a0267

Browse files
resyntax-ci[bot]mflatt
authored andcommitted
Automated Resyntax fixes
This is an automated change generated by Resyntax. #### Pass 1 Applied 9 fixes to [`scribble-text-lib/scribble/text/output.rkt`](../blob/HEAD/scribble-text-lib/scribble/text/output.rkt) * Line 2, `tidy-require`: Keep imports in `require` sorted and grouped by phase, with collections before files. * Line 50, `let-to-define`: Internal definitions are recommended instead of `let` expressions, to reduce nesting. * Line 76, `if-begin-to-cond`: Using `cond` instead of `if` here makes `begin` unnecessary * Line 89, `if-let-to-cond`: `cond` with internal definitions is preferred over `if` with `let`, to reduce nesting * Line 123, `if-begin-to-cond`: Using `cond` instead of `if` here makes `begin` unnecessary * Line 148, `if-begin-to-cond`: Using `cond` instead of `if` here makes `begin` unnecessary * Line 153, `if-begin-to-cond`: Using `cond` instead of `if` here makes `begin` unnecessary * Line 246, `if-let-to-cond`: `cond` with internal definitions is preferred over `if` with `let`, to reduce nesting * Line 295, `if-let-to-cond`: `cond` with internal definitions is preferred over `if` with `let`, to reduce nesting Applied 4 fixes to [`scribble-text-lib/scribble/text/syntax-utils.rkt`](../blob/HEAD/scribble-text-lib/scribble/text/syntax-utils.rkt) * Line 3, `tidy-require`: Keep imports in `require` sorted and grouped by phase, with collections before files. * Line 123, `if-let-to-cond`: `cond` with internal definitions is preferred over `if` with `let`, to reduce nesting * Line 161, `always-throwing-if-to-when`: Using `when` and `unless` is simpler than a conditional with an always-throwing branch. * Line 185, `tidy-require`: Keep imports in `require` sorted and grouped by phase, with collections before files. Applied 1 fix to [`scribble-text-lib/scribble/text/lang.rkt`](../blob/HEAD/scribble-text-lib/scribble/text/lang.rkt) * Line 3, `tidy-require`: Keep imports in `require` sorted and grouped by phase, with collections before files. Applied 1 fix to [`scribble-text-lib/scribble/text/main.rkt`](../blob/HEAD/scribble-text-lib/scribble/text/main.rkt) * Line 3, `tidy-require`: Keep imports in `require` sorted and grouped by phase, with collections before files. Applied 5 fixes to [`scribble-lib/scriblib/private/counter.rkt`](../blob/HEAD/scribble-lib/scriblib/private/counter.rkt) * Line 20, `quasiquote-to-list`: This quasiquotation is equialent to a simple `list` call. * Line 39, `let-to-define`: Internal definitions are recommended instead of `let` expressions, to reduce nesting. * Line 55, `let-to-define`: Internal definitions are recommended instead of `let` expressions, to reduce nesting. * Line 74, `map-to-for`: This `map` operation can be replaced with a `for/list` loop. * Line 87, `let-to-define`: Internal definitions are recommended instead of `let` expressions, to reduce nesting. ## Summary Fixed 20 issues in 5 files. * Fixed 5 occurrences of `tidy-require` * Fixed 4 occurrences of `if-let-to-cond` * Fixed 4 occurrences of `let-to-define` * Fixed 4 occurrences of `if-begin-to-cond` * Fixed 1 occurrence of `quasiquote-to-list` * Fixed 1 occurrence of `always-throwing-if-to-when` * Fixed 1 occurrence of `map-to-for`
1 parent 37ab227 commit 58a0267

File tree

5 files changed

+204
-158
lines changed

5 files changed

+204
-158
lines changed

scribble-lib/scriblib/private/counter.rkt

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
(define (tag->counter-tag counter tag . kind)
1919
(if (generated-tag? tag)
20-
`(,(string->symbol (format "counter-~a" kind)) ,tag)
20+
(list (string->symbol (format "counter-~a" kind)) tag)
2121
`(counter (,(counter-name counter) ,tag ,@kind))))
2222

2323
(define (counter-target counter tag label
@@ -36,27 +36,28 @@
3636
(list
3737
(make-delayed-element
3838
(lambda (renderer part ri)
39-
(let ([n (resolve-get part ri (tag->counter-tag counter tag "value"))])
40-
(cons
41-
(make-element label-style
42-
(let ([l (cons (make-element (default-figure-counter-style) (format "~a" n))
43-
(decode-content (list label-suffix)))])
44-
(if label
45-
(list* label 'nbsp l)
46-
l)))
47-
content)))
39+
(define n (resolve-get part ri (tag->counter-tag counter tag "value")))
40+
(cons (make-element label-style
41+
(let ([l (cons (make-element (default-figure-counter-style)
42+
(format "~a" n))
43+
(decode-content (list label-suffix)))])
44+
(if label
45+
(list* label 'nbsp l)
46+
l)))
47+
content))
4848
(lambda () (if label
4949
(list* label 'nbsp "N" content)
5050
(cons "N" content)))
5151
(lambda () (if label
5252
(list* label 'nbsp "N" content)
5353
(cons "N" content)))))
5454
(lambda (ci)
55-
(let ([n (if continue?
56-
(counter-n counter)
57-
(add1 (counter-n counter)))])
58-
(set-counter-n! counter n)
59-
(collect-put! ci (generate-tag (tag->counter-tag counter tag "value") ci) n)))))
55+
(define n
56+
(if continue?
57+
(counter-n counter)
58+
(add1 (counter-n counter))))
59+
(set-counter-n! counter n)
60+
(collect-put! ci (generate-tag (tag->counter-tag counter tag "value") ci) n))))
6061
(tag->counter-tag counter tag)))
6162
(if (counter-target-wrap counter)
6263
((counter-target-wrap counter)
@@ -71,45 +72,36 @@
7172
(define (t-encode s)
7273
(apply
7374
string-append
74-
(map (lambda (c)
75-
(cond
76-
[(and (or (char-alphabetic? c) (char-numeric? c))
77-
((char->integer c) . < . 128))
78-
(string c)]
79-
[(char=? c #\space) "_"]
80-
[else (format "x~x" (char->integer c))]))
81-
(string->list (format "~s" s)))))
75+
(for/list ([c (in-string (format "~s" s))])
76+
(cond
77+
[(and (or (char-alphabetic? c) (char-numeric? c)) ((char->integer c) . < . 128)) (string c)]
78+
[(char=? c #\space) "_"]
79+
[else (format "x~x" (char->integer c))]))))
8280

8381
(define (counter-ref counter tag label
8482
#:link-render-style [link-style #f])
8583
(make-delayed-element
8684
(lambda (renderer part ri)
87-
(let ([n (resolve-get part ri (tag->counter-tag counter tag "value"))])
88-
(let ([n (if (counter-ref-wrap counter)
89-
((counter-ref-wrap counter)
90-
(format "~a" n)
91-
;; Don't use this argument:
92-
(format "t:~a" (t-encode (list 'counter (list (counter-name counter) tag)))))
93-
(list (format "~a" n)))]
94-
[link-number-only? (eq? (link-render-style-mode
95-
(or link-style
96-
(current-link-render-style)))
97-
'number)])
98-
(cond
99-
[(and label link-number-only?)
100-
(make-element #f
101-
(list label 'nbsp
102-
(make-link-element
103-
#f
104-
(list n)
105-
(tag->counter-tag counter tag))))]
106-
[else
107-
(make-link-element
108-
#f
109-
(if label
110-
(list label 'nbsp n)
111-
n)
112-
(tag->counter-tag counter tag))]))))
85+
(define n (resolve-get part ri (tag->counter-tag counter tag "value")))
86+
(let ([n (if (counter-ref-wrap counter)
87+
((counter-ref-wrap counter)
88+
(format "~a" n)
89+
;; Don't use this argument:
90+
(format "t:~a" (t-encode (list 'counter (list (counter-name counter) tag)))))
91+
(list (format "~a" n)))]
92+
[link-number-only?
93+
(eq? (link-render-style-mode (or link-style (current-link-render-style))) 'number)])
94+
(cond
95+
[(and label link-number-only?)
96+
(make-element
97+
#f
98+
(list label 'nbsp (make-link-element #f (list n) (tag->counter-tag counter tag))))]
99+
[else
100+
(make-link-element #f
101+
(if label
102+
(list label 'nbsp n)
103+
n)
104+
(tag->counter-tag counter tag))])))
113105
(lambda () (if label
114106
(list label 'nbsp "N")
115107
(list "N")))

scribble-text-lib/scribble/text/lang.rkt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#lang racket/base
22

3-
(require "syntax-utils.rkt" "output.rkt"
4-
racket/promise racket/list racket/string)
3+
(require racket/list
4+
racket/promise
5+
racket/string
6+
"output.rkt"
7+
"syntax-utils.rkt")
58

69
(provide (except-out (all-from-out racket/base) #%module-begin)
710
(all-from-out "output.rkt" racket/promise racket/list racket/string)
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#lang racket/base
22

3-
(require "output.rkt" "syntax-utils.rkt"
4-
racket/promise racket/list racket/string)
3+
(require racket/list
4+
racket/promise
5+
racket/string
6+
"output.rkt"
7+
"syntax-utils.rkt")
58

69
(provide (all-from-out "output.rkt" racket/promise racket/list racket/string)
710
begin/text include/text)

scribble-text-lib/scribble/text/output.rkt

Lines changed: 103 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#lang racket/base
2-
(require racket/promise
3-
racket/contract/base)
2+
(require racket/contract/base
3+
racket/promise)
44

55
(provide
66
special?
@@ -47,7 +47,8 @@
4747
;; the low-level string output function (can change with `with-writer')
4848
(define write write-string)
4949
;; to get the output column
50-
(define (getcol) (let-values ([(line col pos) (port-next-location p)]) col))
50+
(define (getcol) (define-values (line col pos) (port-next-location p))
51+
col)
5152
;; total size of the two prefixes
5253
(define (2pfx-length pfx1 pfx2)
5354
(if (and pfx1 pfx2)
@@ -73,56 +74,85 @@
7374
(define-syntax-rule (show pfx) ; optimize when not needed
7475
(unless (eq? pfx 0) (write (->str pfx) p)))
7576
(when (and pfx1 pfx2)
76-
(if (eq? 0 col)
77-
(begin (show pfx1) (show pfx2))
78-
(let ([len1 (if (number? pfx1) pfx1 (string-length pfx1))])
79-
(cond [(< col len1) (write (->str pfx1) p col) (show pfx2)]
80-
[(= col len1) (show pfx2)]
81-
[(eq? 0 pfx2)]
82-
[else
83-
(let ([col (- col len1)]
84-
[len2 (if (number? pfx2) pfx2 (string-length pfx2))])
85-
(when (< col len2) (write (->str pfx2) p col)))])))))
77+
(cond
78+
[(eq? 0 col)
79+
(show pfx1)
80+
(show pfx2)]
81+
[else
82+
(define len1
83+
(if (number? pfx1)
84+
pfx1
85+
(string-length pfx1)))
86+
(cond
87+
[(< col len1)
88+
(write (->str pfx1) p col)
89+
(show pfx2)]
90+
[(= col len1) (show pfx2)]
91+
[(eq? 0 pfx2)]
92+
[else
93+
(let ([col (- col len1)]
94+
[len2 (if (number? pfx2)
95+
pfx2
96+
(string-length pfx2))])
97+
(when (< col len2)
98+
(write (->str pfx2) p col)))])])))
8699
;; the basic printing unit: strings
87100
(define (output-string x)
88101
(define pfx (mcar pfxs))
89-
(if (not pfx) ; prefix disabled?
90-
(write x p)
91-
(let ([len (string-length x)]
92-
[nls (regexp-match-positions* #rx"\n" x)])
93-
(let loop ([start 0] [nls nls] [lpfx (mcdr pfxs)] [col (getcol)])
94-
(cond [(pair? nls)
95-
(define nl (car nls))
96-
(if (regexp-match? #rx"^ *$" x start (car nl))
97-
(newline p) ; only spaces before the end of the line
98-
(begin (output-pfx col pfx lpfx)
99-
(write x p start (cdr nl))))
100-
(loop (cdr nl) (cdr nls) 0 0)]
101-
;; last substring from here (always set lpfx state when done)
102-
[(start . = . len)
103-
(set-mcdr! pfxs lpfx)]
104-
[(col . > . (2pfx-length pfx lpfx))
105-
(set-mcdr! pfxs lpfx)
106-
;; the prefix was already shown, no accumulation needed
107-
(write x p start)]
108-
[else
109-
(define m (regexp-match-positions #rx"^ +" x start))
110-
;; accumulate spaces to lpfx, display if it's not all spaces
111-
(define lpfx* (if m (pfx+ lpfx (- (cdar m) (caar m))) lpfx))
112-
(set-mcdr! pfxs lpfx*)
113-
(unless (and m (= len (cdar m)))
114-
(output-pfx col pfx lpfx*)
115-
;; the spaces were already added to lpfx
116-
(write x p (if m (cdar m) start)))])))))
102+
(cond
103+
; prefix disabled?
104+
[(not pfx) (write x p)]
105+
[else
106+
(define len (string-length x))
107+
(define nls (regexp-match-positions* #rx"\n" x))
108+
(let loop ([start 0]
109+
[nls nls]
110+
[lpfx (mcdr pfxs)]
111+
[col (getcol)])
112+
(cond
113+
[(pair? nls)
114+
(define nl (car nls))
115+
(if (regexp-match? #rx"^ *$" x start (car nl))
116+
(newline p) ; only spaces before the end of the line
117+
(begin
118+
(output-pfx col pfx lpfx)
119+
(write x p start (cdr nl))))
120+
(loop (cdr nl) (cdr nls) 0 0)]
121+
;; last substring from here (always set lpfx state when done)
122+
[(start . = . len) (set-mcdr! pfxs lpfx)]
123+
[(col . > . (2pfx-length pfx lpfx))
124+
(set-mcdr! pfxs lpfx)
125+
;; the prefix was already shown, no accumulation needed
126+
(write x p start)]
127+
[else
128+
(define m (regexp-match-positions #rx"^ +" x start))
129+
;; accumulate spaces to lpfx, display if it's not all spaces
130+
(define lpfx*
131+
(if m
132+
(pfx+ lpfx (- (cdar m) (caar m)))
133+
lpfx))
134+
(set-mcdr! pfxs lpfx*)
135+
(unless (and m (= len (cdar m)))
136+
(output-pfx col pfx lpfx*)
137+
;; the spaces were already added to lpfx
138+
(write x
139+
p
140+
(if m
141+
(cdar m)
142+
start)))]))]))
117143
;; blocks and splices
118144
(define (output-block c)
119145
(define pfx (mcar pfxs))
120146
(define lpfx (mcdr pfxs))
121147
(define npfx (pfx+col (pfx+ pfx lpfx)))
122148
(set-mcar! pfxs npfx) (set-mcdr! pfxs 0)
123-
(if (list? c)
124-
(for ([c (in-list c)]) (loop c))
125-
(begin (loop (car c)) (loop (cdr c))))
149+
(cond
150+
[(list? c)
151+
(for ([c (in-list c)])
152+
(loop c))]
153+
[else
154+
(loop (car c))
155+
(loop (cdr c))])
126156
(set-mcar! pfxs pfx) (set-mcdr! pfxs lpfx))
127157
(define (output-splice c)
128158
(for-each loop c))
@@ -145,16 +175,18 @@
145175
(define c (special-contents x))
146176
(case (special-flag x)
147177
;; preserve tailness & avoid `set!' for blocks/splices if possible
148-
[(block) (if list=block?
149-
(output-block c)
150-
(begin (set! list=block? #t)
151-
(output-block c)
152-
(set! list=block? #f)))]
153-
[(splice) (if list=block?
154-
(begin (set! list=block? #f)
155-
(output-splice c)
156-
(set! list=block? #t))
157-
(output-splice c))]
178+
[(block) (cond
179+
[list=block? (output-block c)]
180+
[else
181+
(set! list=block? #t)
182+
(output-block c)
183+
(set! list=block? #f)])]
184+
[(splice) (cond
185+
[list=block?
186+
(set! list=block? #f)
187+
(output-splice c)
188+
(set! list=block? #t)]
189+
[else (output-splice c)])]
158190
[(flush) ; useful before `disable-prefix'
159191
(output-pfx (getcol) (mcar pfxs) (mcdr pfxs))]
160192
[(disable-prefix) ; save the previous pfxs
@@ -243,11 +275,16 @@
243275
(let ([t (make-weak-hasheq)]
244276
[last '(#f #f)]) ; cache for the last port, to avoid a hash lookup
245277
(λ (p)
246-
(if (eq? p (car last)) (cdr last)
247-
(let ([s (or (hash-ref t p #f)
248-
(let ([s (mcons 0 0)]) (hash-set! t p s) s))])
249-
(set! last (cons p s))
250-
s)))))
278+
(cond
279+
[(eq? p (car last)) (cdr last)]
280+
[else
281+
(define s
282+
(or (hash-ref t p #f)
283+
(let ([s (mcons 0 0)])
284+
(hash-set! t p s)
285+
s)))
286+
(set! last (cons p s))
287+
s]))))
251288

252289
;; special constructs
253290

@@ -292,12 +329,13 @@
292329
(define (add-newlines list #:sep [sep "\n"])
293330
(define r
294331
(let loop ([list list])
295-
(if (null? list)
296-
null
297-
(let ([1st (car list)])
298-
(if (or (not 1st) (void? 1st))
299-
(loop (cdr list))
300-
(list* sep 1st (loop (cdr list))))))))
332+
(cond
333+
[(null? list) null]
334+
[else
335+
(define 1st (car list))
336+
(if (or (not 1st) (void? 1st))
337+
(loop (cdr list))
338+
(list* sep 1st (loop (cdr list))))])))
301339
(if (null? r) r (cdr r)))
302340

303341
(provide split-lines)

0 commit comments

Comments
 (0)