Skip to content

Commit fe8bc46

Browse files
committed
PR feedback
1 parent 4ca9802 commit fe8bc46

File tree

6 files changed

+256
-127
lines changed

6 files changed

+256
-127
lines changed

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,11 @@ extension FFMSwift2JavaGenerator {
370370
paramDecls.append("AllocatingSwiftArena swiftArena$")
371371
}
372372

373-
printDeclDocumentation(&printer, decl)
373+
TranslatedDocumentation.printDocumentation(
374+
importedFunc: decl,
375+
translatedDecl: translated,
376+
in: &printer
377+
)
374378
printer.printBraceBlock(
375379
"""
376380
\(annotationsStr)\(modifiers) \(returnTy) \(methodName)(\(paramDecls.joined(separator: ", ")))
@@ -385,32 +389,6 @@ extension FFMSwift2JavaGenerator {
385389
}
386390
}
387391

388-
private func printDeclDocumentation(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
389-
if var documentation = SwiftDocumentationParser.parse(decl.swiftDecl) {
390-
if let translatedDecl = translatedDecl(for: decl), translatedDecl.translatedSignature.requiresSwiftArena {
391-
documentation.parameters.append(
392-
SwiftDocumentation.Parameter(
393-
name: "swiftArena$",
394-
description: "the arena that will manage the lifetime and allocation of Swift objects"
395-
)
396-
)
397-
}
398-
399-
documentation.print(in: &printer)
400-
} else {
401-
printer.print(
402-
"""
403-
/**
404-
* Downcall to Swift:
405-
* {@snippet lang=swift :
406-
* \(decl.signatureString)
407-
* }
408-
*/
409-
"""
410-
)
411-
}
412-
}
413-
414392
/// Print the actual downcall to the Swift API.
415393
///
416394
/// This assumes that all the parameters are passed-in with appropriate names.

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,11 @@ extension JNISwift2JavaGenerator {
538538

539539
if shouldGenerateGlobalArenaVariation {
540540
if let importedFunc {
541-
printDeclDocumentation(&printer, importedFunc)
541+
TranslatedDocumentation.printDocumentation(
542+
importedFunc: importedFunc,
543+
translatedDecl: translatedDecl,
544+
in: &printer
545+
)
542546
}
543547
var modifiers = modifiers
544548

@@ -564,7 +568,11 @@ extension JNISwift2JavaGenerator {
564568
parameters.append("SwiftArena swiftArena$")
565569
}
566570
if let importedFunc {
567-
printDeclDocumentation(&printer, importedFunc)
571+
TranslatedDocumentation.printDocumentation(
572+
importedFunc: importedFunc,
573+
translatedDecl: translatedDecl,
574+
in: &printer
575+
)
568576
}
569577
let signature = "\(annotationsStr)\(modifiers.joined(separator: " ")) \(resultType) \(translatedDecl.name)(\(parameters.joined(separator: ", ")))\(throwsClause)"
570578
if skipMethodBody {
@@ -633,32 +641,6 @@ extension JNISwift2JavaGenerator {
633641
}
634642
}
635643

636-
private func printDeclDocumentation(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
637-
if var documentation = SwiftDocumentationParser.parse(decl.swiftDecl) {
638-
if let translatedDecl = translatedDecl(for: decl), translatedDecl.translatedFunctionSignature.requiresSwiftArena {
639-
documentation.parameters.append(
640-
SwiftDocumentation.Parameter(
641-
name: "swiftArena$",
642-
description: "the arena that the the returned object will be attached to"
643-
)
644-
)
645-
}
646-
647-
documentation.print(in: &printer)
648-
} else {
649-
printer.print(
650-
"""
651-
/**
652-
* Downcall to Swift:
653-
* {@snippet lang=swift :
654-
* \(decl.signatureString)
655-
* }
656-
*/
657-
"""
658-
)
659-
}
660-
}
661-
662644
private func printTypeMetadataAddressFunction(_ printer: inout CodePrinter, _ type: ImportedNominalType) {
663645
printer.print("private static native long $typeMetadataAddressDowncall();")
664646

Sources/JExtractSwiftLib/SwiftDocumentationParsing.swift

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,6 @@ struct SwiftDocumentation: Equatable {
2525
var discussion: String?
2626
var parameters: [Parameter] = []
2727
var returns: String?
28-
29-
func print(in printer: inout CodePrinter) {
30-
printer.print("/**")
31-
if let summary = summary {
32-
printer.print(" * \(summary)")
33-
}
34-
35-
if let discussion = discussion {
36-
let paragraphs = discussion.split(separator: "\n\n")
37-
for paragraph in paragraphs {
38-
printer.print(" * <p>")
39-
printer.print(" * \(paragraph)")
40-
printer.print(" * </p>")
41-
}
42-
}
43-
44-
for parameter in parameters {
45-
printer.print(" * @param \(parameter.name) \(parameter.description)")
46-
}
47-
48-
if let returns = returns {
49-
printer.print(" * @return \(returns)")
50-
}
51-
printer.print(" */")
52-
}
5328
}
5429

5530
enum SwiftDocumentationParser {
@@ -60,6 +35,7 @@ enum SwiftDocumentationParser {
6035
case returns
6136
}
6237

38+
// TODO: Replace with Regex
6339
// Capture Groups: 1=Tag, 2=Arg(Optional), 3=Description
6440
private static let tagRegex = try! NSRegularExpression(pattern: "^-\\s*(\\w+)(?:\\s+([^:]+))?\\s*:\\s*(.*)$")
6541

@@ -135,8 +111,8 @@ enum SwiftDocumentationParser {
135111
// Any blank lines will move us to discussion
136112
state = .discussion
137113
if let discussion = doc.discussion, !discussion.isEmpty {
138-
if !discussion.hasSuffix("\n\n") {
139-
doc.discussion?.append("\n\n")
114+
if !discussion.hasSuffix("\n") {
115+
doc.discussion?.append("\n")
140116
}
141117
}
142118
} else {
@@ -152,7 +128,6 @@ enum SwiftDocumentationParser {
152128
return doc
153129
}
154130

155-
/// This is a test
156131
private static func appendLineToState(_ state: State, line: String, doc: inout SwiftDocumentation) {
157132
switch state {
158133
case .summary: append(&doc.summary, line)
@@ -166,15 +141,13 @@ enum SwiftDocumentationParser {
166141
}
167142

168143
private static func append(_ existing: inout String, _ new: String) {
169-
let separator = existing.last == "\n" ? "" : " "
170-
existing += separator + new
144+
existing += "\n" + new
171145
}
172146

173147
private static func append(_ existing: inout String?, _ new: String) {
174148
if existing == nil { existing = new }
175149
else {
176-
let separator = existing?.last == "\n" ? "" : " "
177-
existing! += separator + new
150+
existing! += "\n" + new
178151
}
179152
}
180153

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftSyntax
16+
17+
enum TranslatedDocumentation {
18+
static func printDocumentation(
19+
importedFunc: ImportedFunc,
20+
translatedDecl: FFMSwift2JavaGenerator.TranslatedFunctionDecl,
21+
in printer: inout CodePrinter
22+
) {
23+
var documentation = SwiftDocumentationParser.parse(importedFunc.swiftDecl)
24+
25+
if translatedDecl.translatedSignature.requiresSwiftArena {
26+
documentation?.parameters.append(
27+
SwiftDocumentation.Parameter(
28+
name: "swiftArena$",
29+
description: "the arena that will manage the lifetime and allocation of Swift objects"
30+
)
31+
)
32+
}
33+
34+
printDocumentation(documentation, syntax: importedFunc.swiftDecl, in: &printer)
35+
}
36+
37+
static func printDocumentation(
38+
importedFunc: ImportedFunc,
39+
translatedDecl: JNISwift2JavaGenerator.TranslatedFunctionDecl,
40+
in printer: inout CodePrinter
41+
) {
42+
var documentation = SwiftDocumentationParser.parse(importedFunc.swiftDecl)
43+
44+
if translatedDecl.translatedFunctionSignature.requiresSwiftArena {
45+
documentation?.parameters.append(
46+
SwiftDocumentation.Parameter(
47+
name: "swiftArena$",
48+
description: "the arena that the the returned object will be attached to"
49+
)
50+
)
51+
}
52+
53+
printDocumentation(documentation, syntax: importedFunc.swiftDecl, in: &printer)
54+
}
55+
56+
private static func printDocumentation(
57+
_ parsedDocumentation: SwiftDocumentation?,
58+
syntax: some DeclSyntaxProtocol,
59+
in printer: inout CodePrinter
60+
) {
61+
var groups = [String]()
62+
if let summary = parsedDocumentation?.summary {
63+
groups.append("\(summary)")
64+
}
65+
66+
if let discussion = parsedDocumentation?.discussion {
67+
let paragraphs = discussion.split(separator: "\n\n")
68+
for paragraph in paragraphs {
69+
groups.append("<p>\(paragraph)")
70+
}
71+
}
72+
73+
groups.append(
74+
"""
75+
\(parsedDocumentation != nil ? "<p>" : "")Downcall to Swift:
76+
{@snippet lang=swift :
77+
\(syntax.signatureString)
78+
}
79+
"""
80+
)
81+
82+
var annotationsGroup = [String]()
83+
84+
for param in parsedDocumentation?.parameters ?? [] {
85+
annotationsGroup.append("@param \(param.name) \(param.description)")
86+
}
87+
88+
if let returns = parsedDocumentation?.returns {
89+
annotationsGroup.append("@return \(returns)")
90+
}
91+
92+
if !annotationsGroup.isEmpty {
93+
groups.append(annotationsGroup.joined(separator: "\n"))
94+
}
95+
96+
printer.print("/**")
97+
let oldIdentationText = printer.indentationText
98+
printer.indentationText += " * "
99+
for (idx, group) in groups.enumerated() {
100+
printer.print(group)
101+
if idx < groups.count - 1 {
102+
printer.print("")
103+
}
104+
}
105+
printer.indentationText = oldIdentationText
106+
printer.print(" */")
107+
108+
}
109+
}

Tests/JExtractSwiftTests/MethodImportTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ final class MethodImportTests {
9292
"""
9393
/**
9494
* Hello World!
95+
*
96+
* <p>Downcall to Swift:
97+
* {@snippet lang=swift :
98+
* public func helloWorld()
99+
* }
95100
*/
96101
public static void helloWorld() {
97102
swiftjava___FakeModule_helloWorld.call();

0 commit comments

Comments
 (0)