Skip to content

Commit ad1951e

Browse files
committed
Cleanup: use 'let x?' Swift sugar for Optional pattern matching
1 parent 96377e4 commit ad1951e

34 files changed

+188
-251
lines changed

Sources/AppBundle/command/impl/ConfigCommand.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,9 @@ extension String {
6262
}
6363
}
6464
if args.json {
65-
if let json = JSONEncoder.aeroSpaceDefault.encodeToString(configMap) {
66-
return io.out(json)
67-
} else {
68-
return io.err("Can't convert json Data to String")
65+
return switch JSONEncoder.aeroSpaceDefault.encodeToString(configMap) {
66+
case let json?: io.out(json)
67+
case nil: io.err("Can't convert json Data to String")
6968
}
7069
} else {
7170
switch configMap {
@@ -97,17 +96,15 @@ extension ConfigMapValue {
9796
case .scalar(let scalar):
9897
return .failure("Can't dereference scalar value '\(scalar.describe)'")
9998
case .map(let map):
100-
if let child = map[key] {
101-
return child.find(keyPath: keyPath.slice(1...).orDie())
102-
} else {
103-
return .failure("No value at key token '\(key)'")
99+
return switch map[key] {
100+
case let child?: child.find(keyPath: keyPath.slice(1...).orDie())
101+
case nil: .failure("No value at key token '\(key)'")
104102
}
105103
case .array(let array):
106104
if let key = Int(key) {
107-
if let child = array.getOrNil(atIndex: key) {
108-
return child.find(keyPath: keyPath.slice(1...).orDie())
109-
} else {
110-
return .failure("Index out of bounds. Index: \(key), Size: \(array.count)")
105+
return switch array.getOrNil(atIndex: key) {
106+
case let child?: child.find(keyPath: keyPath.slice(1...).orDie())
107+
case nil: .failure("Index out of bounds. Index: \(key), Size: \(array.count)")
111108
}
112109
} else {
113110
return .failure("Can't convert key token '\(key)' to Int")

Sources/AppBundle/command/impl/FocusBackAndForthCommand.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ struct FocusBackAndForthCommand: Command {
66
/*conforms*/ let shouldResetClosedWindowsCache = false
77

88
func run(_ env: CmdEnv, _ io: CmdIo) -> Bool {
9-
if let prevFocus {
10-
return setFocus(to: prevFocus)
11-
} else {
12-
return io.err("Prev window has been closed")
9+
switch prevFocus {
10+
case let prevFocus?: setFocus(to: prevFocus)
11+
case nil: io.err("Prev window has been closed")
1312
}
1413
}
1514
}

Sources/AppBundle/command/impl/ListAppsCommand.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@ struct ListAppsCommand: Command {
1111
result = result.filter { $0.nsApp.isHidden == hidden }
1212
}
1313

14-
if args.outputOnlyCount {
15-
return io.out("\(result.count)")
16-
} else {
17-
let list = result.map { AeroObj.app($0) }
18-
if args.json {
19-
return switch list.formatToJson(args.format, ignoreRightPaddingVar: args._format.isEmpty) {
14+
lazy var list = result.map(AeroObj.app)
15+
return switch true {
16+
case args.outputOnlyCount:
17+
io.out("\(result.count)")
18+
case args.json:
19+
switch list.formatToJson(args.format, ignoreRightPaddingVar: args._format.isEmpty) {
2020
case .success(let json): io.out(json)
2121
case .failure(let msg): io.err(msg)
2222
}
23-
} else {
24-
return switch list.format(args.format) {
23+
default:
24+
switch list.format(args.format) {
2525
case .success(let lines): io.out(lines)
2626
case .failure(let msg): io.err(msg)
2727
}
28-
}
2928
}
3029
}
3130
}

Sources/AppBundle/command/impl/ListMonitorsCommand.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,20 @@ struct ListMonitorsCommand: Command {
1616
result = result.filter { (monitor) in (monitor.activeWorkspace == mouseWorkspace) == mouse }
1717
}
1818

19-
if args.outputOnlyCount {
20-
return io.out("\(result.count)")
21-
} else {
22-
let list = result.map { AeroObj.monitor($0) }
23-
if args.json {
24-
return switch list.formatToJson(args.format, ignoreRightPaddingVar: args._format.isEmpty) {
19+
lazy var list = result.map(AeroObj.monitor)
20+
return switch true {
21+
case args.outputOnlyCount:
22+
io.out("\(result.count)")
23+
case args.json:
24+
switch list.formatToJson(args.format, ignoreRightPaddingVar: args._format.isEmpty) {
2525
case .success(let json): io.out(json)
2626
case .failure(let msg): io.err(msg)
2727
}
28-
} else {
29-
return switch list.format(args.format) {
28+
default:
29+
switch list.format(args.format) {
3030
case .success(let lines): io.out(lines)
3131
case .failure(let msg): io.err(msg)
3232
}
33-
}
3433
}
3534
}
3635
}

Sources/AppBundle/command/impl/ListWindowsCommand.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ struct ListWindowsCommand: Command {
1010
var windows: [Window] = []
1111

1212
if args.filteringOptions.focused {
13-
if let window = focus.windowOrNil {
14-
windows = [window]
15-
} else {
16-
return io.err(noWindowIsFocused)
13+
switch focus.windowOrNil {
14+
case let window?: windows = [window]
15+
case nil: return io.err(noWindowIsFocused)
1716
}
1817
} else {
1918
var workspaces: Set<Workspace> = args.filteringOptions.workspaces.isEmpty

Sources/AppBundle/command/impl/ListWorkspacesCommand.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,20 @@ struct ListWorkspacesCommand: Command {
1919
result = result.filter { $0.isEffectivelyEmpty == empty }
2020
}
2121

22-
if args.outputOnlyCount {
23-
return io.out("\(result.count)")
24-
} else {
25-
let list = result.map { AeroObj.workspace($0) }
26-
if args.json {
27-
return switch list.formatToJson(args.format, ignoreRightPaddingVar: args._format.isEmpty) {
22+
lazy var list = result.map() { AeroObj.workspace($0) }
23+
return switch true {
24+
case args.outputOnlyCount:
25+
io.out("\(result.count)")
26+
case args.json:
27+
switch list.formatToJson(args.format, ignoreRightPaddingVar: args._format.isEmpty) {
2828
case .success(let json): io.out(json)
2929
case .failure(let msg): io.err(msg)
3030
}
31-
} else {
32-
return switch list.format(args.format) {
31+
default:
32+
switch list.format(args.format) {
3333
case .success(let lines): io.out(lines)
3434
case .failure(let msg): io.err(msg)
3535
}
36-
}
3736
}
3837
}
3938
}

Sources/AppBundle/command/impl/MoveCommand.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,15 @@ private let moveOutMacosUnconventionalWindow = "moving macOS fullscreen, minimiz
153153

154154
extension TilingTreeNodeCases {
155155
@MainActor fileprivate func findDeepMoveInTargetRecursive(_ orientation: Orientation) -> TilingTreeNodeCases {
156-
return switch self {
156+
switch self {
157157
case .window:
158158
self
159+
case .tilingContainer(let container) where container.orientation == orientation:
160+
.tilingContainer(container)
159161
case .tilingContainer(let container):
160-
if container.orientation == orientation {
161-
.tilingContainer(container)
162-
} else {
163-
container.mostRecentChild.orDie("Empty containers must be detached during normalization")
164-
.tilingTreeNodeCasesOrDie()
165-
.findDeepMoveInTargetRecursive(orientation)
166-
}
162+
container.mostRecentChild.orDie("Empty containers must be detached during normalization")
163+
.tilingTreeNodeCasesOrDie()
164+
.findDeepMoveInTargetRecursive(orientation)
167165
}
168166
}
169167
}

Sources/AppBundle/command/impl/MoveMouseCommand.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ private func moveMouse(_ io: CmdIo, _ point: CGPoint) -> Bool {
4343
mouseCursorPosition: point,
4444
mouseButton: CGMouseButton.left,
4545
)
46-
if let event {
47-
event.post(tap: CGEventTapLocation.cghidEventTap)
48-
return true
49-
} else {
50-
return io.err("Failed to move mouse")
46+
switch event {
47+
case nil: return io.err("Failed to move mouse")
48+
case let event?:
49+
event.post(tap: CGEventTapLocation.cghidEventTap)
50+
return true
5151
}
5252
}
5353

Sources/AppBundle/command/impl/SwapCommand.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ struct SwapCommand: Command {
1717
let targetWindow: Window?
1818
switch args.target.val {
1919
case .direction(let direction):
20-
if let (parent, ownIndex) = currentWindow.closestParent(hasChildrenInDirection: direction, withLayout: nil) {
21-
targetWindow = parent.children[ownIndex + direction.focusOffset].findLeafWindowRecursive(snappedTo: direction.opposite)
22-
} else if args.wrapAround {
23-
targetWindow = target.workspace.findLeafWindowRecursive(snappedTo: direction.opposite)
24-
} else {
25-
return false
20+
switch currentWindow.closestParent(hasChildrenInDirection: direction, withLayout: nil) {
21+
case let (parent, ownIndex)?:
22+
targetWindow = parent.children[ownIndex + direction.focusOffset].findLeafWindowRecursive(snappedTo: direction.opposite)
23+
case nil where args.wrapAround:
24+
targetWindow = target.workspace.findLeafWindowRecursive(snappedTo: direction.opposite)
25+
case nil:
26+
return false
2627
}
2728
case .dfsRelative(let nextPrev):
2829
let windows = target.workspace.rootTilingContainer.allLeafWindowsRecursive

Sources/AppBundle/command/impl/WorkspaceCommand.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ struct WorkspaceCommand: Command {
4848
.union([current])
4949
.sorted()
5050
let index = workspaces.firstIndex(where: { $0 == target.workspace }) ?? 0
51-
let workspace: Workspace? = if wrapAround {
52-
workspaces.get(wrappingIndex: isNext ? index + 1 : index - 1)
53-
} else {
54-
workspaces.getOrNil(atIndex: isNext ? index + 1 : index - 1)
51+
let workspace: Workspace? = switch wrapAround {
52+
case true: workspaces.get(wrappingIndex: isNext ? index + 1 : index - 1)
53+
case false: workspaces.getOrNil(atIndex: isNext ? index + 1 : index - 1)
5554
}
5655
return workspace
5756
}

0 commit comments

Comments
 (0)