-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update StringGenerator to produce APIs that work on WWDC21 OS releases * Make implementation details of generated Localizable struct private * Refactor generator and create SwiftSyntax extensions * Update snapshots * Make BundleDescription type fileprivate * Restore documentation
- Loading branch information
1 parent
9488e2c
commit 15acd61
Showing
21 changed files
with
2,394 additions
and
557 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
33 changes: 33 additions & 0 deletions
33
Sources/StringGenerator/SwiftSyntax/ExtensionDeclSyntax.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
extension ExtensionDeclSyntax { | ||
init( | ||
availability: AvailabilityArgumentListSyntax, | ||
accessLevel: Keyword? = nil, | ||
extendedType: some TypeSyntaxProtocol, | ||
@MemberBlockItemListBuilder memberBlockBuilder: () -> MemberBlockItemListSyntax | ||
) { | ||
self.init( | ||
attributes: [ | ||
.attribute( | ||
AttributeSyntax(availability: availability) | ||
.with(\.trailingTrivia, .newline) | ||
) | ||
], | ||
modifiers: DeclModifierListSyntax { | ||
if let accessLevel { | ||
DeclModifierSyntax(name: .keyword(accessLevel)) | ||
} | ||
}, | ||
extendedType: extendedType, | ||
memberBlock: MemberBlockSyntax(members: memberBlockBuilder()) | ||
) | ||
} | ||
|
||
func spacingMembers(_ lineCount: Int = 2) -> ExtensionDeclSyntax { | ||
updating(\.memberBlock) { memberBlock in | ||
memberBlock = memberBlock.spacingMembers(lineCount) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Sources/StringGenerator/SwiftSyntax/FunctionCallExprSyntax.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
extension FunctionCallExprSyntax { | ||
func multiline() -> FunctionCallExprSyntax { | ||
self | ||
.updating(\.arguments) { arguments in | ||
arguments = LabeledExprListSyntax { | ||
for (idx, argument) in zip(1..., arguments) { | ||
if idx == arguments.count { | ||
argument.with(\.leadingTrivia, .newline) | ||
} else { | ||
argument | ||
.with(\.trailingComma, .commaToken()) | ||
.with(\.leadingTrivia, .newline) | ||
} | ||
} | ||
} | ||
} | ||
.with(\.rightParen, .rightParenToken(leadingTrivia: .newline)) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
Sources/StringGenerator/SwiftSyntax/FunctionSignatureSyntax.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
extension FunctionSignatureSyntax { | ||
func multiline() -> FunctionSignatureSyntax { | ||
self | ||
.updating(\.parameterClause) { $0 = $0.multiline() } | ||
} | ||
} | ||
|
||
extension FunctionParameterClauseSyntax { | ||
func multiline() -> FunctionParameterClauseSyntax { | ||
self | ||
.updating(\.parameters) { parameters in | ||
parameters = FunctionParameterListSyntax { | ||
for parameter in parameters { | ||
parameter.with(\.leadingTrivia, .newline) | ||
} | ||
} | ||
} | ||
.with(\.rightParen, .rightParenToken(leadingTrivia: .newline)) | ||
} | ||
|
||
func commaSeparated() -> FunctionParameterClauseSyntax { | ||
self.updating(\.parameters) { parameters in | ||
parameters = FunctionParameterListSyntax { | ||
for (idx, parameter) in zip(1..., parameters) { | ||
if idx == parameters.count { | ||
parameter | ||
} else { | ||
parameter | ||
.with(\.trailingComma, .commaToken()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Sources/StringGenerator/SwiftSyntax/MemberBlockSyntax.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import SwiftSyntax | ||
|
||
extension MemberBlockSyntax { | ||
func spacingMembers(_ lineCount: Int = 2) -> MemberBlockSyntax { | ||
updating(\.members) { members in | ||
members = MemberBlockItemListSyntax { | ||
for (idx, item) in zip(1..., members) { | ||
if idx == members.count { | ||
item | ||
} else { | ||
item.with(\.trailingTrivia, .newlines(lineCount)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sources/StringGenerator/SwiftSyntax/SourceFileSyntax.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import SwiftSyntax | ||
|
||
extension SourceFileSyntax { | ||
func spacingStatements(_ lineCount: Int = 2) -> Self { | ||
var copy = self | ||
copy.statements = CodeBlockItemListSyntax { | ||
for (idx, item) in zip(1..., statements) { | ||
if idx == statements.count { | ||
item | ||
} else { | ||
item.with(\.trailingTrivia, .newlines(lineCount)) | ||
} | ||
} | ||
} | ||
return copy | ||
} | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import SwiftSyntax | ||
|
||
extension StructDeclSyntax { | ||
func spacingMembers(_ lineCount: Int = 2) -> StructDeclSyntax { | ||
updating(\.memberBlock) { memberBlock in | ||
memberBlock = memberBlock.spacingMembers(lineCount) | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import SwiftSyntax | ||
|
||
public extension SyntaxProtocol { | ||
/// Returns a new syntax node that has the child at `keyPath` updated by the changes closure | ||
func updating<T>(_ keyPath: WritableKeyPath<Self, T>, changes: (inout T) -> Void) -> Self { | ||
var copy = self | ||
changes(©[keyPath: keyPath]) | ||
return copy | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Sources/StringGenerator/SwiftSyntax/TokenSyntax+Import.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import SwiftSyntax | ||
|
||
extension TokenSyntax { | ||
enum Import: String { | ||
case Foundation = "Foundation" | ||
} | ||
|
||
static func `import`(_ value: Import) -> TokenSyntax { | ||
.identifier(value.rawValue) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/StringGenerator/SwiftSyntax/TokenSyntax+Types.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import SwiftSyntax | ||
|
||
extension TokenSyntax { | ||
enum MetaType: String { | ||
case String | ||
case StaticString | ||
case LocalizationValue | ||
case Locale | ||
case Bundle | ||
case AnyClass | ||
case BundleDescription | ||
case LocalizedStringResource | ||
} | ||
|
||
static func type(_ value: MetaType) -> TokenSyntax { | ||
.identifier(value.rawValue) | ||
} | ||
} | ||
|
||
extension TypeSyntaxProtocol where Self == IdentifierTypeSyntax { | ||
static func identifier(_ value: TokenSyntax.MetaType) -> IdentifierTypeSyntax { | ||
IdentifierTypeSyntax(name: .type(value)) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.