Skip to content

Commit 977301f

Browse files
committed
add ffm
1 parent 4d50513 commit 977301f

File tree

4 files changed

+172
-35
lines changed

4 files changed

+172
-35
lines changed

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaBindingsPrinting.swift

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

373-
// TODO: we could copy the Swift method's documentation over here, that'd be great UX
374373
printDeclDocumentation(&printer, decl)
375374
printer.printBraceBlock(
376375
"""
@@ -387,16 +386,29 @@ extension FFMSwift2JavaGenerator {
387386
}
388387

389388
private func printDeclDocumentation(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
390-
printer.print(
391-
"""
392-
/**
393-
* Downcall to Swift:
394-
* {@snippet lang=swift :
395-
* \(decl.signatureString)
396-
* }
397-
*/
398-
"""
399-
)
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+
}
400412
}
401413

402414
/// Print the actual downcall to the Swift API.

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -634,33 +634,17 @@ extension JNISwift2JavaGenerator {
634634
}
635635

636636
private func printDeclDocumentation(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
637-
if let documentation = SwiftDocumentationParser.parse(decl.swiftDecl) {
638-
printer.print("/**")
639-
if let summary = documentation.summary {
640-
printer.print(" * \(summary)")
641-
}
642-
643-
if let discussion = documentation.discussion {
644-
let paragraphs = discussion.split(separator: "\n\n")
645-
for paragraph in paragraphs {
646-
printer.print(" * <p>")
647-
printer.print(" * \(paragraph)")
648-
printer.print(" * </p>")
649-
}
650-
}
651-
652-
for parameter in documentation.parameters {
653-
printer.print(" * @param \(parameter.name) \(parameter.description)")
654-
}
655-
637+
if var documentation = SwiftDocumentationParser.parse(decl.swiftDecl) {
656638
if let translatedDecl = translatedDecl(for: decl), translatedDecl.translatedFunctionSignature.requiresSwiftArena {
657-
printer.print(" * @param swiftArena$ the arena that the the returned object will be attached to")
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+
)
658645
}
659646

660-
if let returns = documentation.returns {
661-
printer.print(" * @return \(returns)")
662-
}
663-
printer.print(" */")
647+
documentation.print(in: &printer)
664648
} else {
665649
printer.print(
666650
"""

Sources/JExtractSwiftLib/SwiftDocumentationParsing.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ struct SwiftDocumentation: Equatable {
1111
var discussion: String?
1212
var parameters: [Parameter] = []
1313
var returns: String?
14+
15+
func print(in printer: inout CodePrinter) {
16+
printer.print("/**")
17+
if let summary = summary {
18+
printer.print(" * \(summary)")
19+
}
20+
21+
if let discussion = discussion {
22+
let paragraphs = discussion.split(separator: "\n\n")
23+
for paragraph in paragraphs {
24+
printer.print(" * <p>")
25+
printer.print(" * \(paragraph)")
26+
printer.print(" * </p>")
27+
}
28+
}
29+
30+
for parameter in parameters {
31+
printer.print(" * @param \(parameter.name) \(parameter.description)")
32+
}
33+
34+
if let returns = returns {
35+
printer.print(" * @return \(returns)")
36+
}
37+
printer.print(" */")
38+
}
1439
}
1540

1641
enum SwiftDocumentationParser {

Tests/JExtractSwiftTests/SwiftDocumentationParsingTests.swift

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ struct SwiftDocumentationParsingTests {
3030
public static void f() {
3131
"""
3232
]
33+
),
34+
(
35+
JExtractGenerationMode.ffm,
36+
[
37+
"""
38+
/**
39+
* Simple summary
40+
*/
41+
public static void f() {
42+
"""
43+
]
3344
)
3445
]
3546
)
@@ -69,6 +80,26 @@ struct SwiftDocumentationParsingTests {
6980
public static void g() {
7081
"""
7182
]
83+
),
84+
(
85+
JExtractGenerationMode.ffm,
86+
[
87+
"""
88+
/**
89+
* Downcall to Swift:
90+
* {@snippet lang=swift :
91+
* public func f()
92+
* }
93+
*/
94+
public static void f() {
95+
""",
96+
"""
97+
/**
98+
* Simple summary
99+
*/
100+
public static void g() {
101+
"""
102+
]
72103
)
73104
]
74105
)
@@ -109,6 +140,18 @@ struct SwiftDocumentationParsingTests {
109140
public static MyClass f(SwiftArena swiftArena$) {
110141
"""
111142
]
143+
),
144+
(
145+
JExtractGenerationMode.ffm,
146+
[
147+
"""
148+
/**
149+
* Simple summary
150+
* @param swiftArena$ the arena that will manage the lifetime and allocation of Swift objects
151+
*/
152+
public static MyClass f(AllocatingSwiftArena swiftArena$)
153+
"""
154+
]
112155
)
113156
]
114157
)
@@ -147,6 +190,23 @@ struct SwiftDocumentationParsingTests {
147190
public static void f(java.lang.String arg0, java.lang.String arg1) {
148191
"""
149192
]
193+
),
194+
(
195+
JExtractGenerationMode.ffm,
196+
[
197+
"""
198+
/**
199+
* Simple summary
200+
* <p>
201+
* Some information about this function that will span multiple lines
202+
* </p>
203+
* @param arg0 Description about arg0
204+
* @param arg1 Description about arg1
205+
* @return return value
206+
*/
207+
public static void f(java.lang.String arg0, java.lang.String arg1) {
208+
"""
209+
]
150210
)
151211
]
152212
)
@@ -191,6 +251,23 @@ struct SwiftDocumentationParsingTests {
191251
public static void f(java.lang.String arg0, java.lang.String arg1) {
192252
"""
193253
]
254+
),
255+
(
256+
JExtractGenerationMode.ffm,
257+
[
258+
"""
259+
/**
260+
* Simple summary
261+
* <p>
262+
* Some information about this function that will span multiple lines
263+
* </p>
264+
* @param arg0 Description about arg0
265+
* @param arg1 Description about arg1
266+
* @return return value
267+
*/
268+
public static void f(java.lang.String arg0, java.lang.String arg1) {
269+
"""
270+
]
194271
)
195272
]
196273
)
@@ -242,6 +319,29 @@ struct SwiftDocumentationParsingTests {
242319
public static void f(java.lang.String arg0, java.lang.String arg1) {
243320
"""
244321
]
322+
),
323+
(
324+
JExtractGenerationMode.ffm,
325+
[
326+
"""
327+
/**
328+
* Simple summary, that we have broken across multiple lines
329+
* <p>
330+
* Some information about this function that will span multiple lines
331+
* </p>
332+
* <p>
333+
* Some more disucssion...
334+
* </p>
335+
* <p>
336+
* And more...
337+
* </p>
338+
* @param arg0 Description about arg0
339+
* @param arg1 Description about arg1
340+
* @return return value across multiple lines
341+
*/
342+
public static void f(java.lang.String arg0, java.lang.String arg1) {
343+
"""
344+
]
245345
)
246346
]
247347
)
@@ -295,6 +395,22 @@ struct SwiftDocumentationParsingTests {
295395
public static void f(java.lang.String arg0, java.lang.String arg1) {
296396
"""
297397
]
398+
),
399+
(
400+
JExtractGenerationMode.ffm,
401+
[
402+
"""
403+
/**
404+
* <p>
405+
* Discussion?
406+
* </p>
407+
* @param arg0 this is arg0
408+
* @param arg1 this is arg1
409+
* @return return value
410+
*/
411+
public static void f(java.lang.String arg0, java.lang.String arg1) {
412+
"""
413+
]
298414
)
299415
]
300416
)

0 commit comments

Comments
 (0)