import Foundation import Observation public enum MacDestination: Hashable, Sendable { case pane(Destination.Pane), task(String), project(String) } public struct MacJumpResult: Identifiable, Sendable { public let target: MacDestination public let title: String public let subtitle: String public let needsYou: Bool let search: String var brief = "" public var id: MacDestination { target } } /// Local, bounded by the same task window as the sidebar. No remote search /// or text from a task thread is implied by a match. public struct MacJumpIndex { private var entries: [MacJumpResult] = [] public init(tasks: [ThinTask], needs: [ThinTask], projects: [ThinProject]) { let needsIDs = Set(needs.map(\.id)) var latest: [String: ThinTask] = [:] for task in needs + tasks where task.revision >= (latest[task.id]?.revision ?? -1) { latest[task.id] = task } var seen = Set() for entry in needs + tasks where seen.insert(entry.id).inserted { let task = latest[entry.id] ?? entry entries.append(.init(target: .task(task.id), title: task.title, subtitle: task.project, needsYou: needsIDs.contains(task.id), search: [task.project, task.repo ?? "", task.branch].joined(separator: " "), brief: task.brief)) } for project in projects where project.state == "active" { entries.append(.init(target: .project(project.id), title: project.displayName, subtitle: project.repo, needsYou: false, search: [project.displayName, project.repo].joined(separator: " "))) } for (pane, key) in [(Destination.Pane.conversation, "tab.conversation"), (.needs, "tab.needs"), (.tasks, "tab.tasks"), (.projects, "nav.projects")] { let title = EquipoStrings.text(.init(key)) entries.append(.init(target: .pane(pane), title: title, subtitle: "", needsYou: false, search: title)) } } public func results(_ query: String) -> [MacJumpResult] { entries.enumerated().compactMap { index, entry -> (Int, Int, MacJumpResult)? in guard let score = Self.rank(query, entry: entry) else { return nil } return (score, index, entry) }.sorted { $0.0 == $1.0 ? $0.1 < $1.1 : $0.0 > $1.0 }.map(\.2) } private static func folded(_ text: String) -> String { text.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current) } private static func words(_ text: String) -> [String] { folded(text).split { !$0.isLetter && !$0.isNumber }.map(String.init) } private static func rank(_ query: String, entry: MacJumpResult) -> Int? { let query = folded(query.trimmingCharacters(in: .whitespacesAndNewlines)) if query.isEmpty { return 0 } let queryWords = words(query), titleWords = words(entry.title) if !queryWords.isEmpty, queryWords.allSatisfy({ q in titleWords.contains { $0.hasPrefix(q) } }) { return 50000 } if let titleScore = score(query, in: entry.title) { return 30000 + max(0, titleScore) } if let otherScore = score(query, in: entry.search) { return 10000 + max(0, otherScore) } let briefWords = Set(words(entry.brief)) if query.count >= 2, !queryWords.isEmpty, queryWords.allSatisfy({ briefWords.contains($0) }) { return 100 } return nil } public static func score(_ query: String, in text: String) -> Int? { let query = query.trimmingCharacters(in: .whitespacesAndNewlines).folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current) let text = text.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current) if query.isEmpty { return 0 } if text.hasPrefix(query) { return 10000 } if text.contains(query) { return 5000 } var cursor = text.startIndex, gaps = 0 for character in query { guard let next = text[cursor...].firstIndex(of: character) else { return nil } gaps += text.distance(from: cursor, to: next) cursor = text.index(after: next) } return 1000 - gaps } } @MainActor @Observable public final class MacNavigationHistory { private var entries: [MacDestination] = [.pane(.conversation)] private var index = 0 public init() {} public var current: MacDestination { entries[index] } public var pane: Destination.Pane { for entry in entries.prefix(index + 1).reversed() { switch entry { case .pane(let pane): return pane; case .project: return .projects; case .task: continue } } return .conversation } public var canBack: Bool { index > 0 } public var canForward: Bool { index + 1 < entries.count } public func go(_ destination: MacDestination) { // The inspector persists independently of pane navigation. if case .task = destination { return } guard current != destination else { return } entries = Array(entries.prefix(index + 1)); entries.append(destination); index += 1 } @discardableResult public func back() -> MacDestination? { guard canBack else { return nil }; index -= 1; return current } @discardableResult public func forward() -> MacDestination? { guard canForward else { return nil }; index += 1; return current } }