Skip to content

[jextract] CodePrinter indentation improvements #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions Sources/JExtractSwift/CodePrinter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public struct CodePrinter {
}
}
public var indentationText: String = ""
/// If true, next print() should starts with indentation.
var atNewline = true

public static func toString(_ block: (inout CodePrinter) throws -> ()) rethrows -> String {
var printer = CodePrinter()
Expand Down Expand Up @@ -73,8 +75,8 @@ public struct CodePrinter {
line: UInt = #line,
body: (inout CodePrinter) -> ()
) {
indent()
print("\(text) {")
indent()
body(&self)
outdent()
print("}", .sloc, function: function, file: file, line: line)
Expand Down Expand Up @@ -113,27 +115,27 @@ public struct CodePrinter {
file: String = #fileID,
line: UInt = #line
) {
append(indentationText)

let lines = "\(text)".split(separator: "\n")
if indentationDepth > 0 && lines.count > 1 {
for line in lines {
append(indentationText)
append(contentsOf: line)
let lines = "\(text)".split(separator: "\n", omittingEmptySubsequences: false)
var first = true
for line in lines {
if !first {
append("\n")
append(indentationText)
} else {
if atNewline {
append(indentationText)
}
first = false
}
} else {
append("\(text)")
append(contentsOf: line)
}

if terminator == .sloc {
append(" // \(function) @ \(file):\(line)\n")
append(indentationText)
atNewline = true
} else {
append(terminator.rawValue)
if terminator == .newLine || terminator == .commaNewLine {
append(indentationText)
}
atNewline = terminator == .newLine || terminator == .commaNewLine
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/JExtractSwift/ImportedDecls+Printing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ extension ImportedFunc {
var renderCommentSnippet: String? {
if let syntax {
"""
* {@snippet lang=swift :
* \(syntax)
* }
* {@snippet lang=swift :
* \(syntax)
* }
"""
} else {
nil
Expand Down
13 changes: 6 additions & 7 deletions Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ extension Swift2JavaTranslator {
/**
* Create an instance of {@code \(parentName.unqualifiedJavaTypeName)}.
*
\(decl.renderCommentSnippet ?? " *")
\(decl.renderCommentSnippet ?? " *")
*/
public \(parentName.unqualifiedJavaTypeName)(\(renderJavaParamDecls(decl, paramPassingStyle: .wrapper))) {
this(/*arena=*/null, \(renderForwardJavaParams(decl, paramPassingStyle: .wrapper)));
Expand Down Expand Up @@ -543,7 +543,7 @@ extension Swift2JavaTranslator {
* Create an instance of {@code \(parentName.unqualifiedJavaTypeName)}.
* This instance is managed by the passed in {@link SwiftArena} and may not outlive the arena's lifetime.
*
\(decl.renderCommentSnippet ?? " *")
\(decl.renderCommentSnippet ?? " *")
*/
public \(parentName.unqualifiedJavaTypeName)(SwiftArena arena, \(renderJavaParamDecls(decl, paramPassingStyle: .wrapper))) {
var mh$ = \(descClassIdentifier).HANDLE;
Expand Down Expand Up @@ -601,7 +601,7 @@ extension Swift2JavaTranslator {
"""
/**
* Address for:
\(snippet)
\(snippet)
*/
public static MemorySegment \(decl.baseIdentifier)\(methodNameSegment)$address() {
return \(decl.baseIdentifier).\(addrName);
Expand All @@ -623,7 +623,7 @@ extension Swift2JavaTranslator {
"""
/**
* Downcall method handle for:
\(snippet)
\(snippet)
*/
public static MethodHandle \(decl.baseIdentifier)\(methodNameSegment)$handle() {
return \(decl.baseIdentifier).\(handleName);
Expand All @@ -645,7 +645,7 @@ extension Swift2JavaTranslator {
"""
/**
* Function descriptor for:
\(snippet)
\(snippet)
*/
public static FunctionDescriptor \(decl.baseIdentifier)\(methodNameSegment)$descriptor() {
return \(decl.baseIdentifier).\(descName);
Expand Down Expand Up @@ -744,7 +744,7 @@ extension Swift2JavaTranslator {
"""
/**
* Downcall to Swift:
\(decl.renderCommentSnippet ?? "* ")
\(decl.renderCommentSnippet ?? "* ")
*/
"""

Expand Down Expand Up @@ -1065,7 +1065,6 @@ extension Swift2JavaTranslator {
} else {
printer.print("FunctionDescriptor.of(")
printer.indent()
printer.print("", .continue)

// Write return type
let returnTyIsLastTy = decl.parameters.isEmpty && !decl.hasParent
Expand Down
4 changes: 2 additions & 2 deletions Tests/JExtractSwiftTests/VariableImportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ final class VariableImportTests {
expectedChunks: [
"""
private static class counterInt {
public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
/* -> */SWIFT_INT,
/*self$*/SWIFT_POINTER
);
public static final MemorySegment ADDR_GET =
FakeModule.findOrThrow("swiftjava_FakeModule_MySwiftClass_counterInt");

public static final MethodHandle HANDLE_GET = Linker.nativeLinker().downcallHandle(ADDR_GET, DESC_GET);
public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
/*newValue*/SWIFT_INT,
/*self$*/SWIFT_POINTER
);
Expand Down