-
-
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.
generate type with Hashable and Equatable conformance
- Loading branch information
1 parent
f9dfc7d
commit 638d847
Showing
18 changed files
with
276 additions
and
26 deletions.
There are no files selected for viewing
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
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
59 changes: 59 additions & 0 deletions
59
...gGenerator/Snippets/String/StringsTable/StringStringsTableComparisonFunctionSnippet.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,59 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
struct StringStringsTableComparisonFunctionSnippet: Snippet { | ||
let stringsTable: SourceFile.StringExtension.StringsTableStruct | ||
let leftArg: TokenSyntax = "lhs" | ||
let rightArg: TokenSyntax = "rhs" | ||
|
||
var syntax: some DeclSyntaxProtocol { | ||
FunctionDeclSyntax( | ||
modifiers: modifiers, | ||
name: .binaryOperator("=="), | ||
signature: FunctionSignatureSyntax( | ||
parameterClause: FunctionParameterClauseSyntax { | ||
FunctionParameterSyntax( | ||
firstName: leftArg, | ||
type: IdentifierTypeSyntax(name: stringsTable.type) | ||
) | ||
FunctionParameterSyntax( | ||
firstName: rightArg, | ||
type: IdentifierTypeSyntax(name: stringsTable.type) | ||
) | ||
}, | ||
returnClause: ReturnClauseSyntax(type: .identifier(.Bool)) | ||
), | ||
body: CodeBlockSyntax(statements: body) | ||
) | ||
} | ||
|
||
@DeclModifierListBuilder | ||
var modifiers: DeclModifierListSyntax { | ||
DeclModifierSyntax(name: stringsTable.accessLevel.token) | ||
DeclModifierSyntax(name: .keyword(.static)) | ||
} | ||
|
||
var body: CodeBlockItemListSyntax { | ||
// Array of `lhs.foo == rhs.foo` | ||
var comparisons = stringsTable.comparableProperties.map { property in | ||
InfixOperatorExprSyntax( | ||
leftOperand: MemberAccessExprSyntax(leftArg, property.name), | ||
operator: BinaryOperatorExprSyntax(operator: .binaryOperator("==")), | ||
rightOperand: MemberAccessExprSyntax(rightArg, property.name) | ||
) | ||
} | ||
|
||
var next = comparisons.removeLast() | ||
while !comparisons.isEmpty { | ||
let left = comparisons.removeLast() | ||
|
||
next = InfixOperatorExprSyntax( | ||
leftOperand: left, | ||
operator: BinaryOperatorExprSyntax(operator: .binaryOperator("&&")), | ||
rightOperand: next | ||
) | ||
} | ||
|
||
return [CodeBlockItemSyntax(item: .expr(ExprSyntax(next)))] | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ingGenerator/Snippets/String/StringsTable/StringStringsTableHashIntoFunctionSnippet.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,43 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
struct StringStringsTableHashIntoFunctionSnippet: Snippet { | ||
let stringsTable: SourceFile.StringExtension.StringsTableStruct | ||
let hasherToken: TokenSyntax = "hasher" | ||
|
||
var syntax: some DeclSyntaxProtocol { | ||
FunctionDeclSyntax( | ||
modifiers: modifiers, | ||
name: "hash", | ||
signature: FunctionSignatureSyntax( | ||
parameterClause: FunctionParameterClauseSyntax { | ||
FunctionParameterSyntax( | ||
firstName: "into", | ||
secondName: hasherToken, | ||
type: AttributedTypeSyntax( | ||
specifiers: TypeSpecifierListSyntax { | ||
SimpleTypeSpecifierSyntax(specifier: .keyword(.inout)) | ||
}, | ||
baseType: IdentifierTypeSyntax(name: .type(.Hasher)) | ||
) | ||
) | ||
} | ||
), | ||
body: CodeBlockSyntax(statements: body) | ||
) | ||
} | ||
|
||
@DeclModifierListBuilder | ||
var modifiers: DeclModifierListSyntax { | ||
DeclModifierSyntax(name: stringsTable.accessLevel.token) | ||
} | ||
|
||
@CodeBlockItemListBuilder | ||
var body: CodeBlockItemListSyntax { | ||
for property in stringsTable.comparableProperties { | ||
FunctionCallExprSyntax(callee: MemberAccessExprSyntax(hasherToken, "combine")) { | ||
LabeledExprSyntax(expression: DeclReferenceExprSyntax(baseName: property.name)) | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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
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
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
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
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.