-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
import SwiftCompilerPlugin | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
@main | ||
struct VergeMacroPlugin: CompilerPlugin { | ||
let providingMacros: [Macro.Type] = [ | ||
//@freestanding(expression) public macro | ||
// #externalMacro(module: "MacroExamplesPlugin", type: "FontLiteralMacro") | ||
|
||
] | ||
} | ||
/** | ||
struct Database: DatabaseType { | ||
|
||
struct Schema: EntitySchemaType { | ||
let author = Author.EntityTableKey() | ||
} | ||
|
||
struct Indexes: IndexesType { | ||
let allBooks = HashIndex<Schema, String, Author>.Key() | ||
} | ||
|
||
var _backingStorage: BackingStorage = .init() | ||
} | ||
*/ | ||
|
||
@attached(member, names: named(_backingStorage)) | ||
@attached(conformance) | ||
public macro DatabaseState() = #externalMacro(module: "VergeMacrosPlugin", type: "DatabaseStateMacro") |
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,48 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
public struct DatabaseStateMacro { | ||
|
||
} | ||
|
||
extension DatabaseStateMacro: ConformanceMacro { | ||
public static func expansion<Declaration, Context>( | ||
of node: SwiftSyntax.AttributeSyntax, | ||
providingConformancesOf declaration: Declaration, | ||
in context: Context | ||
) throws -> [(SwiftSyntax.TypeSyntax, SwiftSyntax.GenericWhereClauseSyntax?)] | ||
where Declaration: SwiftSyntax.DeclGroupSyntax, Context: SwiftSyntaxMacros.MacroExpansionContext { | ||
|
||
// Decode the expansion arguments. | ||
guard let structDecl = declaration.as(StructDeclSyntax.self) else { | ||
// context.diagnose(OptionSetMacroDiagnostic.requiresStruct.diagnose(at: decl)) | ||
return [] | ||
} | ||
|
||
// If there is an explicit conformance to OptionSet already, don't add one. | ||
if let inheritedTypes = structDecl.inheritanceClause?.inheritedTypeCollection, | ||
inheritedTypes.contains(where: { inherited in inherited.typeName.trimmedDescription == "DatabaseType" }) { | ||
return [] | ||
} | ||
|
||
return [("DatabaseType", nil)] | ||
} | ||
|
||
} | ||
|
||
extension DatabaseStateMacro: 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 { | ||
|
||
return [ | ||
"var _backingStorage: BackingStorage = .init()" | ||
] | ||
|
||
} | ||
} |
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,16 @@ | ||
import SwiftCompilerPlugin | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
import SwiftSyntaxMacros | ||
|
||
struct _Macro: AttachedMacro { | ||
|
||
} | ||
|
||
|
||
@main | ||
struct VergeMacroPlugin: CompilerPlugin { | ||
let providingMacros: [Macro.Type] = [ | ||
DatabaseStateMacro.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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
// File.swift | ||
// | ||
// | ||
// Created by Muukii on 2023/06/12. | ||
// Created by Muukii on 2023/06/17. | ||
// | ||
|
||
import Foundation |
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