Skip to content

Commit e7811f5

Browse files
committed
[Refactoring] SR-6051 Expansion of switch statement missing cases when cursor is placed on switch keyword
1 parent d866ebf commit e7811f5

File tree

9 files changed

+236
-74
lines changed

9 files changed

+236
-74
lines changed

include/swift/IDE/RefactoringKinds.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ CURSOR_REFACTORING(FillProtocolStub, "Add Missing Protocol Requirements", fillst
3434

3535
CURSOR_REFACTORING(ExpandDefault, "Expand Default", expand.default)
3636

37+
CURSOR_REFACTORING(ExpandSwitchCases, "Expand Switch Cases", expand.switch.cases)
38+
3739
CURSOR_REFACTORING(LocalizeString, "Localize String", localize.string)
3840

3941
CURSOR_REFACTORING(SimplifyNumberLiteral, "Simplify Long Number Literal", simplify.long.number.literal)

lib/IDE/Refactoring.cpp

Lines changed: 148 additions & 74 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
enum E {
2+
case e1
3+
case e2
4+
case e3
5+
case e4
6+
}
7+
8+
func foo(e: E) -> Int {
9+
switch e {
10+
case .e1: <#code#>
11+
case .e2: <#code#>
12+
case .e3: <#code#>
13+
case .e4: <#code#>
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
enum E {
2+
case e1
3+
case e2
4+
case e3
5+
case e4
6+
}
7+
8+
func foo(e: E) -> Int {
9+
switch e {
10+
case .e1: <#code#>
11+
case .e2: <#code#>
12+
case .e3: <#code#>
13+
case .e4: <#code#>
14+
return 3
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
enum E {
2+
case e1
3+
case e2
4+
case e3
5+
case e4
6+
}
7+
8+
func foo(e: E) -> Int {
9+
switch e { }
10+
}
11+
// RUN: rm -rf %t.result && mkdir -p %t.result
12+
// RUN: %refactor -expand-switch-cases -source-filename %s -pos=9:8 >> %t.result/L10.swift
13+
// RUN: diff -u %S/Outputs/basic/L10.swift.expected %t.result/L10.swift
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
enum E {
2+
case e1
3+
case e2
4+
case e3
5+
case e4
6+
}
7+
8+
func foo(e: E) -> Int {
9+
switch e { }
10+
}
11+
// RUN: rm -rf %t.result && mkdir -p %t.result
12+
// RUN: %refactor -expand-switch-cases -source-filename %s -pos=9:8 >> %t.result/L10.swift
13+
// RUN: diff -u %S/Outputs/partially_handled/L10.swift.expected %t.result/L10.swift
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
enum E {
2+
case e1
3+
case e2
4+
case e3
5+
case e4
6+
}
7+
8+
func foo(e: E) -> Int {
9+
switch e {
10+
default: return 3
11+
}
12+
}
13+
// RUN: rm -rf %t.result && mkdir -p %t.result
14+
// RUN: %refactor -expand-switch-cases -source-filename %s -pos=9:8 >> %t.result/L10.swift
15+
// RUN: diff -u %S/Outputs/with_default/L10.swift.expected %t.result/L10.swift

test/refactoring/RefactoringKind/basic.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ func testForceTry() {
216216
func throwingFunc() throws -> Int { return 3 }
217217
let _ = try! throwingFunc()
218218
}
219+
220+
func testSwitchExpand() {
221+
enum AccountType {
222+
case savings, checking
223+
}
224+
let account: AccountType = .savings
225+
switch account { }
226+
}
219227
// RUN: %refactor -source-filename %s -pos=2:1 -end-pos=5:13 | %FileCheck %s -check-prefix=CHECK1
220228
// RUN: %refactor -source-filename %s -pos=3:1 -end-pos=5:13 | %FileCheck %s -check-prefix=CHECK1
221229
// RUN: %refactor -source-filename %s -pos=4:1 -end-pos=5:13 | %FileCheck %s -check-prefix=CHECK1
@@ -301,6 +309,9 @@ func testForceTry() {
301309
// RUN: %refactor -source-filename %s -pos=217:13 | %FileCheck %s -check-prefix=CHECK-TRY-CATCH
302310
// RUN: %refactor -source-filename %s -pos=217:14 | %FileCheck %s -check-prefix=CHECK-TRY-CATCH
303311

312+
// RUN: %refactor -source-filename %s -pos=225:3 | %FileCheck %s -check-prefix=CHECK-EXPAND-SWITCH
313+
314+
304315
// CHECK1: Action begins
305316
// CHECK1-NEXT: Extract Method
306317
// CHECK1-NEXT: Action ends
@@ -342,3 +353,4 @@ func testForceTry() {
342353

343354
// CHECK-TRY-CATCH: Convert To Do/Catch
344355

356+
// CHECK-EXPAND-SWITCH: Expand Switch Cases

tools/swift-refactor/swift-refactor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Action(llvm::cl::desc("kind:"), llvm::cl::init(RefactoringKind::None),
3636
"fill-stub", "Perform fill protocol stub refactoring"),
3737
clEnumValN(RefactoringKind::ExpandDefault,
3838
"expand-default", "Perform expand default statement refactoring"),
39+
clEnumValN(RefactoringKind::ExpandSwitchCases,
40+
"expand-switch-cases", "Perform switch cases expand refactoring"),
3941
clEnumValN(RefactoringKind::LocalizeString,
4042
"localize-string", "Perform string localization refactoring"),
4143
clEnumValN(RefactoringKind::CollapseNestedIfExpr,

0 commit comments

Comments
 (0)