$ show outfoxx/swiftpoet
SwiftPoet is a Kotlin/Java library for programmatically generating Swift source code. It provides a fluent builder API to construct Swift files with classes, structs, enums, protocols, extensions, functions, properties, type aliases, imports, attributes, documentation, and more. The library handles type name resolution, import management, keyword escaping, and proper formatting including indentation and line wrapping. It is inspired by JavaPoet and designed for use in annotation processing, metadata-driven code generation, and similar scenarios. The architecture is a pure library with no runtime dependencies beyond Kotlin stdlib, making it lightweight and embeddable. The codebase demonstrates strong software craftsmanship with clean abstractions, extensive test coverage, and reusable utility components.
The LLM identified 4 files worth highlighting for reuse:
Implements soft line wrapping on an appendable, buffering text and deciding when to wrap based on a column limit. It is a self-contained utility that can be extracted and reused in any code generation or text formatting tool that needs to respect column limits.
Manages identifier name allocation to avoid collisions with keywords and other names. Supports tagging for scoped allocation and cloning for independent inner scopes. Reusable in any code generation context where unique identifiers are needed.
Provides a builder for constructing code fragments with placeholders (%L, %N, %S, %T, %W, etc.) and control flow (if/else, switch). The format string system is self-contained and could be adapted for other languages or code generation tools.
Contains the stringLiteralWithQuotes function that generates Swift string literals with proper escaping, raw strings, multiline, and hash-based delimiters. The complex logic for choosing the optimal literal form is reusable in any Swift code generation tool.