import Foundation import SwiftUI /// Where a name a file uses is defined, from `repo definitions` /// (krz/gitbay#333): the first definition, and how many there are. A name defined /// once links to its line; one defined more often to a search. nonisolated struct Definition: Decodable, Sendable, Hashable { let name: String let count: Int let path: String let line: Int private static let scheme = "gitbay-definition" /// The index's names, as the server matches them. nonisolated(unsafe) private static let identifier = /[\p{L}_$][\p{L}\p{N}_$]*/ /// `line` with each defined name marked as a link the file view opens. /// The highlighting stays; names the index does not define stay plain. static func link(_ line: AttributedString, definitions: [String: Definition]) -> AttributedString { guard !definitions.isEmpty else { return line } var out = line let text = String(line.characters) for match in text.matches(of: identifier) { let name = String(match.output) guard definitions[name] != nil, let lower = AttributedString.Index(match.range.lowerBound, within: out), let upper = AttributedString.Index(match.range.upperBound, within: out) else { continue } var components = URLComponents() components.scheme = scheme components.host = "name" components.path = "/" + name out[lower.. String? { guard url.scheme == scheme else { return nil } let path = url.path(percentEncoded: false) return path.hasPrefix("/") ? String(path.dropFirst()) : nil } }