Skip to content

Commit 3cff8d7

Browse files
committed
Add editor place holder pattern
1 parent 59f04f4 commit 3cff8d7

File tree

20 files changed

+287
-7
lines changed

20 files changed

+287
-7
lines changed

CodeGeneration/Sources/SyntaxSupport/PatternNodes.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,22 @@ public let PATTERN_NODES: [Node] = [
182182
]
183183
),
184184

185+
Node(
186+
kind: .editorPlaceholderPattern,
187+
base: .pattern,
188+
nameForDiagnostics: "editor placeholder",
189+
documentation: """
190+
An editor placeholder, e.g. `<#pattern#>` that is used in a position that expects a pattern.
191+
""",
192+
children: [
193+
Child(
194+
name: "placeholder",
195+
kind: .token(choices: [.token(.identifier)]),
196+
documentation: """
197+
The actual editor placeholder that starts with `<#` and ends with `#>`.
198+
"""
199+
)
200+
]
201+
),
202+
185203
]

CodeGeneration/Sources/SyntaxSupport/SyntaxNodeKind.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public enum SyntaxNodeKind: String, CaseIterable {
111111
case dynamicReplacementAttributeArguments
112112
case editorPlaceholderDecl
113113
case editorPlaceholderExpr
114+
case editorPlaceholderPattern
114115
case effectsAttributeArgumentList
115116
case enumCaseDecl
116117
case enumCaseElement

Release Notes/510.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
- Description: Remarks are used by the Swift compiler and other tools to describe some aspect of translation that doesn't reflect correctness, but may be useful for the user. Remarks have been added to the diagnostic severity enums to align with the Swift compiler.
2222
- Pull Request: https://github.com/apple/swift-syntax/pull/2143
2323

24+
- New pattern node `EditorPlaceholderPatternSyntax`
25+
- Description: This node type will be placeholder that is used in a position that expects a pattern.
26+
- Issue: https://github.com/apple/swift-syntax/issues/2147
27+
- Pull Request: https://github.com/apple/swift-syntax/pull/2150
28+
2429
## API Behavior Changes
2530

2631
## Deprecations

Sources/SwiftParser/Patterns.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,21 @@ extension Parser {
7070
)
7171
case (.lhs(.identifier), let handle)?:
7272
let identifier = self.eat(handle)
73-
return RawPatternSyntax(
74-
RawIdentifierPatternSyntax(
75-
identifier: identifier,
76-
arena: self.arena
73+
if identifier.tokenText.isEditorPlaceholder {
74+
return RawPatternSyntax(
75+
RawEditorPlaceholderPatternSyntax(
76+
placeholder: identifier,
77+
arena: self.arena
78+
)
7779
)
78-
)
80+
} else {
81+
return RawPatternSyntax(
82+
RawIdentifierPatternSyntax(
83+
identifier: identifier,
84+
arena: self.arena
85+
)
86+
)
87+
}
7988
case (.lhs(.dollarIdentifier), let handle)?:
8089
let dollarIdent = self.eat(handle)
8190
let unexpectedBeforeIdentifier = RawUnexpectedNodesSyntax(elements: [RawSyntax(dollarIdent)], arena: self.arena)

Sources/SwiftParserDiagnostics/generated/SyntaxKindNameForDiagnostics.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ extension SyntaxKind {
143143
return "editor placeholder"
144144
case .editorPlaceholderExpr:
145145
return "editor placeholder"
146+
case .editorPlaceholderPattern:
147+
return "editor placeholder"
146148
case .effectsAttributeArgumentList:
147149
return "@_effects arguments"
148150
case .enumCaseDecl:

Sources/SwiftSyntax/Documentation.docc/generated/SwiftSyntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ These articles are intended for developers wishing to contribute to SwiftSyntax
151151
- <doc:SwiftSyntax/PatternSyntax>
152152
- <doc:SwiftSyntax/PatternSyntaxProtocol>
153153
- <doc:SwiftSyntax/MissingPatternSyntax>
154+
- <doc:SwiftSyntax/EditorPlaceholderPatternSyntax>
154155
- <doc:SwiftSyntax/ExpressionPatternSyntax>
155156
- <doc:SwiftSyntax/IdentifierPatternSyntax>
156157
- <doc:SwiftSyntax/IsTypePatternSyntax>

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,12 @@ public func childName(_ keyPath: AnyKeyPath) -> String? {
10771077
return "placeholder"
10781078
case \EditorPlaceholderExprSyntax.unexpectedAfterPlaceholder:
10791079
return "unexpectedAfterPlaceholder"
1080+
case \EditorPlaceholderPatternSyntax.unexpectedBeforePlaceholder:
1081+
return "unexpectedBeforePlaceholder"
1082+
case \EditorPlaceholderPatternSyntax.placeholder:
1083+
return "placeholder"
1084+
case \EditorPlaceholderPatternSyntax.unexpectedAfterPlaceholder:
1085+
return "unexpectedAfterPlaceholder"
10801086
case \EnumCaseDeclSyntax.unexpectedBeforeAttributes:
10811087
return "unexpectedBeforeAttributes"
10821088
case \EnumCaseDeclSyntax.attributes:

Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
776776
visitAnyPost(node._syntaxNode)
777777
}
778778

779+
override open func visit(_ node: EditorPlaceholderPatternSyntax) -> SyntaxVisitorContinueKind {
780+
return visitAny(node._syntaxNode)
781+
}
782+
783+
override open func visitPost(_ node: EditorPlaceholderPatternSyntax) {
784+
visitAnyPost(node._syntaxNode)
785+
}
786+
779787
override open func visit(_ node: EffectsAttributeArgumentListSyntax) -> SyntaxVisitorContinueKind {
780788
return visitAny(node._syntaxNode)
781789
}

Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
770770

771771
public init?(_ node: some SyntaxProtocol) {
772772
switch node.raw.kind {
773-
case .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
773+
case .editorPlaceholderPattern, .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
774774
self._syntaxNode = node._syntaxNode
775775
default:
776776
return nil
@@ -795,6 +795,7 @@ public struct PatternSyntax: PatternSyntaxProtocol, SyntaxHashable {
795795

796796
public static var structure: SyntaxNodeStructure {
797797
return .choices([
798+
.node(EditorPlaceholderPatternSyntax.self),
798799
.node(ExpressionPatternSyntax.self),
799800
.node(IdentifierPatternSyntax.self),
800801
.node(IsTypePatternSyntax.self),
@@ -1486,6 +1487,7 @@ extension Syntax {
14861487
.node(DynamicReplacementAttributeArgumentsSyntax.self),
14871488
.node(EditorPlaceholderDeclSyntax.self),
14881489
.node(EditorPlaceholderExprSyntax.self),
1490+
.node(EditorPlaceholderPatternSyntax.self),
14891491
.node(EffectsAttributeArgumentListSyntax.self),
14901492
.node(EnumCaseDeclSyntax.self),
14911493
.node(EnumCaseElementListSyntax.self),

Sources/SwiftSyntax/generated/SyntaxEnum.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public enum SyntaxEnum {
105105
case dynamicReplacementAttributeArguments(DynamicReplacementAttributeArgumentsSyntax)
106106
case editorPlaceholderDecl(EditorPlaceholderDeclSyntax)
107107
case editorPlaceholderExpr(EditorPlaceholderExprSyntax)
108+
case editorPlaceholderPattern(EditorPlaceholderPatternSyntax)
108109
case effectsAttributeArgumentList(EffectsAttributeArgumentListSyntax)
109110
case enumCaseDecl(EnumCaseDeclSyntax)
110111
case enumCaseElementList(EnumCaseElementListSyntax)
@@ -479,6 +480,8 @@ public extension Syntax {
479480
return .editorPlaceholderDecl(EditorPlaceholderDeclSyntax(self)!)
480481
case .editorPlaceholderExpr:
481482
return .editorPlaceholderExpr(EditorPlaceholderExprSyntax(self)!)
483+
case .editorPlaceholderPattern:
484+
return .editorPlaceholderPattern(EditorPlaceholderPatternSyntax(self)!)
482485
case .effectsAttributeArgumentList:
483486
return .effectsAttributeArgumentList(EffectsAttributeArgumentListSyntax(self)!)
484487
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxKind.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public enum SyntaxKind: CaseIterable {
105105
case dynamicReplacementAttributeArguments
106106
case editorPlaceholderDecl
107107
case editorPlaceholderExpr
108+
case editorPlaceholderPattern
108109
case effectsAttributeArgumentList
109110
case enumCaseDecl
110111
case enumCaseElementList
@@ -600,6 +601,8 @@ public enum SyntaxKind: CaseIterable {
600601
return EditorPlaceholderDeclSyntax.self
601602
case .editorPlaceholderExpr:
602603
return EditorPlaceholderExprSyntax.self
604+
case .editorPlaceholderPattern:
605+
return EditorPlaceholderPatternSyntax.self
603606
case .effectsAttributeArgumentList:
604607
return EffectsAttributeArgumentListSyntax.self
605608
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxRewriter.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,13 @@ open class SyntaxRewriter {
710710
return ExprSyntax(visitChildren(node))
711711
}
712712

713+
/// Visit a ``EditorPlaceholderPatternSyntax``.
714+
/// - Parameter node: the node that is being visited
715+
/// - Returns: the rewritten node
716+
open func visit(_ node: EditorPlaceholderPatternSyntax) -> PatternSyntax {
717+
return PatternSyntax(visitChildren(node))
718+
}
719+
713720
/// Visit a ``EffectsAttributeArgumentListSyntax``.
714721
/// - Parameter node: the node that is being visited
715722
/// - Returns: the rewritten node
@@ -2456,6 +2463,10 @@ open class SyntaxRewriter {
24562463
return {
24572464
self.visitImpl($0, EditorPlaceholderExprSyntax.self, self.visit)
24582465
}
2466+
case .editorPlaceholderPattern:
2467+
return {
2468+
self.visitImpl($0, EditorPlaceholderPatternSyntax.self, self.visit)
2469+
}
24592470
case .effectsAttributeArgumentList:
24602471
return {
24612472
self.visitImpl($0, EffectsAttributeArgumentListSyntax.self, self.visit)
@@ -3390,6 +3401,8 @@ open class SyntaxRewriter {
33903401
return visitImpl(node, EditorPlaceholderDeclSyntax.self, visit)
33913402
case .editorPlaceholderExpr:
33923403
return visitImpl(node, EditorPlaceholderExprSyntax.self, visit)
3404+
case .editorPlaceholderPattern:
3405+
return visitImpl(node, EditorPlaceholderPatternSyntax.self, visit)
33933406
case .effectsAttributeArgumentList:
33943407
return visitImpl(node, EffectsAttributeArgumentListSyntax.self, visit)
33953408
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxTransform.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,11 @@ public protocol SyntaxTransformVisitor {
470470
/// - Returns: the sum of whatever the child visitors return.
471471
func visit(_ node: EditorPlaceholderExprSyntax) -> ResultType
472472

473+
/// Visiting ``EditorPlaceholderPatternSyntax`` specifically.
474+
/// - Parameter node: the node we are visiting.
475+
/// - Returns: the sum of whatever the child visitors return.
476+
func visit(_ node: EditorPlaceholderPatternSyntax) -> ResultType
477+
473478
/// Visiting ``EffectsAttributeArgumentListSyntax`` specifically.
474479
/// - Parameter node: the node we are visiting.
475480
/// - Returns: the sum of whatever the child visitors return.
@@ -2036,6 +2041,13 @@ extension SyntaxTransformVisitor {
20362041
visitAny(Syntax(node))
20372042
}
20382043

2044+
/// Visiting ``EditorPlaceholderPatternSyntax`` specifically.
2045+
/// - Parameter node: the node we are visiting.
2046+
/// - Returns: nil by default.
2047+
public func visit(_ node: EditorPlaceholderPatternSyntax) -> ResultType {
2048+
visitAny(Syntax(node))
2049+
}
2050+
20392051
/// Visiting ``EffectsAttributeArgumentListSyntax`` specifically.
20402052
/// - Parameter node: the node we are visiting.
20412053
/// - Returns: nil by default.
@@ -3522,6 +3534,8 @@ extension SyntaxTransformVisitor {
35223534
return visit(derived)
35233535
case .editorPlaceholderExpr(let derived):
35243536
return visit(derived)
3537+
case .editorPlaceholderPattern(let derived):
3538+
return visit(derived)
35253539
case .effectsAttributeArgumentList(let derived):
35263540
return visit(derived)
35273541
case .enumCaseDecl(let derived):

Sources/SwiftSyntax/generated/SyntaxVisitor.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,18 @@ open class SyntaxVisitor {
11141114
open func visitPost(_ node: EditorPlaceholderExprSyntax) {
11151115
}
11161116

1117+
/// Visiting ``EditorPlaceholderPatternSyntax`` specifically.
1118+
/// - Parameter node: the node we are visiting.
1119+
/// - Returns: how should we continue visiting.
1120+
open func visit(_ node: EditorPlaceholderPatternSyntax) -> SyntaxVisitorContinueKind {
1121+
return .visitChildren
1122+
}
1123+
1124+
/// The function called after visiting ``EditorPlaceholderPatternSyntax`` and its descendants.
1125+
/// - node: the node we just finished visiting.
1126+
open func visitPost(_ node: EditorPlaceholderPatternSyntax) {
1127+
}
1128+
11171129
/// Visiting ``EffectsAttributeArgumentListSyntax`` specifically.
11181130
/// - Parameter node: the node we are visiting.
11191131
/// - Returns: how should we continue visiting.
@@ -3768,6 +3780,10 @@ open class SyntaxVisitor {
37683780
return {
37693781
self.visitImpl($0, EditorPlaceholderExprSyntax.self, self.visit, self.visitPost)
37703782
}
3783+
case .editorPlaceholderPattern:
3784+
return {
3785+
self.visitImpl($0, EditorPlaceholderPatternSyntax.self, self.visit, self.visitPost)
3786+
}
37713787
case .effectsAttributeArgumentList:
37723788
return {
37733789
self.visitImpl($0, EffectsAttributeArgumentListSyntax.self, self.visit, self.visitPost)
@@ -4705,6 +4721,8 @@ open class SyntaxVisitor {
47054721
visitImpl(node, EditorPlaceholderDeclSyntax.self, visit, visitPost)
47064722
case .editorPlaceholderExpr:
47074723
visitImpl(node, EditorPlaceholderExprSyntax.self, visit, visitPost)
4724+
case .editorPlaceholderPattern:
4725+
visitImpl(node, EditorPlaceholderPatternSyntax.self, visit, visitPost)
47084726
case .effectsAttributeArgumentList:
47094727
visitImpl(node, EffectsAttributeArgumentListSyntax.self, visit, visitPost)
47104728
case .enumCaseDecl:

Sources/SwiftSyntax/generated/raw/RawSyntaxNodesEF.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,64 @@ public struct RawEditorPlaceholderExprSyntax: RawExprSyntaxNodeProtocol {
141141
}
142142
}
143143

144+
@_spi(RawSyntax)
145+
public struct RawEditorPlaceholderPatternSyntax: RawPatternSyntaxNodeProtocol {
146+
@_spi(RawSyntax)
147+
public var layoutView: RawSyntaxLayoutView {
148+
return raw.layoutView!
149+
}
150+
151+
public static func isKindOf(_ raw: RawSyntax) -> Bool {
152+
return raw.kind == .editorPlaceholderPattern
153+
}
154+
155+
public var raw: RawSyntax
156+
157+
init(raw: RawSyntax) {
158+
precondition(Self.isKindOf(raw))
159+
self.raw = raw
160+
}
161+
162+
private init(unchecked raw: RawSyntax) {
163+
self.raw = raw
164+
}
165+
166+
public init?(_ other: some RawSyntaxNodeProtocol) {
167+
guard Self.isKindOf(other.raw) else {
168+
return nil
169+
}
170+
self.init(unchecked: other.raw)
171+
}
172+
173+
public init(
174+
_ unexpectedBeforePlaceholder: RawUnexpectedNodesSyntax? = nil,
175+
placeholder: RawTokenSyntax,
176+
_ unexpectedAfterPlaceholder: RawUnexpectedNodesSyntax? = nil,
177+
arena: __shared SyntaxArena
178+
) {
179+
let raw = RawSyntax.makeLayout(
180+
kind: .editorPlaceholderPattern, uninitializedCount: 3, arena: arena) { layout in
181+
layout.initialize(repeating: nil)
182+
layout[0] = unexpectedBeforePlaceholder?.raw
183+
layout[1] = placeholder.raw
184+
layout[2] = unexpectedAfterPlaceholder?.raw
185+
}
186+
self.init(unchecked: raw)
187+
}
188+
189+
public var unexpectedBeforePlaceholder: RawUnexpectedNodesSyntax? {
190+
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
191+
}
192+
193+
public var placeholder: RawTokenSyntax {
194+
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
195+
}
196+
197+
public var unexpectedAfterPlaceholder: RawUnexpectedNodesSyntax? {
198+
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
199+
}
200+
}
201+
144202
@_spi(RawSyntax)
145203
public struct RawEffectsAttributeArgumentListSyntax: RawSyntaxNodeProtocol {
146204
@_spi(RawSyntax)

Sources/SwiftSyntax/generated/raw/RawSyntaxNodesOP.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ public struct RawPatternSyntax: RawPatternSyntaxNodeProtocol {
12221222

12231223
public static func isKindOf(_ raw: RawSyntax) -> Bool {
12241224
switch raw.kind {
1225-
case .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
1225+
case .editorPlaceholderPattern, .expressionPattern, .identifierPattern, .isTypePattern, .missingPattern, .tuplePattern, .valueBindingPattern, .wildcardPattern:
12261226
return true
12271227
default:
12281228
return false

Sources/SwiftSyntax/generated/raw/RawSyntaxValidation.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,11 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
10351035
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
10361036
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.identifier)]))
10371037
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
1038+
case .editorPlaceholderPattern:
1039+
assert(layout.count == 3)
1040+
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
1041+
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self, tokenChoices: [.tokenKind(.identifier)]))
1042+
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
10381043
case .effectsAttributeArgumentList:
10391044
for (index, element) in layout.enumerated() {
10401045
assertNoError(kind, index, verify(element, as: RawTokenSyntax.self))

0 commit comments

Comments
 (0)