| 1 | import Foundation |
| 2 | import Observation |
| 3 | |
| 4 | public enum MacDestination: Hashable, Sendable { |
| 5 | case pane(Destination.Pane), task(String), project(String) |
| 6 | } |
| 7 | |
| 8 | public struct MacJumpResult: Identifiable, Sendable { |
| 9 | public let target: MacDestination |
| 10 | public let title: String |
| 11 | public let subtitle: String |
| 12 | public let needsYou: Bool |
| 13 | let search: String |
| 14 | var brief = "" |
| 15 | public var id: MacDestination { target } |
| 16 | } |
| 17 | |
| 18 | /// Local, bounded by the same task window as the sidebar. No remote search |
| 19 | /// or text from a task thread is implied by a match. |
| 20 | public struct MacJumpIndex { |
| 21 | private var entries: [MacJumpResult] = [] |
| 22 | public init(tasks: [ThinTask], needs: [ThinTask], projects: [ThinProject]) { |
| 23 | let needsIDs = Set(needs.map(\.id)) |
| 24 | var latest: [String: ThinTask] = [:] |
| 25 | for task in needs + tasks where task.revision >= (latest[task.id]?.revision ?? -1) { latest[task.id] = task } |
| 26 | var seen = Set<String>() |
| 27 | for entry in needs + tasks where seen.insert(entry.id).inserted { |
| 28 | let task = latest[entry.id] ?? entry |
| 29 | entries.append(.init(target: .task(task.id), title: task.title, subtitle: task.project, |
| 30 | needsYou: needsIDs.contains(task.id), |
| 31 | search: [task.project, task.repo ?? "", task.branch].joined(separator: " "), brief: task.brief)) |
| 32 | } |
| 33 | for project in projects where project.state == "active" { |
| 34 | entries.append(.init(target: .project(project.id), title: project.displayName, subtitle: project.repo, |
| 35 | needsYou: false, search: [project.displayName, project.repo].joined(separator: " "))) |
| 36 | } |
| 37 | for (pane, key) in [(Destination.Pane.conversation, "tab.conversation"), (.needs, "tab.needs"), (.tasks, "tab.tasks"), (.projects, "nav.projects")] { |
| 38 | let title = EquipoStrings.text(.init(key)) |
| 39 | entries.append(.init(target: .pane(pane), title: title, subtitle: "", needsYou: false, search: title)) |
| 40 | } |
| 41 | } |
| 42 | public func results(_ query: String) -> [MacJumpResult] { |
| 43 | entries.enumerated().compactMap { index, entry -> (Int, Int, MacJumpResult)? in |
| 44 | guard let score = Self.rank(query, entry: entry) else { return nil } |
| 45 | return (score, index, entry) |
| 46 | }.sorted { $0.0 == $1.0 ? $0.1 < $1.1 : $0.0 > $1.0 }.map(\.2) |
| 47 | } |
| 48 | private static func folded(_ text: String) -> String { |
| 49 | text.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current) |
| 50 | } |
| 51 | private static func words(_ text: String) -> [String] { |
| 52 | folded(text).split { !$0.isLetter && !$0.isNumber }.map(String.init) |
| 53 | } |
| 54 | private static func rank(_ query: String, entry: MacJumpResult) -> Int? { |
| 55 | let query = folded(query.trimmingCharacters(in: .whitespacesAndNewlines)) |
| 56 | if query.isEmpty { return 0 } |
| 57 | let queryWords = words(query), titleWords = words(entry.title) |
| 58 | if !queryWords.isEmpty, queryWords.allSatisfy({ q in titleWords.contains { $0.hasPrefix(q) } }) { return 50000 } |
| 59 | if let titleScore = score(query, in: entry.title) { return 30000 + max(0, titleScore) } |
| 60 | if let otherScore = score(query, in: entry.search) { return 10000 + max(0, otherScore) } |
| 61 | let briefWords = Set(words(entry.brief)) |
| 62 | if query.count >= 2, !queryWords.isEmpty, queryWords.allSatisfy({ briefWords.contains($0) }) { return 100 } |
| 63 | return nil |
| 64 | } |
| 65 | public static func score(_ query: String, in text: String) -> Int? { |
| 66 | let query = query.trimmingCharacters(in: .whitespacesAndNewlines).folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current) |
| 67 | let text = text.folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current) |
| 68 | if query.isEmpty { return 0 } |
| 69 | if text.hasPrefix(query) { return 10000 } |
| 70 | if text.contains(query) { return 5000 } |
| 71 | var cursor = text.startIndex, gaps = 0 |
| 72 | for character in query { |
| 73 | guard let next = text[cursor...].firstIndex(of: character) else { return nil } |
| 74 | gaps += text.distance(from: cursor, to: next) |
| 75 | cursor = text.index(after: next) |
| 76 | } |
| 77 | return 1000 - gaps |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @MainActor @Observable public final class MacNavigationHistory { |
| 82 | private var entries: [MacDestination] = [.pane(.conversation)] |
| 83 | private var index = 0 |
| 84 | public init() {} |
| 85 | public var current: MacDestination { entries[index] } |
| 86 | public var pane: Destination.Pane { |
| 87 | for entry in entries.prefix(index + 1).reversed() { |
| 88 | switch entry { case .pane(let pane): return pane; case .project: return .projects; case .task: continue } |
| 89 | } |
| 90 | return .conversation |
| 91 | } |
| 92 | public var canBack: Bool { index > 0 } |
| 93 | public var canForward: Bool { index + 1 < entries.count } |
| 94 | public func go(_ destination: MacDestination) { |
| 95 | // The inspector persists independently of pane navigation. |
| 96 | if case .task = destination { return } |
| 97 | guard current != destination else { return } |
| 98 | entries = Array(entries.prefix(index + 1)); entries.append(destination); index += 1 |
| 99 | } |
| 100 | @discardableResult public func back() -> MacDestination? { guard canBack else { return nil }; index -= 1; return current } |
| 101 | @discardableResult public func forward() -> MacDestination? { guard canForward else { return nil }; index += 1; return current } |
| 102 | } |
| 103 | |