Skip to content

Commit e18a26b

Browse files
authored
Merge pull request #221 from rintaro/jextract-codeprinter
2 parents c8681c1 + 126661e commit e18a26b

File tree

4 files changed

+27
-26
lines changed

4 files changed

+27
-26
lines changed

Sources/JExtractSwift/CodePrinter.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public struct CodePrinter {
3333
}
3434
}
3535
public var indentationText: String = ""
36+
/// If true, next print() should starts with indentation.
37+
var atNewline = true
3638

3739
public static func toString(_ block: (inout CodePrinter) throws -> ()) rethrows -> String {
3840
var printer = CodePrinter()
@@ -73,8 +75,8 @@ public struct CodePrinter {
7375
line: UInt = #line,
7476
body: (inout CodePrinter) -> ()
7577
) {
76-
indent()
7778
print("\(text) {")
79+
indent()
7880
body(&self)
7981
outdent()
8082
print("}", .sloc, function: function, file: file, line: line)
@@ -113,27 +115,27 @@ public struct CodePrinter {
113115
file: String = #fileID,
114116
line: UInt = #line
115117
) {
116-
append(indentationText)
117-
118-
let lines = "\(text)".split(separator: "\n")
119-
if indentationDepth > 0 && lines.count > 1 {
120-
for line in lines {
121-
append(indentationText)
122-
append(contentsOf: line)
118+
let lines = "\(text)".split(separator: "\n", omittingEmptySubsequences: false)
119+
var first = true
120+
for line in lines {
121+
if !first {
123122
append("\n")
123+
append(indentationText)
124+
} else {
125+
if atNewline {
126+
append(indentationText)
127+
}
128+
first = false
124129
}
125-
} else {
126-
append("\(text)")
130+
append(contentsOf: line)
127131
}
128132

129133
if terminator == .sloc {
130134
append(" // \(function) @ \(file):\(line)\n")
131-
append(indentationText)
135+
atNewline = true
132136
} else {
133137
append(terminator.rawValue)
134-
if terminator == .newLine || terminator == .commaNewLine {
135-
append(indentationText)
136-
}
138+
atNewline = terminator == .newLine || terminator == .commaNewLine
137139
}
138140
}
139141

Sources/JExtractSwift/ImportedDecls+Printing.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ extension ImportedFunc {
2222
var renderCommentSnippet: String? {
2323
if let syntax {
2424
"""
25-
* {@snippet lang=swift :
26-
* \(syntax)
27-
* }
25+
* {@snippet lang=swift :
26+
* \(syntax)
27+
* }
2828
"""
2929
} else {
3030
nil

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ extension Swift2JavaTranslator {
510510
/**
511511
* Create an instance of {@code \(parentName.unqualifiedJavaTypeName)}.
512512
*
513-
\(decl.renderCommentSnippet ?? " *")
513+
\(decl.renderCommentSnippet ?? " *")
514514
*/
515515
public \(parentName.unqualifiedJavaTypeName)(\(renderJavaParamDecls(decl, paramPassingStyle: .wrapper))) {
516516
this(/*arena=*/null, \(renderForwardJavaParams(decl, paramPassingStyle: .wrapper)));
@@ -543,7 +543,7 @@ extension Swift2JavaTranslator {
543543
* Create an instance of {@code \(parentName.unqualifiedJavaTypeName)}.
544544
* This instance is managed by the passed in {@link SwiftArena} and may not outlive the arena's lifetime.
545545
*
546-
\(decl.renderCommentSnippet ?? " *")
546+
\(decl.renderCommentSnippet ?? " *")
547547
*/
548548
public \(parentName.unqualifiedJavaTypeName)(SwiftArena arena, \(renderJavaParamDecls(decl, paramPassingStyle: .wrapper))) {
549549
var mh$ = \(descClassIdentifier).HANDLE;
@@ -601,7 +601,7 @@ extension Swift2JavaTranslator {
601601
"""
602602
/**
603603
* Address for:
604-
\(snippet)
604+
\(snippet)
605605
*/
606606
public static MemorySegment \(decl.baseIdentifier)\(methodNameSegment)$address() {
607607
return \(decl.baseIdentifier).\(addrName);
@@ -623,7 +623,7 @@ extension Swift2JavaTranslator {
623623
"""
624624
/**
625625
* Downcall method handle for:
626-
\(snippet)
626+
\(snippet)
627627
*/
628628
public static MethodHandle \(decl.baseIdentifier)\(methodNameSegment)$handle() {
629629
return \(decl.baseIdentifier).\(handleName);
@@ -645,7 +645,7 @@ extension Swift2JavaTranslator {
645645
"""
646646
/**
647647
* Function descriptor for:
648-
\(snippet)
648+
\(snippet)
649649
*/
650650
public static FunctionDescriptor \(decl.baseIdentifier)\(methodNameSegment)$descriptor() {
651651
return \(decl.baseIdentifier).\(descName);
@@ -744,7 +744,7 @@ extension Swift2JavaTranslator {
744744
"""
745745
/**
746746
* Downcall to Swift:
747-
\(decl.renderCommentSnippet ?? "* ")
747+
\(decl.renderCommentSnippet ?? "* ")
748748
*/
749749
"""
750750

@@ -1065,7 +1065,6 @@ extension Swift2JavaTranslator {
10651065
} else {
10661066
printer.print("FunctionDescriptor.of(")
10671067
printer.indent()
1068-
printer.print("", .continue)
10691068

10701069
// Write return type
10711070
let returnTyIsLastTy = decl.parameters.isEmpty && !decl.hasParent

Tests/JExtractSwiftTests/VariableImportTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ final class VariableImportTests {
4747
expectedChunks: [
4848
"""
4949
private static class counterInt {
50-
public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
50+
public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
5151
/* -> */SWIFT_INT,
5252
/*self$*/SWIFT_POINTER
5353
);
5454
public static final MemorySegment ADDR_GET =
5555
FakeModule.findOrThrow("swiftjava_FakeModule_MySwiftClass_counterInt");
5656
5757
public static final MethodHandle HANDLE_GET = Linker.nativeLinker().downcallHandle(ADDR_GET, DESC_GET);
58-
public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
58+
public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
5959
/*newValue*/SWIFT_INT,
6060
/*self$*/SWIFT_POINTER
6161
);

0 commit comments

Comments
 (0)