Skip to content

CodePrinting: make CodePrinter generic over a Language type#799

Merged
ktoso merged 2 commits into
swiftlang:mainfrom
ktoso:add-codeprinter-language
Jun 21, 2026
Merged

CodePrinting: make CodePrinter generic over a Language type#799
ktoso merged 2 commits into
swiftlang:mainfrom
ktoso:add-codeprinter-language

Conversation

@ktoso

@ktoso ktoso commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Introduces protocol CodePrinterLanguage with concrete tags SwiftLanguage, JavaLanguage. CodePrinter becomes CodePrinter<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.

@ktoso ktoso force-pushed the add-codeprinter-language branch from 2516cac to ddcbfc9 Compare June 20, 2026 10:31
ktoso added 2 commits June 20, 2026 20:19
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.
@ktoso ktoso marked this pull request as ready for review June 20, 2026 11:26
@ktoso ktoso force-pushed the add-codeprinter-language branch from ddcbfc9 to 57963ae Compare June 20, 2026 11:27
@ktoso ktoso merged commit 75c6ed7 into swiftlang:main Jun 21, 2026
41 of 42 checks passed
@ktoso ktoso deleted the add-codeprinter-language branch June 21, 2026 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant