Skip to content

Commit 7215983

Browse files
authored
Merge pull request #2150 from kimdv/kimdv/2147-sometimes-the-placeholder-is-identified-as-identifierpattern
Handle pattern as editor placeholder
2 parents 59f04f4 + 6d507ec commit 7215983

25 files changed

+672
-17
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public enum SyntaxNodeKind: String, CaseIterable {
111111
case dynamicReplacementAttributeArguments
112112
case editorPlaceholderDecl
113113
case editorPlaceholderExpr
114+
case editorPlaceholderPattern
115+
case editorPlaceholderType
114116
case effectsAttributeArgumentList
115117
case enumCaseDecl
116118
case enumCaseElement

CodeGeneration/Sources/SyntaxSupport/TypeNodes.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,28 @@ public let TYPE_NODES: [Node] = [
530530
]
531531
),
532532

533+
Node(
534+
kind: .editorPlaceholderType,
535+
base: .type,
536+
nameForDiagnostics: "editor placeholder",
537+
documentation: """
538+
An editor placeholder, e.g. `<#Type#>` that is used in a position that expects a type.
539+
""",
540+
children: [
541+
Child(
542+
name: "placeholder",
543+
kind: .token(choices: [.token(.identifier)]),
544+
documentation: """
545+
The actual editor placeholder that starts with `<#` and ends with `#>`.
546+
"""
547+
),
548+
Child(
549+
name: "genericArgumentClause",
550+
kind: .node(kind: .genericArgumentClause),
551+
documentation: "Generic arguments that is attatched to the type.",
552+
isOptional: true
553+
),
554+
]
555+
),
556+
533557
]

Release Notes/510.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
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+
29+
- New type node `EditorPlaceholderTypeSyntax`
30+
- Description: This node type will be placeholder that is used in a position that expects a type.
31+
- Issue: https://github.com/apple/swift-syntax/issues/2156
32+
- Pull Request: https://github.com/apple/swift-syntax/pull/2150
33+
2434
## API Behavior Changes
2535

2636
## 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/SwiftParser/Types.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,25 @@ extension Parser {
349349
generics = nil
350350
}
351351

352-
return RawTypeSyntax(
353-
RawIdentifierTypeSyntax(
354-
unexpectedBeforeName,
355-
name: name,
356-
genericArgumentClause: generics,
357-
arena: self.arena
352+
if name.tokenText.isEditorPlaceholder {
353+
return RawTypeSyntax(
354+
RawEditorPlaceholderTypeSyntax(
355+
placeholder: name,
356+
genericArgumentClause: generics,
357+
arena: self.arena
358+
)
358359
)
359-
)
360+
} else {
361+
return RawTypeSyntax(
362+
RawIdentifierTypeSyntax(
363+
unexpectedBeforeName,
364+
name: name,
365+
genericArgumentClause: generics,
366+
arena: self.arena
367+
)
368+
)
369+
}
370+
360371
}
361372

362373
/// Parse the existential `Any` type.
@@ -938,7 +949,9 @@ extension Parser {
938949

939950
extension Parser {
940951
mutating func parseResultType() -> RawTypeSyntax {
941-
if self.at(prefix: "<") {
952+
if self.at(prefix: "<#") {
953+
return self.parseTypeIdentifier()
954+
} else if self.at(prefix: "<") {
942955
let generics = self.parseGenericParameters()
943956
let baseType = self.parseType()
944957
return RawTypeSyntax(

Sources/SwiftParserDiagnostics/generated/SyntaxKindNameForDiagnostics.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ extension SyntaxKind {
143143
return "editor placeholder"
144144
case .editorPlaceholderExpr:
145145
return "editor placeholder"
146+
case .editorPlaceholderPattern:
147+
return "editor placeholder"
148+
case .editorPlaceholderType:
149+
return "editor placeholder"
146150
case .effectsAttributeArgumentList:
147151
return "@_effects arguments"
148152
case .enumCaseDecl:

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

Lines changed: 2 additions & 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>
@@ -190,6 +191,7 @@ These articles are intended for developers wishing to contribute to SwiftSyntax
190191
- <doc:SwiftSyntax/ClassRestrictionTypeSyntax>
191192
- <doc:SwiftSyntax/CompositionTypeSyntax>
192193
- <doc:SwiftSyntax/DictionaryTypeSyntax>
194+
- <doc:SwiftSyntax/EditorPlaceholderTypeSyntax>
193195
- <doc:SwiftSyntax/FunctionTypeSyntax>
194196
- <doc:SwiftSyntax/IdentifierTypeSyntax>
195197
- <doc:SwiftSyntax/ImplicitlyUnwrappedOptionalTypeSyntax>

Sources/SwiftSyntax/generated/ChildNameForKeyPath.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,22 @@ 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"
1086+
case \EditorPlaceholderTypeSyntax.unexpectedBeforePlaceholder:
1087+
return "unexpectedBeforePlaceholder"
1088+
case \EditorPlaceholderTypeSyntax.placeholder:
1089+
return "placeholder"
1090+
case \EditorPlaceholderTypeSyntax.unexpectedBetweenPlaceholderAndGenericArgumentClause:
1091+
return "unexpectedBetweenPlaceholderAndGenericArgumentClause"
1092+
case \EditorPlaceholderTypeSyntax.genericArgumentClause:
1093+
return "genericArgumentClause"
1094+
case \EditorPlaceholderTypeSyntax.unexpectedAfterGenericArgumentClause:
1095+
return "unexpectedAfterGenericArgumentClause"
10801096
case \EnumCaseDeclSyntax.unexpectedBeforeAttributes:
10811097
return "unexpectedBeforeAttributes"
10821098
case \EnumCaseDeclSyntax.attributes:

Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,22 @@ 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+
787+
override open func visit(_ node: EditorPlaceholderTypeSyntax) -> SyntaxVisitorContinueKind {
788+
return visitAny(node._syntaxNode)
789+
}
790+
791+
override open func visitPost(_ node: EditorPlaceholderTypeSyntax) {
792+
visitAnyPost(node._syntaxNode)
793+
}
794+
779795
override open func visit(_ node: EffectsAttributeArgumentListSyntax) -> SyntaxVisitorContinueKind {
780796
return visitAny(node._syntaxNode)
781797
}

Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift

Lines changed: 6 additions & 2 deletions
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),
@@ -1298,7 +1299,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
12981299

12991300
public init?(_ node: some SyntaxProtocol) {
13001301
switch node.raw.kind {
1301-
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .dictionaryType, .functionType, .identifierType, .implicitlyUnwrappedOptionalType, .memberType, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packElementType, .packExpansionType, .someOrAnyType, .suppressedType, .tupleType:
1302+
case .arrayType, .attributedType, .classRestrictionType, .compositionType, .dictionaryType, .editorPlaceholderType, .functionType, .identifierType, .implicitlyUnwrappedOptionalType, .memberType, .metatypeType, .missingType, .namedOpaqueReturnType, .optionalType, .packElementType, .packExpansionType, .someOrAnyType, .suppressedType, .tupleType:
13021303
self._syntaxNode = node._syntaxNode
13031304
default:
13041305
return nil
@@ -1328,6 +1329,7 @@ public struct TypeSyntax: TypeSyntaxProtocol, SyntaxHashable {
13281329
.node(ClassRestrictionTypeSyntax.self),
13291330
.node(CompositionTypeSyntax.self),
13301331
.node(DictionaryTypeSyntax.self),
1332+
.node(EditorPlaceholderTypeSyntax.self),
13311333
.node(FunctionTypeSyntax.self),
13321334
.node(IdentifierTypeSyntax.self),
13331335
.node(ImplicitlyUnwrappedOptionalTypeSyntax.self),
@@ -1486,6 +1488,8 @@ extension Syntax {
14861488
.node(DynamicReplacementAttributeArgumentsSyntax.self),
14871489
.node(EditorPlaceholderDeclSyntax.self),
14881490
.node(EditorPlaceholderExprSyntax.self),
1491+
.node(EditorPlaceholderPatternSyntax.self),
1492+
.node(EditorPlaceholderTypeSyntax.self),
14891493
.node(EffectsAttributeArgumentListSyntax.self),
14901494
.node(EnumCaseDeclSyntax.self),
14911495
.node(EnumCaseElementListSyntax.self),

Sources/SwiftSyntax/generated/SyntaxEnum.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public enum SyntaxEnum {
105105
case dynamicReplacementAttributeArguments(DynamicReplacementAttributeArgumentsSyntax)
106106
case editorPlaceholderDecl(EditorPlaceholderDeclSyntax)
107107
case editorPlaceholderExpr(EditorPlaceholderExprSyntax)
108+
case editorPlaceholderPattern(EditorPlaceholderPatternSyntax)
109+
case editorPlaceholderType(EditorPlaceholderTypeSyntax)
108110
case effectsAttributeArgumentList(EffectsAttributeArgumentListSyntax)
109111
case enumCaseDecl(EnumCaseDeclSyntax)
110112
case enumCaseElementList(EnumCaseElementListSyntax)
@@ -479,6 +481,10 @@ public extension Syntax {
479481
return .editorPlaceholderDecl(EditorPlaceholderDeclSyntax(self)!)
480482
case .editorPlaceholderExpr:
481483
return .editorPlaceholderExpr(EditorPlaceholderExprSyntax(self)!)
484+
case .editorPlaceholderPattern:
485+
return .editorPlaceholderPattern(EditorPlaceholderPatternSyntax(self)!)
486+
case .editorPlaceholderType:
487+
return .editorPlaceholderType(EditorPlaceholderTypeSyntax(self)!)
482488
case .effectsAttributeArgumentList:
483489
return .effectsAttributeArgumentList(EffectsAttributeArgumentListSyntax(self)!)
484490
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxKind.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public enum SyntaxKind: CaseIterable {
105105
case dynamicReplacementAttributeArguments
106106
case editorPlaceholderDecl
107107
case editorPlaceholderExpr
108+
case editorPlaceholderPattern
109+
case editorPlaceholderType
108110
case effectsAttributeArgumentList
109111
case enumCaseDecl
110112
case enumCaseElementList
@@ -600,6 +602,10 @@ public enum SyntaxKind: CaseIterable {
600602
return EditorPlaceholderDeclSyntax.self
601603
case .editorPlaceholderExpr:
602604
return EditorPlaceholderExprSyntax.self
605+
case .editorPlaceholderPattern:
606+
return EditorPlaceholderPatternSyntax.self
607+
case .editorPlaceholderType:
608+
return EditorPlaceholderTypeSyntax.self
603609
case .effectsAttributeArgumentList:
604610
return EffectsAttributeArgumentListSyntax.self
605611
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxRewriter.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,20 @@ 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+
720+
/// Visit a ``EditorPlaceholderTypeSyntax``.
721+
/// - Parameter node: the node that is being visited
722+
/// - Returns: the rewritten node
723+
open func visit(_ node: EditorPlaceholderTypeSyntax) -> TypeSyntax {
724+
return TypeSyntax(visitChildren(node))
725+
}
726+
713727
/// Visit a ``EffectsAttributeArgumentListSyntax``.
714728
/// - Parameter node: the node that is being visited
715729
/// - Returns: the rewritten node
@@ -2456,6 +2470,14 @@ open class SyntaxRewriter {
24562470
return {
24572471
self.visitImpl($0, EditorPlaceholderExprSyntax.self, self.visit)
24582472
}
2473+
case .editorPlaceholderPattern:
2474+
return {
2475+
self.visitImpl($0, EditorPlaceholderPatternSyntax.self, self.visit)
2476+
}
2477+
case .editorPlaceholderType:
2478+
return {
2479+
self.visitImpl($0, EditorPlaceholderTypeSyntax.self, self.visit)
2480+
}
24592481
case .effectsAttributeArgumentList:
24602482
return {
24612483
self.visitImpl($0, EffectsAttributeArgumentListSyntax.self, self.visit)
@@ -3390,6 +3412,10 @@ open class SyntaxRewriter {
33903412
return visitImpl(node, EditorPlaceholderDeclSyntax.self, visit)
33913413
case .editorPlaceholderExpr:
33923414
return visitImpl(node, EditorPlaceholderExprSyntax.self, visit)
3415+
case .editorPlaceholderPattern:
3416+
return visitImpl(node, EditorPlaceholderPatternSyntax.self, visit)
3417+
case .editorPlaceholderType:
3418+
return visitImpl(node, EditorPlaceholderTypeSyntax.self, visit)
33933419
case .effectsAttributeArgumentList:
33943420
return visitImpl(node, EffectsAttributeArgumentListSyntax.self, visit)
33953421
case .enumCaseDecl:

0 commit comments

Comments
 (0)