1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
extension SymbolGraph.Symbol {
/**
A mix-in that generically describes the fragments
of a symbol's declaration in a particular language, allowing for idiomatic
presentation.
For example, one can use this to drive a "code snippet"
with syntax highlighting for a function signature.
For example, the following declaration in Swift:
```swift
func foo<S: Sequence>(_ sequence: S) -> Int {
// ...
}
```
Would be represented with the following list of tokens when displaying
the declaration fragment:
```json
[
{
"kind": "keyword",
"spelling": "func"
},
{
"kind": "identifier",
"spelling": "foo"
},
{
"kind": "punctuation",
"spelling": "<"
},
{
"kind": "identifier",
"spelling": "S"
},
{
"kind": "punctuation",
"spelling": ":"
},
{
"kind": "trivia",
"spelling": " "
},
{
"kind": "typeIdentifier",
"spelling": "Sequence",
"preciseIdentifier": "$sST"
},
{
"kind": "punctuation",
"spelling": ">"
},
{
"kind": "punctuation",
"spelling": "("
},
{
"kind": "identifier",
"spelling": "_"
},
{
"kind": "trivia",
"spelling": " "
},
{
"kind": "identifier",
"spelling": "seq"
},
{
"kind": "punctuation",
"spelling": ":"
},
{
"kind": "typeIdentifier",
"spelling": "Sequence",
"preciseIdentifier": "$sST"
},
{
"kind": "punctuation",
"spelling": ":"
},
{
"kind": "identifier",
"spelling": "S"
},
{
"kind": "punctuation",
"spelling": ")"
},
{
"kind": "trivia",
"spelling": " "
},
{
"kind": "punctuation",
"spelling": "->"
},
{
"kind": "trivia",
"spelling": " "
},
{
"kind": "typeIdentifier",
"spelling": "Int",
"preciseIdentifier": "$sSi"
}
]
```
*/
public struct DeclarationFragments: Mixin, Codable {
public static let mixinKey = "declarationFragments"
/**
A list of fragments spelling out the declarations,
allowing for a presentation that's idiomatic to the source language
and cross-referencing identifiers to other symbols in the module.
> Note: These may not necessarily, literally be lexer tokens but they may be.
*/
public var declarationFragments: [Fragment]
/**
Initialize a declaration fragments mix-in with the given list of fragments.
- Parameters:
- declarationFragments: The list of fragments spelling out the declaration.
*/
public init(declarationFragments: [Fragment]) {
self.declarationFragments = declarationFragments
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
declarationFragments = try container.decode([Fragment].self)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(declarationFragments)
}
}
/// Convenience method to fetch the declaration fragment mixin value.
public var declarationFragments : [DeclarationFragments.Fragment]? {
(mixins[DeclarationFragments.mixinKey] as? DeclarationFragments)?
.declarationFragments
}
}
extension UnifiedSymbolGraph.Symbol {
/// Convenience method to fetch the selector-specific declaration fragment mixins.
public var declarationFragments: [UnifiedSymbolGraph.Selector: [SymbolGraph.Symbol.DeclarationFragments.Fragment]] {
mixins.compactMapValues({
($0[SymbolGraph.Symbol.DeclarationFragments.mixinKey] as? SymbolGraph.Symbol.DeclarationFragments)?.declarationFragments
})
}
}
|