-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Version 1.0.1 #25
Closed
Closed
Version 1.0.1 #25
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ff24c2d
Update templates
87da560
Add rootView comment
6b38703
[Helpers] Add SceneDelegate
310d49e
Remove Alerts
e56891e
[Helpers] Add CoordinatorSceneFlowProvider
b454f15
Add Macro for enum identifiers
63b342b
Add ModalCoverModel
05eec5a
Update examples
24ccf4f
Move macro to separate repository
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
14 changes: 14 additions & 0 deletions
14
Sources/EnumIdentifiersGenerator/EnumIdentifiersGenerator.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,14 @@ | ||
// | ||
// EnumIdentifiersGenerator.swift | ||
// | ||
// | ||
// Created by Simon Sestak on 31/07/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
@attached(member, names: arbitrary) | ||
public macro EnumIdentifiersGenerator() = #externalMacro( | ||
module: "EnumIdentifiersGeneratorMacro", | ||
type: "EnumIdentifiersGeneratorMacro" | ||
) |
119 changes: 119 additions & 0 deletions
119
Sources/EnumIdentifiersGeneratorMacro/EnumIdentifiersGeneratorMacro.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,119 @@ | ||
// | ||
// EnumIdentifiersGeneratorMacro.swift | ||
// | ||
// | ||
// Created by Simon Sestak on 31/07/2024. | ||
// | ||
|
||
import SwiftCompilerPlugin | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
import SwiftDiagnostics | ||
import Foundation | ||
|
||
public struct EnumIdentifiersGeneratorMacro: MemberMacro { | ||
public static func expansion<Declaration, Context>( | ||
of node: SwiftSyntax.AttributeSyntax, | ||
providingMembersOf declaration: Declaration, | ||
in context: Context) throws -> [SwiftSyntax.DeclSyntax] where Declaration : SwiftSyntax.DeclGroupSyntax, Context : SwiftSyntaxMacros.MacroExpansionContext { | ||
|
||
guard let declaration = declaration.as(EnumDeclSyntax.self) else { | ||
let enumError = Diagnostic(node: node._syntaxNode, message: Diagnostics.mustBeEnum) | ||
context.diagnose(enumError) | ||
return [] | ||
} | ||
|
||
guard let enumCases: [SyntaxProtocol] = declaration.memberBlock | ||
.children(viewMode: .fixedUp).filter({ $0.kind == .memberDeclList }) | ||
.first? | ||
.children(viewMode: .fixedUp).filter({ $0.kind == SyntaxKind.memberDeclListItem }) | ||
.flatMap({ $0.children(viewMode: .fixedUp).filter({ $0.kind == .enumCaseDecl })}) | ||
.flatMap({ $0.children(viewMode: .fixedUp).filter({ $0.kind == .enumCaseElementList })}) | ||
.flatMap({ $0.children(viewMode: .fixedUp).filter({ $0.kind == .enumCaseElement })}) | ||
else { | ||
let enumError = Diagnostic(node: node._syntaxNode, message: Diagnostics.mustHaveCases) | ||
context.diagnose(enumError) | ||
return [] | ||
} | ||
|
||
let caseIds: [String] = enumCases.compactMap { enumCase in | ||
guard let firstToken = enumCase.firstToken(viewMode: .fixedUp) else { | ||
return nil | ||
} | ||
|
||
guard case let .identifier(id) = firstToken.tokenKind else { | ||
return nil | ||
} | ||
|
||
return id | ||
} | ||
|
||
guard !caseIds.isEmpty else { | ||
let enumError = Diagnostic(node: node._syntaxNode, message: Diagnostics.mustHaveCases) | ||
context.diagnose(enumError) | ||
return [] | ||
} | ||
|
||
let modifier = declaration.hasPublicModifier ? "public " : "" | ||
let enumID = "\(modifier)enum CaseID: String, Hashable, CaseIterable, CustomStringConvertible {\n\(caseIds.map { " case \($0)\n" }.joined())\n \(modifier)var description: String {\nself.rawValue\n }\n}" | ||
let idAccessor = "\(modifier)var caseId: CaseID {\n switch self {\n\(caseIds.map { " case .\($0): .\($0)\n" }.joined()) }\n}" | ||
let identifierVariable = "var id: String { self.caseId.rawValue }" | ||
let hashableConformance = "func hash(into hasher: inout Hasher) {\nhasher.combine(id)\n}" | ||
let comparableConformance = "static func == (lhs: Self, rhs: Self) -> Bool {\nlhs.id == rhs.id\n}" | ||
|
||
return [ | ||
DeclSyntax(stringLiteral: enumID), | ||
DeclSyntax(stringLiteral: idAccessor), | ||
DeclSyntax(stringLiteral: identifierVariable), | ||
DeclSyntax(stringLiteral: hashableConformance), | ||
DeclSyntax(stringLiteral: comparableConformance) | ||
] | ||
} | ||
|
||
public enum Diagnostics: String, DiagnosticMessage { | ||
|
||
case mustBeEnum, mustHaveCases | ||
|
||
public var message: String { | ||
switch self { | ||
case .mustBeEnum: | ||
return "`@EnumIdentifiersGeneratorMacro` can only be applied to an `enum`" | ||
case .mustHaveCases: | ||
return "`@EnumIdentifiersGeneratorMacro` can only be applied to an `enum` with `case` statements" | ||
} | ||
} | ||
|
||
public var diagnosticID: MessageID { | ||
MessageID(domain: "EnumIdentifiersGeneratorMacro", id: rawValue) | ||
} | ||
|
||
public var severity: DiagnosticSeverity { .error } | ||
} | ||
} | ||
|
||
private extension DeclGroupSyntax { | ||
var hasPublicModifier: Bool { | ||
self.modifiers.children(viewMode: .fixedUp) | ||
.compactMap { syntax in | ||
syntax.as(DeclModifierSyntax.self)? | ||
.children(viewMode: .fixedUp) | ||
.contains { syntax in | ||
switch syntax.as(TokenSyntax.self)?.tokenKind { | ||
case .keyword(.public): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
} | ||
.contains(true) | ||
} | ||
} | ||
|
||
@main | ||
struct MacroPlugin: CompilerPlugin { | ||
let providingMacros: [Macro.Type] = [ | ||
EnumIdentifiersGeneratorMacro.self, | ||
] | ||
} |
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
22 changes: 22 additions & 0 deletions
22
Sources/FuturedArchitecture/Architecture/ModalCoverModel.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 @@ | ||||||
// | ||||||
// File.swift | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// | ||||||
// | ||||||
// Created by Simon Sestak on 01/08/2024. | ||||||
// | ||||||
|
||||||
import Foundation | ||||||
|
||||||
public struct ModalCoverModel<Destination: Hashable & Identifiable>: Identifiable { | ||||||
let destination: Destination | ||||||
let style: Style | ||||||
|
||||||
enum Style { | ||||||
case sheet | ||||||
case fullscreenCover | ||||||
} | ||||||
|
||||||
public var id: Destination.ID { | ||||||
destination.id | ||||||
} | ||||||
} |
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.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EnumIdentifiersGenerator
zvážil bych jiné jméno :) 🤔