forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslatedDocumentation.swift
More file actions
119 lines (102 loc) · 3.56 KB
/
TranslatedDocumentation.swift
File metadata and controls
119 lines (102 loc) · 3.56 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift.org project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import CodePrinting
import SwiftJavaConfigurationShared
import SwiftSyntax
enum TranslatedDocumentation {
static func printDocumentation(
importedFunc: ImportedFunc,
translatedDecl: FFMSwift2JavaGenerator.TranslatedFunctionDecl,
config: Configuration,
in printer: inout CodePrinter
) {
var documentation = SwiftDocumentationParser.parse(importedFunc.swiftDecl)
if translatedDecl.translatedSignature.requiresSwiftArena {
documentation?.parameters.append(
SwiftDocumentation.Parameter(
name: "swiftArena",
description: "the arena that will manage the lifetime and allocation of Swift objects"
)
)
}
printDocumentation(documentation, syntax: importedFunc.swiftDecl, config: config, in: &printer)
}
static func printDocumentation(
importedFunc: ImportedFunc,
translatedDecl: JNISwift2JavaGenerator.TranslatedFunctionDecl,
config: Configuration,
in printer: inout CodePrinter
) {
var documentation = SwiftDocumentationParser.parse(importedFunc.swiftDecl)
if translatedDecl.translatedFunctionSignature.requiresSwiftArena {
documentation?.parameters.append(
SwiftDocumentation.Parameter(
name: "swiftArena",
description: "the arena that the the returned object will be attached to"
)
)
}
printDocumentation(documentation, syntax: importedFunc.swiftDecl, config: config, in: &printer)
}
private static func printDocumentation(
_ parsedDocumentation: SwiftDocumentation?,
syntax: some DeclSyntaxProtocol,
config: Configuration,
in printer: inout CodePrinter
) {
var groups = [String]()
if let summary = parsedDocumentation?.summary {
groups.append("\(summary)")
}
if let discussion = parsedDocumentation?.discussion {
let paragraphs = discussion.split(separator: "\n\n")
for paragraph in paragraphs {
groups.append("<p>\(paragraph)")
}
}
let signatureString = syntax.signatureString
groups.append(
"""
\(parsedDocumentation != nil ? "<p>" : "")Downcall to Swift:
\(config.javadocCodeSnippetStart(lang: "swift"))
\(signatureString)
\(config.javadocCodeSnippetEnd)
"""
)
var annotationsGroup = [String]()
for param in parsedDocumentation?.parameters ?? [] {
annotationsGroup.append("@param \(param.name) \(param.description)")
}
if let throwsDescription = parsedDocumentation?.throwsDescription {
annotationsGroup.append("@throws Exception \(throwsDescription)")
}
if let returns = parsedDocumentation?.returns {
annotationsGroup.append("@return \(returns)")
}
if !annotationsGroup.isEmpty {
groups.append(annotationsGroup.joined(separator: "\n"))
}
printer.print("/**")
let oldIdentationText = printer.indentationText
printer.indentationText += " * "
for (idx, group) in groups.enumerated() {
printer.print(group)
if idx < groups.count - 1 {
printer.print("")
}
}
printer.indentationText = oldIdentationText
printer.print(" */")
}
}