文档注释

用于提取 Swift 文档注释的实体。

安装

Swift Package Manager

只需将其添加到您的 Package.swift 文件的 dependencies 中

let package = Package(
    name: "MyPackage",
    products: [...],
    targets: [
        .target(
            "YouAppModule",
            dependencies: [
                .product(name: "DocumentationComment", package: "DocumentationComment")
            ]
        )
    ],
    dependencies: [
        .package(url: "https://github.com/fumito-ito/DocumentationComment.git", .upToNextMajor(from: "0.0.2"))
    ]
)

用法

您可以通过将 Swift 的 Annotation Comment 作为字符串传递来生成一个 DocumentationComment 对象。

let annotationComment = """
/// Returns a command that runs unconditionally before every build.
///
/// Prebuild commands can have a significant performance impact
/// and should only be used when there would be no way to know the
/// list of output file paths without first reading the contents
/// of one or more input files. Typically there is no way to
/// determine this list without first running the command, so
/// instead of encoding that list, the caller supplies an
/// `outputFilesDirectory` parameter, and all files in that
/// directory after the command runs are treated as output files.
///
/// ```
/// let sampleText = "This is sample text"
/// ```
///
/// > the paths in the list of output files may depend on the list
/// > of input file paths, but **must not** depend on reading the contents
/// > of any input files. Such cases must be handled using a `prebuildCommand`.
///
/// - Parameters:
///   - executable: The absolute path to the executable to be invoked.
///   - arguments: Command-line arguments to be passed to the executable.
/// - Throws: Any error thrown by `deferred` or `work` (in that order).
/// - Returns: `true` if a path from `source` to `destination` exists, `false` otherwise.
/// - Attention: Special attention is needed for this part.
/// - Author: Who is the original author of this code?
"""

let doc = try DocumentationComment(annotationComment)

DocumentationComment 从注释中提取摘要、参数等。

print(doc.abstract?.stringify) // => Returns a command that runs unconditionally before every build.
print(doc.parameters.first?.name) // => executable
print(doc.parameters.first?.description) // => The absolute path to the executable to be invoked.

也可以处理块注释格式的注释。

let blockComment = """
/*
 Returns a command that runs unconditionally before every build.

 Prebuild commands can have a significant performance impact
 and should only be used when there would be no way to know the
 list of output file paths without first reading the contents
 of one or more input files. Typically there is no way to
 determine this list without first running the command, so
 instead of encoding that list, the caller supplies an
 `outputFilesDirectory` parameter, and all files in that
 directory after the command runs are treated as output files.

 \```
 let sampleText = "This is sample text"
 \```

 > the paths in the list of output files may depend on the list
 > of input file paths, but **must not** depend on reading the contents
 > of any input files. Such cases must be handled using a `prebuildCommand`.

 - Parameters:
   - executable: The absolute path to the executable to be invoked.
   - arguments: Command-line arguments to be passed to the executable.
 - Throws: Any error thrown by `deferred` or `work` (in that order).
 - Returns: `true` if a path from `source` to `destination` exists, `false` otherwise.
 - Attention: Special attention is needed for this part.
 - Author: Who is the original author of this code?
 */
"""

let doc = try DocumentationComment(blockComment)

支持的字段

摘要 (Abstract)

提取注释的第一段字符串,作为该注释的摘要。

描述 (Description)

提取第二段及后续段落作为注释的详细信息。

参数 (Parameter)

Parameters 节或 Parameter 字段中提取参数名称及其详细信息。

Parameters 节
- Parameters:
  - x: ...
  - y: ...
  - z: ...
Parameter 字段
- Parameter x: ...
- Parameter y: ...

抛出 (Throws)

Throws 字段中提取其详细信息。

- Throws: ...

返回值 (Returns)

Returns 字段中提取其详细信息。

- Returns: ...

字段扩展 (FieldExtension)

从 Xcode 的 QuickHelp 中高亮显示的 Field Extensions 字段中提取其名称和详细信息。

- Attention: ...
- Author: ...
- Authors: ...
- Bug: ...
- Complexity: ...
- Copyright: ...
- Date: ...
- Experiment: ...
- Important: ...
- Invariant: ...
- Note: ...
- Postcondition: ...
- Precondition: ...
- Remark: ...
- Remarks: ...
- Requires: ...
- See: ...
- Since: ...
- Todo: ...
- Version: ...
- Warning: ...