Skip to content

Commit 1330afb

Browse files
authored
Fix list to match define rule focusing (#623)
1 parent fe4a912 commit 1330afb

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

default-recommendations/match-shortcuts-test.rkt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,3 +446,29 @@ no-change-test: "list element variable definitions not refactorable when referen
446446
(define z (list-ref (first pt-list) 2))
447447
(+ x y z))
448448
------------------------------
449+
450+
451+
test: "refactoring list element variable definitions to match-define doesn't reformat surroundings"
452+
------------------------------
453+
(require racket/list)
454+
(define (f pt)
455+
456+
( void )
457+
458+
(define x (list-ref pt 0))
459+
(define y (list-ref pt 1))
460+
(define z (list-ref pt 2))
461+
462+
; comment
463+
( + x y z ))
464+
==============================
465+
(require racket/list)
466+
(define (f pt)
467+
468+
( void )
469+
470+
(match-define (list x y z) pt)
471+
472+
; comment
473+
( + x y z ))
474+
------------------------------

default-recommendations/match-shortcuts.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
`match-define`. Note that the suggested replacement raises an error if the list contains more \
248248
elements than expected."
249249
#:literals (define)
250-
(~seq body-before ... (define v:id ref-expr:list-ref-expr) ...+ body-after ...)
250+
(~seq body-before ... (~and definition (define v:id ref-expr:list-ref-expr)) ...+ body-after ...+)
251251
#:do [(define num-vars (length (attribute v)))]
252252
#:with first-list-id (first (attribute ref-expr.list-id))
253253
#:when (for/and ([list-id (in-list (rest (attribute ref-expr.list-id)))])
@@ -257,7 +257,9 @@ elements than expected."
257257
id:id
258258
#:when (free-identifier=? (attribute id) (attribute first-list-id))
259259
#:when (not (equal? (syntax-property (attribute id) 'usage-count) num-vars))))
260-
(body-before ... (match-define (list v ...) first-list-id) body-after ...))
260+
#:with match-definition
261+
#'(~replacement (match-define (list v ...) first-list-id) #:original-splice (definition ...))
262+
(body-before ... (~focus-replacement-on match-definition) body-after ...))
261263

262264

263265
(define-refactoring-suite match-shortcuts

0 commit comments

Comments
 (0)