CodePrinting: make CodePrinter generic over a Language type#799
Merged
Conversation
2516cac to
ddcbfc9
Compare
Introduces `protocol CodePrinterLanguage` with concrete tags
`SwiftLanguage` and `JavaLanguage`, plus convenience typealiases
`SwiftPrinter` (= `CodePrinter<SwiftLanguage>`) and `JavaPrinter`
(= `CodePrinter<JavaLanguage>`) used at every call site. `CodePrinter`
is now `CodePrinter<Language>` so language-specific helpers can be
exposed via constrained extensions that resolve at compile time.
What moved into language-specific extensions:
- `CodePrinter+Swift.swift` — `printIfBlock` (no parens around the
condition; Swift convention) and `printGuardBlock`. `printGuardBlock`
is therefore unavailable on `JavaPrinter` at compile time.
- `CodePrinter+Java.swift` — `printIfBlock` (C-style parens around the
condition; required by Java).
The base `CodePrinter` no longer carries `printIfBlock`; each call
site resolves to the right shape statically based on the printer's
`Language`. `inlineCommentStyle` now defaults to
`Language.defaultInlineCommentStyle` (still overridable per instance).
All existing call sites are migrated to use the typealiases:
- Files printing Swift code (`*+SwiftThunkPrinting`,
`*+NativeTranslation`, `*+InterfaceWrapperGeneration`) →
`SwiftPrinter`.
- Files printing Java code (`*+JavaBindingsPrinting`,
`*+JavaTranslation`, `*+LabeledTuples`, `*+FoundationData`,
`FFMSwift2JavaGenerator`, `JNISwift2JavaGenerator`,
`TranslatedDocumentation`, `JavaDependencyResolver`) →
`JavaPrinter`.
- `SymbolTable.printImportedModules` is generic over `Language` since
`import X` is valid in both Swift and Java contexts.
- `TextAssertions` test helper case-analyses `renderKind` so each path
uses the correctly-typed printer.
Ad-hoc `printBraceBlock("guard …")` / `printBraceBlock("if (…)")`
sites in JNI/FFM generators are migrated to the typed helpers.
The standalone `PrintGuardBlockTests.swift` is folded into
`InlineCommentStyleTests.swift` (one file per test target area).
Adds `printJavadocComment(_:)` on `JavaPrinter`, which switches between classic `/** … */` block comments and Markdown-style `///` line comments (JEP 467, https://openjdk.org/jeps/467) based on the printer's target Java source version. To carry that version, `CodePrinterLanguage` gains an associated `Options` type (defaulting to `Void` for languages that don't need configuration). `JavaLanguage.Options` holds `sourceVersion: JavaVersion` (reusing the typealias from `SwiftJavaConfigurationShared`); other languages stay zero-overhead. The existing manual block in `TranslatedDocumentation.swift` is migrated to use the helper — the indentation hack (mutating the printer's `indentationText` to insert ` * `) goes away. Pass paragraph breaks as blank lines (`\n\n`) inside the text. Blank lines render as the bare separator (` *` for blocks, `///` for markdown) — no trailing whitespace.
ddcbfc9 to
57963ae
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces
protocol CodePrinterLanguagewith concrete tagsSwiftLanguage,JavaLanguage.CodePrinterbecomesCodePrinter<Language>so language-specific helpers can be exposed via constrained extensions that resolve at compile time.This also helps readability because you know immediately which thunks you're printing, the java or swift ones etc.
This is also future looking for other languages as they would do the same trick and get the same clarity benefits.