From cbc6f13b3b4beb0a07e2acd1add48a084baa3848 Mon Sep 17 00:00:00 2001 From: Jack Firth Date: Sun, 19 Oct 2025 13:18:34 -0700 Subject: [PATCH] Fix list to match define rule focusing --- .../match-shortcuts-test.rkt | 26 +++++++++++++++++++ default-recommendations/match-shortcuts.rkt | 6 +++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/default-recommendations/match-shortcuts-test.rkt b/default-recommendations/match-shortcuts-test.rkt index 15b1e61..81807ab 100644 --- a/default-recommendations/match-shortcuts-test.rkt +++ b/default-recommendations/match-shortcuts-test.rkt @@ -446,3 +446,29 @@ no-change-test: "list element variable definitions not refactorable when referen (define z (list-ref (first pt-list) 2)) (+ x y z)) ------------------------------ + + +test: "refactoring list element variable definitions to match-define doesn't reformat surroundings" +------------------------------ +(require racket/list) +(define (f pt) + + ( void ) + + (define x (list-ref pt 0)) + (define y (list-ref pt 1)) + (define z (list-ref pt 2)) + + ; comment + ( + x y z )) +============================== +(require racket/list) +(define (f pt) + + ( void ) + + (match-define (list x y z) pt) + + ; comment + ( + x y z )) +------------------------------ diff --git a/default-recommendations/match-shortcuts.rkt b/default-recommendations/match-shortcuts.rkt index e04d0a7..39f167a 100644 --- a/default-recommendations/match-shortcuts.rkt +++ b/default-recommendations/match-shortcuts.rkt @@ -247,7 +247,7 @@ `match-define`. Note that the suggested replacement raises an error if the list contains more \ elements than expected." #:literals (define) - (~seq body-before ... (define v:id ref-expr:list-ref-expr) ...+ body-after ...) + (~seq body-before ... (~and definition (define v:id ref-expr:list-ref-expr)) ...+ body-after ...+) #:do [(define num-vars (length (attribute v)))] #:with first-list-id (first (attribute ref-expr.list-id)) #:when (for/and ([list-id (in-list (rest (attribute ref-expr.list-id)))]) @@ -257,7 +257,9 @@ elements than expected." id:id #:when (free-identifier=? (attribute id) (attribute first-list-id)) #:when (not (equal? (syntax-property (attribute id) 'usage-count) num-vars)))) - (body-before ... (match-define (list v ...) first-list-id) body-after ...)) + #:with match-definition + #'(~replacement (match-define (list v ...) first-list-id) #:original-splice (definition ...)) + (body-before ... (~focus-replacement-on match-definition) body-after ...)) (define-refactoring-suite match-shortcuts