Skip to content

Commit

Permalink
Refactor resolving environment variables logic to enhance naming
Browse files Browse the repository at this point in the history
To align with clearer terminology and better reflect the action being performed within the codebase, the method previously named `shouldResolveDocumentAndSelections()` has been refactored to `resolveUserEnvironment()`. This change underscores the function's responsibility to resolve more than just document and selection variables, encompassing the broader set of user environment details. The modification affects key areas in the application, including controllers, models, and runners, ensuring consistency throughout the code.

This adjustment aims to make the code more intuitive and improve maintainability by using more descriptive names that accurately represent the underlying processes. It forms part of our continual effort to enhance code clarity and quality.
  • Loading branch information
zenangst committed Dec 12, 2023
1 parent a52383f commit 433d93b
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ final class ApplicationTriggerController {
commandRunner.concurrentRun(
commands,
checkCancellation: true,
resolveDocumentAndSelections: workflow.shouldResolveDocumentAndSelections()
resolveUserEnvironment: workflow.resolveUserEnvironment()
)
case .serial:
commandRunner.serialRun(
commands,
checkCancellation: true,
resolveDocumentAndSelections: workflow.shouldResolveDocumentAndSelections()
resolveUserEnvironment: workflow.resolveUserEnvironment()
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion App/Sources/Core/Models/Workflow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ extension Workflow {
}

extension Workflow {
func shouldResolveDocumentAndSelections() -> Bool {
func resolveUserEnvironment() -> Bool {
var result: Bool = false
let keywords = UserSpace.EnvironmentKey.allCases
.map(\.asTextVariable)
Expand Down
15 changes: 6 additions & 9 deletions App/Sources/Core/Runners/CommandRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import Combine
import MachPort

protocol CommandRunning {
func serialRun(_ commands: [Command], checkCancellation: Bool, resolveDocumentAndSelections: Bool)
func concurrentRun(_ commands: [Command], checkCancellation: Bool, resolveDocumentAndSelections: Bool)
func serialRun(_ commands: [Command], checkCancellation: Bool, resolveUserEnvironment: Bool)
func concurrentRun(_ commands: [Command], checkCancellation: Bool, resolveUserEnvironment: Bool)
}

final class CommandRunner: CommandRunning, @unchecked Sendable {
Expand Down Expand Up @@ -101,7 +101,7 @@ final class CommandRunner: CommandRunning, @unchecked Sendable {
func serialRun(
_ commands: [Command],
checkCancellation: Bool,
resolveDocumentAndSelections: Bool
resolveUserEnvironment: Bool
) {
serialTask?.cancel()
serialTask = Task.detached(priority: .userInitiated) { [weak self] in
Expand All @@ -115,7 +115,7 @@ final class CommandRunner: CommandRunning, @unchecked Sendable {
})

if shouldDismissMissionControl { await missionControl.dismissIfActive() }
let snapshot = await UserSpace.shared.snapshot(resolvedDocumentAndSelections: resolveDocumentAndSelections)
let snapshot = await UserSpace.shared.snapshot(resolveUserEnvironment: resolveUserEnvironment)
for command in commands {
if checkCancellation { try Task.checkCancellation() }
do {
Expand All @@ -129,10 +129,7 @@ final class CommandRunner: CommandRunning, @unchecked Sendable {
}
}

func concurrentRun(
_ commands: [Command],
checkCancellation: Bool,
resolveDocumentAndSelections: Bool
func concurrentRun(_ commands: [Command], checkCancellation: Bool, resolveUserEnvironment: Bool
) {
concurrentTask?.cancel()
concurrentTask = Task.detached(priority: .userInitiated) { [weak self] in
Expand All @@ -146,7 +143,7 @@ final class CommandRunner: CommandRunning, @unchecked Sendable {

if shouldDismissMissionControl { await missionControl.dismissIfActive() }

let snapshot = await UserSpace.shared.snapshot(resolvedDocumentAndSelections: resolveDocumentAndSelections)
let snapshot = await UserSpace.shared.snapshot(resolveUserEnvironment: resolveUserEnvironment)
for command in commands {
do {
if checkCancellation { try Task.checkCancellation() }
Expand Down
15 changes: 5 additions & 10 deletions App/Sources/Core/Runners/MachPortCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -305,20 +305,15 @@ final class MachPortCoordinator {
}
}

let shouldResolveDocumentAndSelections = workflow.shouldResolveDocumentAndSelections()
let resolveUserEnvironment = workflow.resolveUserEnvironment()
switch workflow.execution {
case .concurrent:
commandRunner.concurrentRun(
commands,
checkCancellation: checkCancellation,
resolveDocumentAndSelections: shouldResolveDocumentAndSelections
commandRunner.concurrentRun(commands, checkCancellation: checkCancellation,
resolveUserEnvironment: resolveUserEnvironment
)
case .serial:
commandRunner.serialRun(
commands,
checkCancellation: checkCancellation,
resolveDocumentAndSelections: shouldResolveDocumentAndSelections
)
commandRunner.serialRun(commands, checkCancellation: checkCancellation,
resolveUserEnvironment: resolveUserEnvironment)
}
}

Expand Down
8 changes: 4 additions & 4 deletions App/Sources/Core/UserSpace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ final class UserSpace: Sendable {
#endif

@MainActor
func snapshot(resolvedDocumentAndSelections: Bool) async -> Snapshot {
Benchmark.shared.start("snapshot: \(resolvedDocumentAndSelections)")
defer { Benchmark.shared.stop("snapshot: \(resolvedDocumentAndSelections)") }
func snapshot(resolveUserEnvironment: Bool) async -> Snapshot {
Benchmark.shared.start("snapshot: \(resolveUserEnvironment)")
defer { Benchmark.shared.stop("snapshot: \(resolveUserEnvironment)") }
var selections = [String]()
var documentPath: String?
var selectedText: String = ""

if resolvedDocumentAndSelections,
if resolveUserEnvironment,
let frontmostApplication = try? frontmostApplication() {
if let documentPathFromAX = try? self.documentPath(for: frontmostApplication) {
documentPath = documentPathFromAX
Expand Down
6 changes: 2 additions & 4 deletions App/Sources/UI/Reducers/DetailCommandActionReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class DetailCommandActionReducer {
let runCommand = command
Task {
do {
try await commandRunner.run(runCommand, snapshot: UserSpace.shared.snapshot(resolvedDocumentAndSelections: false))
try await commandRunner.run(runCommand, snapshot: UserSpace.shared.snapshot(resolveUserEnvironment: false))
} catch let error as KeyboardCommandRunnerError {
let alert = await NSAlert(error: error)
await alert.runModal()
Expand Down Expand Up @@ -161,9 +161,7 @@ final class DetailCommandActionReducer {
let path = (source as NSString).expandingTildeInPath
try await commandRunner.run(
.open(.init(path: path)),
snapshot: UserSpace.shared.snapshot(
resolvedDocumentAndSelections: false
)
snapshot: UserSpace.shared.snapshot(resolveUserEnvironment: false)
)
}
case .reveal(let path):
Expand Down
4 changes: 2 additions & 2 deletions App/Sources/UI/Reducers/DetailViewActionReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ final class DetailViewActionReducer {
let commands = workflow.commands.filter(\.isEnabled)
switch workflow.execution {
case .concurrent:
commandRunner.concurrentRun(commands, checkCancellation: true, resolveDocumentAndSelections: true)
commandRunner.concurrentRun(commands, checkCancellation: true, resolveUserEnvironment: true)
case .serial:
commandRunner.serialRun(commands, checkCancellation: true, resolveDocumentAndSelections: true)
commandRunner.serialRun(commands, checkCancellation: true, resolveUserEnvironment: true)
}
return .none
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ private final class CommandRunner: CommandRunning {
self.serialRunHandler = serial
}

func concurrentRun(_ commands: [Command], checkCancellation: Bool, resolveDocumentAndSelections: Bool) {
func concurrentRun(_ commands: [Command], checkCancellation: Bool, resolveUserEnvironment: Bool) {
concurrentRunHandler(commands)
}

func serialRun(_ commands: [Command], checkCancellation: Bool, resolveDocumentAndSelections: Bool) {
func serialRun(_ commands: [Command], checkCancellation: Bool, resolveUserEnvironment: Bool) {
serialRunHandler(commands)
}
}
Expand Down
Loading

0 comments on commit 433d93b

Please sign in to comment.