|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2023 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 parsed Doxygen `\param` command. |
| 14 | +/// |
| 15 | +/// The Doxygen support in Swift-Markdown parses `\param` commands of the form |
| 16 | +/// `\param name description`, where `description` extends until the next blank line or the next |
| 17 | +/// parsed command. For example, the following input will return two `DoxygenParam` instances: |
| 18 | +/// |
| 19 | +/// ```markdown |
| 20 | +/// \param coordinate The coordinate used to center the transformation. |
| 21 | +/// \param matrix The transformation matrix that describes the transformation. |
| 22 | +/// For more information about transformation matrices, refer to the Transformation |
| 23 | +/// documentation. |
| 24 | +/// ``` |
| 25 | +public struct DoxygenParameter: BlockContainer { |
| 26 | + public var _data: _MarkupData |
| 27 | + |
| 28 | + init(_ raw: RawMarkup) throws { |
| 29 | + guard case .doxygenParam = raw.data else { |
| 30 | + throw RawMarkup.Error.concreteConversionError(from: raw, to: DoxygenParameter.self) |
| 31 | + } |
| 32 | + let absoluteRaw = AbsoluteRawMarkup(markup: raw, metadata: MarkupMetadata(id: .newRoot(), indexInParent: 0)) |
| 33 | + self.init(_MarkupData(absoluteRaw)) |
| 34 | + } |
| 35 | + |
| 36 | + init(_ data: _MarkupData) { |
| 37 | + self._data = data |
| 38 | + } |
| 39 | + |
| 40 | + public func accept<V: MarkupVisitor>(_ visitor: inout V) -> V.Result { |
| 41 | + return visitor.visitDoxygenParameter(self) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +public extension DoxygenParameter { |
| 46 | + /// Create a new Doxygen parameter definition. |
| 47 | + /// |
| 48 | + /// - Parameter name: The name of the parameter being described. |
| 49 | + /// - Parameter children: Block child elements. |
| 50 | + init<Children: Sequence>(name: String, children: Children) where Children.Element == BlockMarkup { |
| 51 | + try! self.init(.doxygenParam(name: name, parsedRange: nil, children.map({ $0.raw.markup }))) |
| 52 | + } |
| 53 | + |
| 54 | + /// Create a new Doxygen parameter definition. |
| 55 | + /// |
| 56 | + /// - Parameter name: The name of the parameter being described. |
| 57 | + /// - Parameter children: Block child elements. |
| 58 | + init(name: String, children: BlockMarkup...) { |
| 59 | + self.init(name: name, children: children) |
| 60 | + } |
| 61 | + |
| 62 | + /// The name of the parameter being described. |
| 63 | + var name: String { |
| 64 | + get { |
| 65 | + guard case let .doxygenParam(name) = _data.raw.markup.data else { |
| 66 | + fatalError("\(self) markup wrapped unexpected \(_data.raw)") |
| 67 | + } |
| 68 | + return name |
| 69 | + } |
| 70 | + set { |
| 71 | + _data = _data.replacingSelf(.doxygenParam( |
| 72 | + name: newValue, |
| 73 | + parsedRange: nil, |
| 74 | + _data.raw.markup.copyChildren() |
| 75 | + )) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments