|
10 | 10 |
|
11 | 11 |
|
12 | 12 | (require (for-syntax racket/base)
|
| 13 | + guard |
13 | 14 | racket/function
|
14 | 15 | racket/list
|
| 16 | + racket/sequence |
15 | 17 | racket/set
|
16 |
| - rebellion/private/static-name |
17 | 18 | resyntax/base
|
18 | 19 | resyntax/default-recommendations/private/lambda-by-any-name
|
19 | 20 | resyntax/default-recommendations/private/literal-constant
|
20 | 21 | resyntax/default-recommendations/private/syntax-identifier-sets
|
21 |
| - resyntax/private/syntax-neighbors |
22 | 22 | syntax/parse)
|
23 | 23 |
|
24 | 24 |
|
|
140 | 140 | (make-list count elem))
|
141 | 141 |
|
142 | 142 |
|
| 143 | +(define/guard (all-free-identifier=? ids) |
| 144 | + (guard-match (cons first-id remaining-ids) (sequence->list ids) #:else #false) |
| 145 | + (for/and ([id (in-list remaining-ids)]) |
| 146 | + (free-identifier=? first-id id))) |
| 147 | + |
| 148 | + |
| 149 | +(define/guard (contiguous-increasing-integer-series? ints) |
| 150 | + (define int-list (sequence->list ints)) |
| 151 | + (guard (not (empty? int-list)) #:else #false) |
| 152 | + (for/and ([previous (in-list int-list)] |
| 153 | + [next (in-list (rest int-list))]) |
| 154 | + (equal? (add1 previous) next))) |
| 155 | + |
| 156 | + |
| 157 | +(define-syntax-class list-selection-expression |
| 158 | + #:attributes (target-list-id index) |
| 159 | + #:literals (list-ref |
| 160 | + first |
| 161 | + second |
| 162 | + third |
| 163 | + fourth |
| 164 | + fifth |
| 165 | + sixth |
| 166 | + seventh |
| 167 | + eighth |
| 168 | + ninth |
| 169 | + tenth |
| 170 | + car |
| 171 | + cadr |
| 172 | + caddr |
| 173 | + cadddr) |
| 174 | + |
| 175 | + (pattern (list-ref target-list-id:id index-stx:nat) #:attr index (syntax-e #'index-stx)) |
| 176 | + (pattern (first target-list-id:id) #:attr index 0) |
| 177 | + (pattern (second target-list-id:id) #:attr index 1) |
| 178 | + (pattern (third target-list-id:id) #:attr index 2) |
| 179 | + (pattern (fourth target-list-id:id) #:attr index 3) |
| 180 | + (pattern (fifth target-list-id:id) #:attr index 4) |
| 181 | + (pattern (sixth target-list-id:id) #:attr index 5) |
| 182 | + (pattern (seventh target-list-id:id) #:attr index 6) |
| 183 | + (pattern (eighth target-list-id:id) #:attr index 7) |
| 184 | + (pattern (ninth target-list-id:id) #:attr index 8) |
| 185 | + (pattern (tenth target-list-id:id) #:attr index 9) |
| 186 | + (pattern (car target-list-id:id) #:attr index 0) |
| 187 | + (pattern (cadr target-list-id:id) #:attr index 1) |
| 188 | + (pattern (caddr target-list-id:id) #:attr index 2) |
| 189 | + (pattern (cadddr target-list-id:id) #:attr index 3)) |
| 190 | + |
| 191 | + |
| 192 | +(define-refactoring-rule list-selectors-to-take-and-drop |
| 193 | + #:description |
| 194 | + "This list expression is constructing a sublist of a larger list, which can be expressed more\ |
| 195 | + clearly with `take` and `drop`." |
| 196 | + #:literals (list) |
| 197 | + |
| 198 | + (list selection:list-selection-expression ...) |
| 199 | + |
| 200 | + #:when (>= (length (attribute selection)) 3) |
| 201 | + |
| 202 | + #:when (all-free-identifier=? (attribute selection.target-list-id)) |
| 203 | + #:with target-list-id (first (attribute selection.target-list-id)) |
| 204 | + |
| 205 | + #:when (contiguous-increasing-integer-series? (attribute selection.index)) |
| 206 | + #:do [(define first-index (first (attribute selection.index))) |
| 207 | + (define last-index (last (attribute selection.index)))] |
| 208 | + |
| 209 | + #:with target-list-with-prefix-dropped |
| 210 | + (if (zero? first-index) #'target-list-id #`(drop target-list-id #,first-index)) |
| 211 | + |
| 212 | + #:with amount-to-take (- (add1 last-index) first-index) |
| 213 | + |
| 214 | + (take target-list-with-prefix-dropped amount-to-take)) |
| 215 | + |
| 216 | + |
143 | 217 | (define-refactoring-suite list-shortcuts
|
144 | 218 | #:rules (append-single-list-to-single-list
|
145 | 219 | append*-and-map-to-append-map
|
|
150 | 224 | filter-to-remv*
|
151 | 225 | first-reverse-to-last
|
152 | 226 | ignored-map-to-for-each
|
| 227 | + list-selectors-to-take-and-drop |
153 | 228 | quasiquote-to-append
|
154 | 229 | quasiquote-to-list
|
155 | 230 | sort-with-keyed-comparator-to-sort-by-key))
|
0 commit comments