|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +/// A collection of API for link completion. |
| 14 | +/// |
| 15 | +/// An example link completion workflow could look something like this; |
| 16 | +/// Assume that there's already an partial link in progress: `First/Second-enum/` |
| 17 | +/// |
| 18 | +/// - First, parse the link into link components using ``parse(linkString:)``. |
| 19 | +/// - Second, narrow down the possible symbols to suggest as completion using ``SymbolInformation/matches(_:)`` |
| 20 | +/// - Third, determine the minimal unique disambiguation for each completion suggestion using ``suggestedDisambiguation(forCollidingSymbols:)`` |
| 21 | +/// |
| 22 | +/// > Tip: You can use ``SymbolInformation/hash(uniqueSymbolID:)`` to compute the hashed symbol identifiers needed for steps 2 and 3 above. |
| 23 | +@_spi(LinkCompletion) // LinkCompletionTools isn't stable API yet |
| 24 | +public enum LinkCompletionTools { |
| 25 | + |
| 26 | + // MARK: Parsing |
| 27 | + |
| 28 | + /// Parses link string into link components; each consisting of a base name and a disambiguation suffix. |
| 29 | + /// |
| 30 | + /// - Parameter linkString: The link string to parse. |
| 31 | + /// - Returns: A list of link components, each consisting of a base name and a disambiguation suffix. |
| 32 | + public static func parse(linkString: String) -> [(name: String, disambiguation: ParsedDisambiguation)] { |
| 33 | + PathHierarchy.PathParser.parse(path: linkString).components.map { pathComponent in |
| 34 | + (name: String(pathComponent.name), disambiguation: ParsedDisambiguation(pathComponent.disambiguation) ) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// A disambiguation suffix for a parsed link component. |
| 39 | + public enum ParsedDisambiguation: Equatable { |
| 40 | + /// This link component isn't disambiguated. |
| 41 | + case none |
| 42 | + |
| 43 | + /// This path component uses a combination of kind and hash disambiguation. |
| 44 | + /// |
| 45 | + /// At least one of `kind` and `hash` will be non-`nil`. |
| 46 | + /// It's never _necessary_ to specify both a `kind` and a `hash` to disambiguate a link component, but it's supported for the developer to include both. |
| 47 | + case kindAndOrHash(kind: String?, hash: String?) |
| 48 | + |
| 49 | + /// This path component uses type signature information for disambiguation. |
| 50 | + /// |
| 51 | + /// At least one of `parameterTypes` and `returnTypes` will be non-`nil`. |
| 52 | + case typeSignature(parameterTypes: [String]?, returnTypes: [String]?) |
| 53 | + |
| 54 | + // This empty-marker case is here because non-frozen enums are only available when Library Evolution is enabled, |
| 55 | + // which is not available to Swift Packages without unsafe flags (rdar://78773361). |
| 56 | + // This can be removed once that is available and applied to Swift-DocC (rdar://89033233). |
| 57 | + @available(*, deprecated, message: "this enum is non-frozen and may be expanded in the future; add a `default` case instead of matching this one") |
| 58 | + case _nonFrozenEnum_useDefaultCase |
| 59 | + |
| 60 | + init(_ disambiguation: PathHierarchy.PathComponent.Disambiguation?) { |
| 61 | + // This initializer is intended to be internal-only. |
| 62 | + switch disambiguation { |
| 63 | + case .kindAndHash(let kind, let hash): |
| 64 | + self = .kindAndOrHash( |
| 65 | + kind: kind.map { String($0) }, |
| 66 | + hash: hash.map { String($0) } |
| 67 | + ) |
| 68 | + case .typeSignature(let parameterTypes, let returnTypes): |
| 69 | + self = .typeSignature( |
| 70 | + parameterTypes: parameterTypes?.map { String($0) }, |
| 71 | + returnTypes: returnTypes?.map { String($0) } |
| 72 | + ) |
| 73 | + case nil: |
| 74 | + self = .none |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Suggests the minimal most readable disambiguation string for each symbol with the same name. |
| 80 | + /// - Parameters: |
| 81 | + /// - collidingSymbols: A list of symbols that all have the same name. |
| 82 | + /// - Returns: A collection of disambiguation strings in the same order as the provided symbol information. |
| 83 | + /// |
| 84 | + /// - Important: It's the callers responsibility to create symbol information that matches what the compilers emit in symbol graph files. |
| 85 | + /// If there are mismatches, DocC may suggest disambiguation that won't resolve with the real compiler emitted symbol data. |
| 86 | + public static func suggestedDisambiguation(forCollidingSymbols collidingSymbols: [SymbolInformation]) -> [String] { |
| 87 | + // Track the order of the symbols so that the disambiguations can be ordered to align with their respective symbols. |
| 88 | + var identifiersInOrder: [ResolvedIdentifier] = [] |
| 89 | + identifiersInOrder.reserveCapacity(collidingSymbols.count) |
| 90 | + |
| 91 | + // Construct a disambiguation container with all the symbol's information. |
| 92 | + var disambiguationContainer = PathHierarchy.DisambiguationContainer() |
| 93 | + for symbol in collidingSymbols { |
| 94 | + let (node, identifier) = Self._makeNodeAndIdentifier(name: "unused") |
| 95 | + identifiersInOrder.append(identifier) |
| 96 | + |
| 97 | + disambiguationContainer.add( |
| 98 | + node, |
| 99 | + kind: symbol.kind, |
| 100 | + hash: symbol.symbolIDHash, |
| 101 | + parameterTypes: symbol.parameterTypes, |
| 102 | + returnTypes: symbol.returnTypes |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + let disambiguatedValues = disambiguationContainer.disambiguatedValues() |
| 107 | + // Compute the minimal suggested disambiguation for each symbol and return their string suffixes in the original symbol's order. |
| 108 | + return identifiersInOrder.map { identifier in |
| 109 | + guard let (_, disambiguation) = disambiguatedValues.first(where: { $0.value.identifier == identifier }) else { |
| 110 | + fatalError("Each node in the `DisambiguationContainer` should always have a entry in the `disambiguatedValues`") |
| 111 | + } |
| 112 | + return disambiguation.makeSuffix() |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /// Information about a symbol for link completion purposes. |
| 117 | + /// |
| 118 | + /// > Note: |
| 119 | + /// > This symbol information doesn't include the name. |
| 120 | + /// > It's the callers responsibility to group symbols by their name. |
| 121 | + /// |
| 122 | + /// > Important: |
| 123 | + /// > It's the callers responsibility to create symbol information that matches what the compilers emit in symbol graph files. |
| 124 | + /// > If there are mismatches, DocC may suggest disambiguation that won't resolve with the real compiler emitted symbol data. |
| 125 | + public struct SymbolInformation { |
| 126 | + /// The kind of symbol, for example `"class"` or `"func.op`. |
| 127 | + /// |
| 128 | + /// ## See Also |
| 129 | + /// - ``/SymbolKit/SymbolGraph/Symbol/KindIdentifier`` |
| 130 | + public var kind: String |
| 131 | + /// A hash of the symbol's unique identifier. |
| 132 | + /// |
| 133 | + /// ## See Also |
| 134 | + /// - ``hash(uniqueSymbolID:)`` |
| 135 | + public var symbolIDHash: String |
| 136 | + /// The type names of this symbol's parameters, or `nil` if this symbol has no function signature information. |
| 137 | + /// |
| 138 | + /// A function without parameters represents i |
| 139 | + public var parameterTypes: [String]? |
| 140 | + /// The type names of this symbol's return value, or `nil` if this symbol has no function signature information. |
| 141 | + public var returnTypes: [String]? |
| 142 | + |
| 143 | + public init( |
| 144 | + kind: String, |
| 145 | + symbolIDHash: String, |
| 146 | + parameterTypes: [String]? = nil, |
| 147 | + returnTypes: [String]? = nil |
| 148 | + ) { |
| 149 | + self.kind = kind |
| 150 | + self.symbolIDHash = symbolIDHash |
| 151 | + self.parameterTypes = parameterTypes |
| 152 | + self.returnTypes = returnTypes |
| 153 | + } |
| 154 | + |
| 155 | + /// Creates a hashed representation of a symbol's unique identifier. |
| 156 | + /// |
| 157 | + /// # See Also |
| 158 | + /// - ``symbolIDHash`` |
| 159 | + public static func hash(uniqueSymbolID: String) -> String { |
| 160 | + uniqueSymbolID.stableHashString |
| 161 | + } |
| 162 | + |
| 163 | + // MARK: Filtering |
| 164 | + |
| 165 | + /// Returns a Boolean value that indicates whether this symbol information matches the parsed disambiguation from one of the link components of a parsed link string. |
| 166 | + public func matches(_ parsedDisambiguation: LinkCompletionTools.ParsedDisambiguation) -> Bool { |
| 167 | + guard let disambiguation = PathHierarchy.PathComponent.Disambiguation(parsedDisambiguation) else { |
| 168 | + return true // No disambiguation to match against. |
| 169 | + } |
| 170 | + |
| 171 | + var disambiguationContainer = PathHierarchy.DisambiguationContainer() |
| 172 | + let (node, _) = LinkCompletionTools._makeNodeAndIdentifier(name: "unused") |
| 173 | + |
| 174 | + disambiguationContainer.add( |
| 175 | + node, |
| 176 | + kind: self.kind, |
| 177 | + hash: self.symbolIDHash, |
| 178 | + parameterTypes: self.parameterTypes, |
| 179 | + returnTypes: self.returnTypes |
| 180 | + ) |
| 181 | + |
| 182 | + do { |
| 183 | + return try disambiguationContainer.find(disambiguation) != nil |
| 184 | + } catch { |
| 185 | + return false |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +private extension PathHierarchy.PathComponent.Disambiguation { |
| 192 | + init?(_ parsedDisambiguation: LinkCompletionTools.ParsedDisambiguation) { |
| 193 | + switch parsedDisambiguation { |
| 194 | + case .kindAndOrHash(let kind, let hash): |
| 195 | + self = .kindAndHash(kind: kind.map { $0[...] }, hash: hash.map { $0[...] }) |
| 196 | + |
| 197 | + case .typeSignature(let parameterTypes, let returnTypes): |
| 198 | + self = .typeSignature(parameterTypes: parameterTypes?.map { $0[...] }, returnTypes: returnTypes?.map { $0[...] }) |
| 199 | + |
| 200 | + // Since this is within DocC we want to have an error if we don't handle new future cases. |
| 201 | + case .none, ._nonFrozenEnum_useDefaultCase: |
| 202 | + return nil |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments