|
| 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 that generates documentation for a symbol at a given cursor location **(LSP Extension)**. |
| 14 | +/// |
| 15 | +/// Primarily designed to support live preview of Swift documentation in editors. |
| 16 | +/// |
| 17 | +/// This request looks up the nearest documentable symbol (if any) at a given cursor location within |
| 18 | +/// a text document and returns a `DoccDocumentationResponse`. The response contains a string |
| 19 | +/// representing single JSON encoded DocC RenderNode. This RenderNode can then be rendered in an |
| 20 | +/// editor via https://github.com/swiftlang/swift-docc-render. |
| 21 | +/// |
| 22 | +/// The position may be ommitted for documentation within DocC markdown and tutorial files as they |
| 23 | +/// represent a single documentation page. It is only required for generating documentation within |
| 24 | +/// Swift files as they usually contain multiple documentable symbols. |
| 25 | +/// |
| 26 | +/// Documentation can fail to be generated for a number of reasons. The most common of which being |
| 27 | +/// that no documentable symbol could be found. In such cases the request will fail with a request |
| 28 | +/// failed LSP error code (-32803) that contains a human-readable error message. This error message can |
| 29 | +/// be displayed within the live preview editor to indicate that something has gone wrong. |
| 30 | +/// |
| 31 | +/// At the moment this request is only available on macOS and Linux. SourceKit-LSP will advertise |
| 32 | +/// `textDocument/doccDocumentation` in its experimental server capabilities if it supports it. |
| 33 | +/// |
| 34 | +/// - Parameters: |
| 35 | +/// - textDocument: The document to generate documentation for. |
| 36 | +/// - position: The cursor position within the document. (optional) |
| 37 | +/// |
| 38 | +/// - Returns: A `DoccDocumentationResponse` for the given location, which may contain an error |
| 39 | +/// message if documentation could not be converted. This error message can be displayed to the user |
| 40 | +/// in the live preview editor. |
| 41 | +/// |
| 42 | +/// ### LSP Extension |
| 43 | +/// |
| 44 | +/// This request is an extension to LSP supported by SourceKit-LSP. |
| 45 | +/// The client is expected to display the documentation in an editor using swift-docc-render. |
| 46 | +public struct DoccDocumentationRequest: TextDocumentRequest, Hashable { |
| 47 | + public static let method: String = "textDocument/doccDocumentation" |
| 48 | + public typealias Response = DoccDocumentationResponse |
| 49 | + |
| 50 | + /// The document in which to lookup the symbol location. |
| 51 | + public var textDocument: TextDocumentIdentifier |
| 52 | + |
| 53 | + /// The document location at which to lookup symbol information. |
| 54 | + public var position: Position? |
| 55 | + |
| 56 | + public init(textDocument: TextDocumentIdentifier, position: Position? = nil) { |
| 57 | + self.textDocument = textDocument |
| 58 | + self.position = position |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +public struct DoccDocumentationResponse: ResponseType, Equatable { |
| 63 | + public var renderNode: String |
| 64 | + |
| 65 | + public init(renderNode: String) { |
| 66 | + self.renderNode = renderNode |
| 67 | + } |
| 68 | +} |
0 commit comments