From 126661e0f559cbed2f97ff32c1fe5a8b242bd94c Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Tue, 15 Apr 2025 23:22:41 +0900 Subject: [PATCH] [jextract] CodePrinter indentation improvements Improve indentations. Introduce 'atNewline' property in the printer, to indicate if the next 'print()' should print the indentation on the first line. Stop handling single line 'print()' specially. --- Sources/JExtractSwift/CodePrinter.swift | 30 ++++++++++--------- .../ImportedDecls+Printing.swift | 6 ++-- .../Swift2JavaTranslator+Printing.swift | 13 ++++---- .../VariableImportTests.swift | 4 +-- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Sources/JExtractSwift/CodePrinter.swift b/Sources/JExtractSwift/CodePrinter.swift index 6e26b960..83669ebb 100644 --- a/Sources/JExtractSwift/CodePrinter.swift +++ b/Sources/JExtractSwift/CodePrinter.swift @@ -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() @@ -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) @@ -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 } } diff --git a/Sources/JExtractSwift/ImportedDecls+Printing.swift b/Sources/JExtractSwift/ImportedDecls+Printing.swift index 966b5a3d..bc755015 100644 --- a/Sources/JExtractSwift/ImportedDecls+Printing.swift +++ b/Sources/JExtractSwift/ImportedDecls+Printing.swift @@ -22,9 +22,9 @@ extension ImportedFunc { var renderCommentSnippet: String? { if let syntax { """ - * {@snippet lang=swift : - * \(syntax) - * } + * {@snippet lang=swift : + * \(syntax) + * } """ } else { nil diff --git a/Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift b/Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift index af85a214..41c3477b 100644 --- a/Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift +++ b/Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift @@ -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))); @@ -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; @@ -601,7 +601,7 @@ extension Swift2JavaTranslator { """ /** * Address for: - \(snippet) + \(snippet) */ public static MemorySegment \(decl.baseIdentifier)\(methodNameSegment)$address() { return \(decl.baseIdentifier).\(addrName); @@ -623,7 +623,7 @@ extension Swift2JavaTranslator { """ /** * Downcall method handle for: - \(snippet) + \(snippet) */ public static MethodHandle \(decl.baseIdentifier)\(methodNameSegment)$handle() { return \(decl.baseIdentifier).\(handleName); @@ -645,7 +645,7 @@ extension Swift2JavaTranslator { """ /** * Function descriptor for: - \(snippet) + \(snippet) */ public static FunctionDescriptor \(decl.baseIdentifier)\(methodNameSegment)$descriptor() { return \(decl.baseIdentifier).\(descName); @@ -744,7 +744,7 @@ extension Swift2JavaTranslator { """ /** * Downcall to Swift: - \(decl.renderCommentSnippet ?? "* ") + \(decl.renderCommentSnippet ?? "* ") */ """ @@ -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 diff --git a/Tests/JExtractSwiftTests/VariableImportTests.swift b/Tests/JExtractSwiftTests/VariableImportTests.swift index 0d45b08b..7a97e195 100644 --- a/Tests/JExtractSwiftTests/VariableImportTests.swift +++ b/Tests/JExtractSwiftTests/VariableImportTests.swift @@ -47,7 +47,7 @@ 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 ); @@ -55,7 +55,7 @@ final class VariableImportTests { 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 );