|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +/// Request for converting documentation for the symbol at a given location **(LSP Extension)**. |
| 14 | +/// |
| 15 | +/// This request looks up the symbol (if any) at a given text document location and returns a |
| 16 | +/// ``ConvertDocumentationResponse`` for that location. This request is primarily designed for editors |
| 17 | +/// to support live preview of Swift documentation. |
| 18 | +/// |
| 19 | +/// - Parameters: |
| 20 | +/// - textDocument: The document to render documentation for. |
| 21 | +/// - position: The document location at which to lookup symbol information. |
| 22 | +/// |
| 23 | +/// - Returns: A ``ConvertDocumentationResponse`` for the given location, which may contain an error |
| 24 | +/// message if documentation could not be converted. This error message can be displayed to the user |
| 25 | +/// in the live preview editor. |
| 26 | +/// |
| 27 | +/// ### LSP Extension |
| 28 | +/// |
| 29 | +/// This request is an extension to LSP supported by SourceKit-LSP. |
| 30 | +/// The client is expected to display the documentation in an editor using swift-docc-render. |
| 31 | +public struct ConvertDocumentationRequest: TextDocumentRequest, Hashable { |
| 32 | + public static let method: String = "textDocument/convertDocumentation" |
| 33 | + public typealias Response = ConvertDocumentationResponse |
| 34 | + |
| 35 | + /// The document in which to lookup the symbol location. |
| 36 | + public var textDocument: TextDocumentIdentifier |
| 37 | + |
| 38 | + /// The document location at which to lookup symbol information. |
| 39 | + public var position: Position |
| 40 | + |
| 41 | + public init(textDocument: TextDocumentIdentifier, position: Position) { |
| 42 | + self.textDocument = textDocument |
| 43 | + self.position = position |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +public enum ConvertDocumentationResponse: ResponseType { |
| 48 | + case renderNode(String) |
| 49 | + case error(ConvertDocumentationError) |
| 50 | +} |
| 51 | + |
| 52 | +public enum ConvertDocumentationError: ResponseType, Equatable { |
| 53 | + case indexNotAvailable |
| 54 | + case noDocumentation |
| 55 | + case symbolNotFound(String) |
| 56 | + |
| 57 | + var message: String { |
| 58 | + switch self { |
| 59 | + case .indexNotAvailable: |
| 60 | + return "The index is not availble to complete the request" |
| 61 | + case .noDocumentation: |
| 62 | + return "No documentation could be rendered for the position in this document" |
| 63 | + case .symbolNotFound(let symbolName): |
| 64 | + return "Could not find symbol \(symbolName) in the project" |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +extension ConvertDocumentationError: Codable { |
| 70 | + enum CodingKeys: String, CodingKey { |
| 71 | + case kind |
| 72 | + case message |
| 73 | + case symbolName |
| 74 | + } |
| 75 | + |
| 76 | + public init(from decoder: Decoder) throws { |
| 77 | + let values = try decoder.container(keyedBy: CodingKeys.self) |
| 78 | + let kind = try values.decode(String.self, forKey: .kind) |
| 79 | + switch kind { |
| 80 | + case "indexNotAvailable": |
| 81 | + self = .indexNotAvailable |
| 82 | + case "noDocumentation": |
| 83 | + self = .noDocumentation |
| 84 | + case "symbolNotFound": |
| 85 | + let symbolName = try values.decode(String.self, forKey: .symbolName) |
| 86 | + self = .symbolNotFound(symbolName) |
| 87 | + default: |
| 88 | + throw DecodingError.dataCorruptedError( |
| 89 | + forKey: CodingKeys.kind, |
| 90 | + in: values, |
| 91 | + debugDescription: "Invalid error kind: \(kind)" |
| 92 | + ) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public func encode(to encoder: any Encoder) throws { |
| 97 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 98 | + switch self { |
| 99 | + case .indexNotAvailable: |
| 100 | + try container.encode("indexNotAvailable", forKey: .kind) |
| 101 | + case .noDocumentation: |
| 102 | + try container.encode("noDocumentation", forKey: .kind) |
| 103 | + case .symbolNotFound(let symbolName): |
| 104 | + try container.encode("symbolNotFound", forKey: .kind) |
| 105 | + try container.encode(symbolName, forKey: .symbolName) |
| 106 | + } |
| 107 | + try container.encode(message, forKey: .message) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +extension ConvertDocumentationResponse: Codable { |
| 112 | + enum CodingKeys: String, CodingKey { |
| 113 | + case type |
| 114 | + case renderNode |
| 115 | + case error |
| 116 | + } |
| 117 | + |
| 118 | + public init(from decoder: Decoder) throws { |
| 119 | + let values = try decoder.container(keyedBy: CodingKeys.self) |
| 120 | + let type = try values.decode(String.self, forKey: .type) |
| 121 | + switch type { |
| 122 | + case "renderNode": |
| 123 | + let renderNode = try values.decode(String.self, forKey: .renderNode) |
| 124 | + self = .renderNode(renderNode) |
| 125 | + case "error": |
| 126 | + let error = try values.decode(ConvertDocumentationError.self, forKey: .error) |
| 127 | + self = .error(error) |
| 128 | + default: |
| 129 | + throw DecodingError.dataCorruptedError( |
| 130 | + forKey: CodingKeys.type, |
| 131 | + in: values, |
| 132 | + debugDescription: "Invalid type: \(type)" |
| 133 | + ) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public func encode(to encoder: any Encoder) throws { |
| 138 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 139 | + switch self { |
| 140 | + case .renderNode(let renderNode): |
| 141 | + try container.encode("renderNode", forKey: .type) |
| 142 | + try container.encode(renderNode, forKey: .renderNode) |
| 143 | + case .error(let error): |
| 144 | + try container.encode("error", forKey: .type) |
| 145 | + try container.encode(error, forKey: .error) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments