Skip to content

Commit 0ef2abe

Browse files
authored
Improve definition context reformatting behavior (#358)
1 parent b414b5d commit 0ef2abe

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

default-recommendations/definition-shortcuts-test.rkt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,16 @@ test: "inlining immediately returned variable definition doesn't reformat entire
106106

107107
1)
108108
------------------------------
109+
110+
111+
test: "inlining immediately returned variable definition in empty context does reformat"
112+
------------------------------
113+
(map (λ (x)
114+
(define y (* x 2))
115+
y)
116+
(list 1 2 3))
117+
------------------------------
118+
------------------------------
119+
(map (λ (x) (* x 2))
120+
(list 1 2 3))
121+
------------------------------

default-recommendations/definition-shortcuts.rkt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
(require (for-syntax racket/base)
13+
racket/list
1314
rebellion/private/static-name
1415
resyntax/base
1516
syntax/parse)
@@ -32,7 +33,11 @@
3233
#:literals (define)
3334
(~seq body-before ... (~and definition (define id1:id expr)) id2:id)
3435
#:when (free-identifier=? #'id1 #'id2)
35-
(body-before ... (~focus-replacement-on (~replacement expr #:original-splice (definition id2)))))
36+
#:with replacement #'(~replacement expr #:original-splice (definition id2))
37+
#:with focused (if (empty? (attribute body-before))
38+
#'replacement
39+
#'(~focus-replacement-on replacement))
40+
(body-before ... focused))
3641

3742

3843
(define-refactoring-suite definition-shortcuts

default-recommendations/match-shortcuts-test.rkt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ test: "migrating single-clause match expressions to match-define doesn't reforma
5151
------------------------------
5252

5353

54+
test: "migrating single-clause match expressions in single-form contexts does reformat"
55+
------------------------------
56+
(map (λ (x) (match x [(list a b c) (+ a b c)]))
57+
(list (list 1 2 3) (list 4 5 6)))
58+
------------------------------
59+
------------------------------
60+
(map (λ (x)
61+
(match-define (list a b c) x)
62+
(+ a b c))
63+
(list (list 1 2 3) (list 4 5 6)))
64+
------------------------------
65+
66+
5467
test: "single-clause match expressions inside cond can be replaced with match-define expressions"
5568
------------------------------
5669
(define (foo x condition)

default-recommendations/match-shortcuts.rkt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,11 @@
99
[match-shortcuts refactoring-suite?]))
1010

1111

12-
(require (for-syntax racket/base)
12+
(require racket/list
1313
racket/match
1414
racket/set
15-
rebellion/private/static-name
1615
resyntax/base
17-
resyntax/default-recommendations/private/definition-context
1816
resyntax/default-recommendations/private/syntax-identifier-sets
19-
resyntax/default-recommendations/private/syntax-lines
20-
resyntax/default-recommendations/private/syntax-tree
21-
resyntax/private/syntax-neighbors
2217
syntax/parse)
2318

2419

@@ -41,7 +36,11 @@
4136
(~seq body-before ... match-expression:single-clause-match)
4237
#:when (set-empty? (set-intersect (syntax-bound-identifiers #'(body-before ...))
4338
(syntax-bound-identifiers #'match-expression.match-pattern)))
44-
(body-before ... (~@ . (~focus-replacement-on (match-expression.as-definition-context-body ...)))))
39+
#:with (new-body ...) (if (empty? (attribute body-before))
40+
(attribute match-expression.as-definition-context-body)
41+
#'(~focus-replacement-on
42+
(match-expression.as-definition-context-body ...)))
43+
(body-before ... new-body ...))
4544

4645

4746
(define-refactoring-suite match-shortcuts

0 commit comments

Comments
 (0)