Skip to content

Commit

Permalink
Replace silently failing precondition with own check function
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabobko committed Oct 26, 2023
1 parent d35a678 commit bedd9e9
Show file tree
Hide file tree
Showing 26 changed files with 45 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/command/CloseAllWindowsButCurrentCommand.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CloseAllWindowsButCurrentCommand: Command {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let focused = focusedWindowOrEffectivelyFocused else { return }
for window in focused.workspace.allLeafWindowsRecursive {
if window != focused {
Expand Down
2 changes: 1 addition & 1 deletion src/command/CompositeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct CompositeCommand: Command { // todo drop
let subCommands: [Command]

func runWithoutRefresh() async {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
for command in subCommands {
await command.runWithoutRefresh()
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/ExecAndForgetCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct ExecAndForgetCommand: Command {
let bashCommand: String

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
// It doesn't throw if exit code is non-zero
try! Process.run(URL(filePath: "/bin/bash"), arguments: ["-c", bashCommand])
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/ExecAndWaitCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct ExecAndWaitCommand: Command {
let bashCommand: String

func runWithoutRefresh() async {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
await withCheckedContinuation { (continuation: CheckedContinuation<(), Never>) in
let process = Process()
process.executableURL = URL(filePath: "/bin/bash")
Expand Down
2 changes: 1 addition & 1 deletion src/command/FlattenWorkspaceTreeCommand.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct FlattenWorkspaceTreeCommand: Command {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let currentWindow = focusedWindowOrEffectivelyFocused else { return }
let workspace = currentWindow.workspace
let windows = workspace.rootTilingContainer.allLeafWindowsRecursive
Expand Down
2 changes: 1 addition & 1 deletion src/command/FocusCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct FocusCommand: Command {
let direction: CardinalDirection

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let currentWindow = focusedWindowOrEffectivelyFocused else { return }
guard let (parent, ownIndex) = currentWindow.closestParent(hasChildrenInDirection: direction, withLayout: nil) else { return }
let windowToFocus = parent.children[ownIndex + direction.focusOffset]
Expand Down
2 changes: 1 addition & 1 deletion src/command/LayoutCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct LayoutCommand: Command {
}

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let window = focusedWindowOrEffectivelyFocused else { return }
let targetLayout: ConfigLayout = toggleBetween.firstIndex(of: window.configLayout)
.flatMap { toggleBetween.getOrNil(atIndex: $0 + 1) }
Expand Down
2 changes: 1 addition & 1 deletion src/command/ModeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct ModeCommand: Command {
let idToActivate: String

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
activateMode(idToActivate)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/command/MoveInCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct MoveInCommand: Command {
let direction: CardinalDirection

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let currentWindow = focusedWindowOrEffectivelyFocused else { return }
guard let (parent, ownIndex) = currentWindow.closestParent(hasChildrenInDirection: direction, withLayout: nil) else { return }
let moveInTarget = parent.children[ownIndex + direction.focusOffset]
Expand Down
6 changes: 3 additions & 3 deletions src/command/MoveThroughCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct MoveThroughCommand: Command {
let direction: CardinalDirection

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let currentWindow = focusedWindowOrEffectivelyFocused else { return }
switch currentWindow.parent.kind {
case .tilingContainer(let parent):
Expand Down Expand Up @@ -40,13 +40,13 @@ private func moveOut(window: Window, direction: CardinalDirection) {
let bindToIndex: Int
switch innerMostChild.parent.genericKind {
case .tilingContainer(let parent):
precondition(parent.orientation == direction.orientation)
check(parent.orientation == direction.orientation)
bindTo = parent
bindToIndex = innerMostChild.ownIndex + direction.insertionOffset
case .workspace(let parent): // create implicit container
let prevRoot = parent.rootTilingContainer
prevRoot.unbindFromParent()
precondition(prevRoot != parent.rootTilingContainer)
check(prevRoot != parent.rootTilingContainer)
parent.rootTilingContainer.orientation = direction.orientation
parent.rootTilingContainer.layout = .list // todo force List layout for implicit containers?
prevRoot.bindTo(parent: parent.rootTilingContainer, adaptiveWeight: WEIGHT_AUTO)
Expand Down
2 changes: 1 addition & 1 deletion src/command/MoveWorkspaceToDisplayCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct MoveWorkspaceToDisplayCommand: Command {
}

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
let focusedWorkspace = Workspace.focused
let prevMonitor = focusedWorkspace.monitor
let sortedMonitors = sortedMonitors
Expand Down
2 changes: 1 addition & 1 deletion src/command/ReloadConfigCommand.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct ReloadConfigCommand: Command {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
reloadConfig()
}
}
2 changes: 1 addition & 1 deletion src/command/ResizeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct ResizeCommand: Command { // todo cover with tests
let diff: Int

func runWithoutRefresh() { // todo support key repeat
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let window = focusedWindowOrEffectivelyFocused else { return }

switch window.parent.kind {
Expand Down
2 changes: 1 addition & 1 deletion src/command/WorkspaceBackAndForthCommand.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct WorkspaceBackAndForthCommand: Command {
func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
guard let previousFocusedWorkspaceName else { return }
WorkspaceCommand(workspaceName: previousFocusedWorkspaceName).runWithoutRefresh()
}
Expand Down
4 changes: 2 additions & 2 deletions src/command/WorkspaceCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ struct WorkspaceCommand : Command {
let workspaceName: String

func runWithoutRefresh() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
let workspace = Workspace.get(byName: workspaceName)
// todo drop anyLeafWindowRecursive. It must not be necessary
if let window = workspace.mostRecentWindow ?? workspace.anyLeafWindowRecursive { // switch to not empty workspace
Expand All @@ -16,7 +16,7 @@ struct WorkspaceCommand : Command {
window.focus()
// The switching itself will be done by refreshWorkspaces and layoutWorkspaces later in refresh
} else { // switch to empty workspace
precondition(workspace.isEffectivelyEmpty)
check(workspace.isEffectivelyEmpty)
workspace.monitor.setActiveWorkspace(workspace)
focusedWorkspaceName = workspace.name
focusedWorkspaceSourceOfTruth = .ownModel
Expand Down
2 changes: 1 addition & 1 deletion src/focused.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var focusedApp: AeroApp? {
if isUnitTest {
return appForTests
} else {
precondition(appForTests == nil)
check(appForTests == nil)
if NSWorkspace.shared.frontmostApplication == _focusedApp {
_focusedApp = nil
}
Expand Down
2 changes: 1 addition & 1 deletion src/mouse/resizeWithMouse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func resizedObs(_ obs: AXObserver, ax: AXUIElement, notif: CFString, data: Unsaf
}

func resetManipulatedWithMouseIfPossible() {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
if currentlyManipulatedWithMouseWindowId != nil {
currentlyManipulatedWithMouseWindowId = nil
for workspace in Workspace.all {
Expand Down
2 changes: 1 addition & 1 deletion src/refresh.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// It's one of the most important function of the whole application.
/// The function is called as a feedback response on every user input
func refresh(startup: Bool = false) {
precondition(Thread.current.isMainThread)
check(Thread.current.isMainThread)
//debug("refresh \(Date.now.formatted(date: .abbreviated, time: .standard))")

// Garbage collect terminated apps and windows before working with all windows
Expand Down
4 changes: 2 additions & 2 deletions src/tree/AeroApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ class AeroApp: Hashable {

static func ==(lhs: AeroApp, rhs: AeroApp) -> Bool {
if lhs.id == rhs.id {
precondition(lhs === rhs)
check(lhs === rhs)
return true
} else {
precondition(lhs !== rhs)
check(lhs !== rhs)
return false
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tree/TreeNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class TreeNode: Equatable {
guard let _parent else { return nil }

let index = _parent._children.remove(element: self) ?? errorT("Can't find child in its parent")
precondition(_parent._mruChildren.remove(self))
check(_parent._mruChildren.remove(self))
self._parent = nil

return BindingData(parent: _parent, adaptiveWeight: adaptiveWeight, index: index)
Expand Down
2 changes: 1 addition & 1 deletion src/tree/TreeNodeEx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ extension TreeNode {
})!
switch innermostChild.parent?.kind {
case .tilingContainer(let parent):
precondition(parent.orientation == direction.orientation)
check(parent.orientation == direction.orientation)
return (parent, innermostChild.ownIndexOrNil!)
case .workspace, nil:
return nil
Expand Down
2 changes: 1 addition & 1 deletion src/tree/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private func rearrangeWorkspacesOnMonitors() {
var preservedOldScreens: [CGPoint] = []
for newScreen in newScreens {
if let oldScreen = oldVisibleScreens.minBy({ ($0 - newScreen).vectorLength }) {
precondition(oldVisibleScreens.remove(oldScreen) != nil)
check(oldVisibleScreens.remove(oldScreen) != nil)
newScreenToOldScreenMapping[newScreen] = oldScreen
preservedOldScreens.append(oldScreen)
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ enum Ax {
key: kAXSizeAttribute,
getter: {
var raw: CGSize = .zero
precondition(AXValueGetValue($0 as! AXValue, .cgSize, &raw))
check(AXValueGetValue($0 as! AXValue, .cgSize, &raw))
return raw
},
setter: {
Expand Down Expand Up @@ -272,7 +272,7 @@ extension AXUIElement {
extension AXObserver {
private static func newImpl(_ pid: pid_t, _ handler: AXObserverCallback) -> AXObserver {
var observer: AXObserver? = nil
precondition(AXObserverCreate(pid, handler, &observer) == .success)
check(AXObserverCreate(pid, handler, &observer) == .success)
return observer!
}

Expand Down
13 changes: 13 additions & 0 deletions src/util/util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ func stringType(of some: Any) -> String {
return string
}

func check(
_ condition: Bool,
_ message: String = "",
file: String = #file,
line: Int = #line,
column: Int = #column,
function: String = #function
) {
if !condition {
error(message)
}
}

@inlinable func errorT<T>(
_ message: String = "",
file: String = #file,
Expand Down
4 changes: 2 additions & 2 deletions test/testUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func setUpWorkspacesForTests() {
focusedWorkspaceSourceOfTruth = .defaultSourceOfTruth
focusedWorkspaceName = mainMonitor.activeWorkspace.name
Workspace.garbageCollectUnusedWorkspaces()
precondition(Workspace.focused.isEffectivelyEmpty)
precondition(Workspace.focused === Workspace.all.singleOrNil(), Workspace.all.map(\.description).joined(separator: ", "))
check(Workspace.focused.isEffectivelyEmpty)
check(Workspace.focused === Workspace.all.singleOrNil(), Workspace.all.map(\.description).joined(separator: ", "))

TestApp.shared.focusedWindow = nil
TestApp.shared.windows = []
Expand Down
4 changes: 2 additions & 2 deletions test/tree/TestApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class TestApp: AeroApp {
get { _windows }
set {
if let focusedWindow {
precondition(newValue.contains(focusedWindow))
check(newValue.contains(focusedWindow))
}
_windows = newValue
}
Expand All @@ -23,7 +23,7 @@ final class TestApp: AeroApp {
get { _focusedWindow }
set {
if let window = newValue {
precondition(windows.contains(window))
check(windows.contains(window))
}
_focusedWindow = newValue
}
Expand Down

0 comments on commit bedd9e9

Please sign in to comment.