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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 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
*/
extension SymbolGraph.Symbol {
public struct Snippet: Mixin, Codable {
enum CodingKeys: String, CodingKey {
// TODO: Remove after obsoleting Chunks.
case chunks
case language
case slices
case lines
}
public static let mixinKey = "snippet"
/// The language of the snippet if known.
public var language: String?
/// The visible lines of code of the snippet to display.
public var lines: [String]
/// Named spans of lines in the snippet.
public var slices: [String: Range<Int>]
// TODO: Remove after obsoleting Chunks.
private var _chunks = [Chunk]()
public init(language: String?, lines: [String], slices: [String: Range<Int>]) {
self.language = language
self.lines = lines
self.slices = slices
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let language = try container.decodeIfPresent(String.self, forKey: .language)
let lines = try container.decode([String].self, forKey: .lines)
let slices = try container.decodeIfPresent([String: Range<Int>].self, forKey: .slices) ?? [:]
self.init(language: language, lines: lines, slices: slices)
// TODO: Remove after obsoleting Chunks.
self._chunks = try container.decodeIfPresent([Chunk].self, forKey: .chunks) ?? []
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(language, forKey: .language)
try container.encode(lines, forKey: .lines)
if !slices.isEmpty {
try container.encode(slices, forKey: .slices)
}
if !_chunks.isEmpty {
try container.encode(_chunks, forKey: .chunks)
}
}
}
}
extension SymbolGraph.Symbol.Snippet {
public struct Chunk: Codable {
public var name: String?
public var language: String?
public var code: String
@available(*, deprecated, message: "Chunks are no longer supported. Use `Slice` instead.")
public init(name: String?, language: String?, code: String) {
self.name = name
self.language = language
self.code = code
}
}
@available(*, deprecated, message: "Chunks are no longer supported. Use `slices` instead.")
public var chunks: [Chunk] {
return _chunks
}
@available(*, deprecated, renamed: "init(slices:)")
public init(chunks: [Chunk]) {
self._chunks = chunks
self.language = chunks.first?.language
self.slices = [:]
self.lines = []
}
}
|