Skip to content

Commit 6d5fc5b

Browse files
committed
Introduce a placeholder declaration to represent editor placeholders in member decl lists
1 parent a0cf4cd commit 6d5fc5b

File tree

20 files changed

+884
-4
lines changed

20 files changed

+884
-4
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,4 +1677,15 @@ public let DECL_NODES: [Node] = [
16771677
collectionElementName: "AdditionalTrailingClosure")
16781678
]),
16791679

1680+
Node(name: "EditorPlaceholderDecl",
1681+
nameForDiagnostics: "editor placeholder",
1682+
kind: "Decl",
1683+
children: [
1684+
Child(name: "Identifier",
1685+
kind: "IdentifierToken",
1686+
tokenChoices: [
1687+
"Identifier"
1688+
])
1689+
]),
1690+
16801691
]

Sources/SwiftParser/Declarations.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ extension Parser {
257257
return RawDeclSyntax(self.parseLetOrVarDeclaration(attrs, .missing(.keyword(.var))))
258258
}
259259

260+
if self.currentToken.isEditorPlaceholder {
261+
let placeholder = self.consumeAnyToken()
262+
return RawDeclSyntax(
263+
RawEditorPlaceholderDeclSyntax(
264+
identifier: placeholder,
265+
arena: self.arena
266+
)
267+
)
268+
}
269+
260270
let isProbablyFuncDecl = self.at(any: [.identifier, .wildcard]) || self.at(anyIn: Operator.self) != nil
261271

262272
if isProbablyFuncDecl {

Sources/SwiftSyntax/Documentation.docc/gyb_generated/SwiftSyntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
7979
- <doc:SwiftSyntax/PrecedenceGroupDeclSyntax>
8080
- <doc:SwiftSyntax/MacroDeclSyntax>
8181
- <doc:SwiftSyntax/MacroExpansionDeclSyntax>
82+
- <doc:SwiftSyntax/EditorPlaceholderDeclSyntax>
8283

8384
### Statements
8485

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public struct RawDeclSyntax: RawDeclSyntaxNodeProtocol {
3434

3535
public static func isKindOf(_ raw: RawSyntax) -> Bool {
3636
switch raw.kind {
37-
case .missingDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl, .macroDecl, .macroExpansionDecl: return true
37+
case .missingDecl, .typealiasDecl, .associatedtypeDecl, .ifConfigDecl, .poundErrorDecl, .poundWarningDecl, .poundSourceLocation, .classDecl, .actorDecl, .structDecl, .protocolDecl, .extensionDecl, .functionDecl, .initializerDecl, .deinitializerDecl, .subscriptDecl, .importDecl, .accessorDecl, .variableDecl, .enumCaseDecl, .enumDecl, .operatorDecl, .precedenceGroupDecl, .macroDecl, .macroExpansionDecl, .editorPlaceholderDecl: return true
3838
default: return false
3939
}
4040
}
@@ -10301,6 +10301,56 @@ public struct RawMacroExpansionDeclSyntax: RawDeclSyntaxNodeProtocol {
1030110301
}
1030210302
}
1030310303

10304+
@_spi(RawSyntax)
10305+
public struct RawEditorPlaceholderDeclSyntax: RawDeclSyntaxNodeProtocol {
10306+
10307+
@_spi(RawSyntax)
10308+
public var layoutView: RawSyntaxLayoutView {
10309+
return raw.layoutView!
10310+
}
10311+
10312+
public static func isKindOf(_ raw: RawSyntax) -> Bool {
10313+
return raw.kind == .editorPlaceholderDecl
10314+
}
10315+
10316+
public var raw: RawSyntax
10317+
init(raw: RawSyntax) {
10318+
assert(Self.isKindOf(raw))
10319+
self.raw = raw
10320+
}
10321+
10322+
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
10323+
guard Self.isKindOf(other.raw) else { return nil }
10324+
self.init(raw: other.raw)
10325+
}
10326+
10327+
public init(
10328+
_ unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? = nil,
10329+
identifier: RawTokenSyntax,
10330+
_ unexpectedAfterIdentifier: RawUnexpectedNodesSyntax? = nil,
10331+
arena: __shared SyntaxArena
10332+
) {
10333+
let raw = RawSyntax.makeLayout(
10334+
kind: .editorPlaceholderDecl, uninitializedCount: 3, arena: arena) { layout in
10335+
layout.initialize(repeating: nil)
10336+
layout[0] = unexpectedBeforeIdentifier?.raw
10337+
layout[1] = identifier.raw
10338+
layout[2] = unexpectedAfterIdentifier?.raw
10339+
}
10340+
self.init(raw: raw)
10341+
}
10342+
10343+
public var unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? {
10344+
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
10345+
}
10346+
public var identifier: RawTokenSyntax {
10347+
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
10348+
}
10349+
public var unexpectedAfterIdentifier: RawUnexpectedNodesSyntax? {
10350+
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
10351+
}
10352+
}
10353+
1030410354
@_spi(RawSyntax)
1030510355
public struct RawTokenListSyntax: RawSyntaxNodeProtocol {
1030610356

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxValidation.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,12 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
15301530
assertNoError(kind, 15, verify(layout[15], as: RawMultipleTrailingClosureElementListSyntax?.self))
15311531
assertNoError(kind, 16, verify(layout[16], as: RawUnexpectedNodesSyntax?.self))
15321532
break
1533+
case .editorPlaceholderDecl:
1534+
assert(layout.count == 3)
1535+
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
1536+
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self))
1537+
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
1538+
break
15331539
case .tokenList:
15341540
for (index, element) in layout.enumerated() {
15351541
assertNoError(kind, index, verify(element, as: RawTokenSyntax.self))

Sources/SwiftSyntax/generated/Misc.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ extension Syntax {
104104
.node(DocumentationAttributeArgumentSyntax.self),
105105
.node(DocumentationAttributeArgumentsSyntax.self),
106106
.node(DynamicReplacementArgumentsSyntax.self),
107+
.node(EditorPlaceholderDeclSyntax.self),
107108
.node(EditorPlaceholderExprSyntax.self),
108109
.node(EffectsArgumentsSyntax.self),
109110
.node(EnumCaseDeclSyntax.self),
@@ -457,6 +458,8 @@ extension SyntaxKind {
457458
return DocumentationAttributeArgumentsSyntax.self
458459
case .dynamicReplacementArguments:
459460
return DynamicReplacementArgumentsSyntax.self
461+
case .editorPlaceholderDecl:
462+
return EditorPlaceholderDeclSyntax.self
460463
case .editorPlaceholderExpr:
461464
return EditorPlaceholderExprSyntax.self
462465
case .effectsArguments:
@@ -984,6 +987,8 @@ extension SyntaxKind {
984987
return "@_documentation arguments"
985988
case .dynamicReplacementArguments:
986989
return "@_dynamicReplacement argument"
990+
case .editorPlaceholderDecl:
991+
return "editor placeholder"
987992
case .editorPlaceholderExpr:
988993
return "editor placeholder"
989994
case .effectsArguments:

Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
737737
visitAnyPost(node._syntaxNode)
738738
}
739739

740+
override open func visit(_ node: EditorPlaceholderDeclSyntax) -> SyntaxVisitorContinueKind {
741+
return visitAny(node._syntaxNode)
742+
}
743+
744+
override open func visitPost(_ node: EditorPlaceholderDeclSyntax) {
745+
visitAnyPost(node._syntaxNode)
746+
}
747+
740748
override open func visit(_ node: EditorPlaceholderExprSyntax) -> SyntaxVisitorContinueKind {
741749
return visitAny(node._syntaxNode)
742750
}

Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
6969

7070
public init? < S: SyntaxProtocol > (_ node: S) {
7171
switch node.raw.kind {
72-
case .accessorDecl, .actorDecl, .associatedtypeDecl, .classDecl, .deinitializerDecl, .enumCaseDecl, .enumDecl, .extensionDecl, .functionDecl, .ifConfigDecl, .importDecl, .initializerDecl, .macroDecl, .macroExpansionDecl, .missingDecl, .operatorDecl, .poundErrorDecl, .poundSourceLocation, .poundWarningDecl, .precedenceGroupDecl, .protocolDecl, .structDecl, .subscriptDecl, .typealiasDecl, .variableDecl:
72+
case .accessorDecl, .actorDecl, .associatedtypeDecl, .classDecl, .deinitializerDecl, .editorPlaceholderDecl, .enumCaseDecl, .enumDecl, .extensionDecl, .functionDecl, .ifConfigDecl, .importDecl, .initializerDecl, .macroDecl, .macroExpansionDecl, .missingDecl, .operatorDecl, .poundErrorDecl, .poundSourceLocation, .poundWarningDecl, .precedenceGroupDecl, .protocolDecl, .structDecl, .subscriptDecl, .typealiasDecl, .variableDecl:
7373
self._syntaxNode = node._syntaxNode
7474
default:
7575
return nil
@@ -82,7 +82,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
8282
internal init(_ data: SyntaxData) {
8383
#if DEBUG
8484
switch data.raw.kind {
85-
case .accessorDecl, .actorDecl, .associatedtypeDecl, .classDecl, .deinitializerDecl, .enumCaseDecl, .enumDecl, .extensionDecl, .functionDecl, .ifConfigDecl, .importDecl, .initializerDecl, .macroDecl, .macroExpansionDecl, .missingDecl, .operatorDecl, .poundErrorDecl, .poundSourceLocation, .poundWarningDecl, .precedenceGroupDecl, .protocolDecl, .structDecl, .subscriptDecl, .typealiasDecl, .variableDecl:
85+
case .accessorDecl, .actorDecl, .associatedtypeDecl, .classDecl, .deinitializerDecl, .editorPlaceholderDecl, .enumCaseDecl, .enumDecl, .extensionDecl, .functionDecl, .ifConfigDecl, .importDecl, .initializerDecl, .macroDecl, .macroExpansionDecl, .missingDecl, .operatorDecl, .poundErrorDecl, .poundSourceLocation, .poundWarningDecl, .precedenceGroupDecl, .protocolDecl, .structDecl, .subscriptDecl, .typealiasDecl, .variableDecl:
8686
break
8787
default:
8888
fatalError("Unable to create DeclSyntax from \(data.raw.kind)")
@@ -123,6 +123,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
123123
.node(AssociatedtypeDeclSyntax.self),
124124
.node(ClassDeclSyntax.self),
125125
.node(DeinitializerDeclSyntax.self),
126+
.node(EditorPlaceholderDeclSyntax.self),
126127
.node(EnumCaseDeclSyntax.self),
127128
.node(EnumDeclSyntax.self),
128129
.node(ExtensionDeclSyntax.self),

Sources/SwiftSyntax/generated/SyntaxEnum.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ public enum SyntaxEnum {
188188

189189
case dynamicReplacementArguments(DynamicReplacementArgumentsSyntax)
190190

191+
case editorPlaceholderDecl(EditorPlaceholderDeclSyntax)
192+
191193
case editorPlaceholderExpr(EditorPlaceholderExprSyntax)
192194

193195
case effectsArguments(EffectsArgumentsSyntax)
@@ -715,6 +717,8 @@ public extension Syntax {
715717
return .documentationAttributeArguments(DocumentationAttributeArgumentsSyntax(self)!)
716718
case .dynamicReplacementArguments:
717719
return .dynamicReplacementArguments(DynamicReplacementArgumentsSyntax(self)!)
720+
case .editorPlaceholderDecl:
721+
return .editorPlaceholderDecl(EditorPlaceholderDeclSyntax(self)!)
718722
case .editorPlaceholderExpr:
719723
return .editorPlaceholderExpr(EditorPlaceholderExprSyntax(self)!)
720724
case .effectsArguments:

Sources/SwiftSyntax/generated/SyntaxKind.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ public enum SyntaxKind {
188188

189189
case dynamicReplacementArguments
190190

191+
case editorPlaceholderDecl
192+
191193
case editorPlaceholderExpr
192194

193195
case effectsArguments

Sources/SwiftSyntax/generated/SyntaxTransform.swift

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

449+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
450+
/// - Parameter node: the node we are visiting.
451+
/// - Returns: the sum of whatever the child visitors return.
452+
func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType
453+
449454
/// Visiting `EditorPlaceholderExprSyntax` specifically.
450455
/// - Parameter node: the node we are visiting.
451456
/// - Returns: the sum of whatever the child visitors return.
@@ -1922,6 +1927,13 @@ extension SyntaxTransformVisitor {
19221927
visitAny(Syntax(node))
19231928
}
19241929

1930+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
1931+
/// - Parameter node: the node we are visiting.
1932+
/// - Returns: nil by default.
1933+
public func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType {
1934+
visitAny(Syntax(node))
1935+
}
1936+
19251937
/// Visiting `EditorPlaceholderExprSyntax` specifically.
19261938
/// - Parameter node: the node we are visiting.
19271939
/// - Returns: nil by default.
@@ -3321,6 +3333,8 @@ extension SyntaxTransformVisitor {
33213333
return visit(derived)
33223334
case .dynamicReplacementArguments(let derived):
33233335
return visit(derived)
3336+
case .editorPlaceholderDecl(let derived):
3337+
return visit(derived)
33243338
case .editorPlaceholderExpr(let derived):
33253339
return visit(derived)
33263340
case .effectsArguments(let derived):

Sources/SwiftSyntax/generated/SyntaxVisitor.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,18 @@ open class SyntaxVisitor {
10481048
open func visitPost(_ node: DynamicReplacementArgumentsSyntax) {
10491049
}
10501050

1051+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
1052+
/// - Parameter node: the node we are visiting.
1053+
/// - Returns: how should we continue visiting.
1054+
open func visit(_ node: EditorPlaceholderDeclSyntax) -> SyntaxVisitorContinueKind {
1055+
return .visitChildren
1056+
}
1057+
1058+
/// The function called after visiting `EditorPlaceholderDeclSyntax` and its descendents.
1059+
/// - node: the node we just finished visiting.
1060+
open func visitPost(_ node: EditorPlaceholderDeclSyntax) {
1061+
}
1062+
10511063
/// Visiting `EditorPlaceholderExprSyntax` specifically.
10521064
/// - Parameter node: the node we are visiting.
10531065
/// - Returns: how should we continue visiting.
@@ -4095,6 +4107,17 @@ open class SyntaxVisitor {
40954107
visitPost(node)
40964108
}
40974109

4110+
/// Implementation detail of doVisit(_:_:). Do not call directly.
4111+
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) {
4112+
let node = EditorPlaceholderDeclSyntax(data)
4113+
let needsChildren = (visit(node) == .visitChildren)
4114+
// Avoid calling into visitChildren if possible.
4115+
if needsChildren && !node.raw.layoutView!.children.isEmpty {
4116+
visitChildren(node)
4117+
}
4118+
visitPost(node)
4119+
}
4120+
40984121
/// Implementation detail of doVisit(_:_:). Do not call directly.
40994122
private func visitImplEditorPlaceholderExprSyntax(_ data: SyntaxData) {
41004123
let node = EditorPlaceholderExprSyntax(data)
@@ -6197,6 +6220,8 @@ open class SyntaxVisitor {
61976220
visitImplDocumentationAttributeArgumentsSyntax(data)
61986221
case .dynamicReplacementArguments:
61996222
visitImplDynamicReplacementArgumentsSyntax(data)
6223+
case .editorPlaceholderDecl:
6224+
visitImplEditorPlaceholderDeclSyntax(data)
62006225
case .editorPlaceholderExpr:
62016226
visitImplEditorPlaceholderExprSyntax(data)
62026227
case .effectsArguments:

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4848,6 +4848,33 @@ public enum SyntaxFactory {
48484848
return MacroExpansionDeclSyntax(data)
48494849
}
48504850
}
4851+
@available(*, deprecated, message: "Use initializer on EditorPlaceholderDeclSyntax")
4852+
public static func makeEditorPlaceholderDecl(_ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil, identifier: TokenSyntax, _ unexpectedAfterIdentifier: UnexpectedNodesSyntax? = nil) -> EditorPlaceholderDeclSyntax {
4853+
let layout: [RawSyntax?] = [
4854+
unexpectedBeforeIdentifier?.raw,
4855+
identifier.raw,
4856+
unexpectedAfterIdentifier?.raw,
4857+
]
4858+
return withExtendedLifetime(SyntaxArena()) { arena in
4859+
let raw = RawSyntax.makeLayout(kind: SyntaxKind.editorPlaceholderDecl,
4860+
from: layout, arena: arena)
4861+
let data = SyntaxData.forRoot(raw)
4862+
return EditorPlaceholderDeclSyntax(data)
4863+
}
4864+
}
4865+
4866+
@available(*, deprecated, message: "Use initializer on EditorPlaceholderDeclSyntax")
4867+
public static func makeBlankEditorPlaceholderDecl(presence: SourcePresence = .present) -> EditorPlaceholderDeclSyntax {
4868+
return withExtendedLifetime(SyntaxArena()) { arena in
4869+
let data = SyntaxData.forRoot(RawSyntax.makeLayout(kind: .editorPlaceholderDecl,
4870+
from: [
4871+
nil,
4872+
RawSyntax.makeMissingToken(kind: TokenKind.identifier(""), arena: arena),
4873+
nil,
4874+
], arena: arena))
4875+
return EditorPlaceholderDeclSyntax(data)
4876+
}
4877+
}
48514878
@available(*, deprecated, message: "Use initializer on TokenListSyntax")
48524879
public static func makeTokenList(
48534880
_ elements: [TokenSyntax]) -> TokenListSyntax {

Sources/SwiftSyntax/gyb_generated/SyntaxRewriter.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,13 @@ open class SyntaxRewriter {
10381038
return DeclSyntax(visitChildren(node))
10391039
}
10401040

1041+
/// Visit a `EditorPlaceholderDeclSyntax`.
1042+
/// - Parameter node: the node that is being visited
1043+
/// - Returns: the rewritten node
1044+
open func visit(_ node: EditorPlaceholderDeclSyntax) -> DeclSyntax {
1045+
return DeclSyntax(visitChildren(node))
1046+
}
1047+
10411048
/// Visit a `TokenListSyntax`.
10421049
/// - Parameter node: the node that is being visited
10431050
/// - Returns: the rewritten node
@@ -3367,6 +3374,16 @@ open class SyntaxRewriter {
33673374
return Syntax(visit(node))
33683375
}
33693376

3377+
/// Implementation detail of visit(_:). Do not call directly.
3378+
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) -> Syntax {
3379+
let node = EditorPlaceholderDeclSyntax(data)
3380+
// Accessing _syntaxNode directly is faster than calling Syntax(node)
3381+
visitPre(node._syntaxNode)
3382+
defer { visitPost(node._syntaxNode) }
3383+
if let newNode = visitAny(node._syntaxNode) { return newNode }
3384+
return Syntax(visit(node))
3385+
}
3386+
33703387
/// Implementation detail of visit(_:). Do not call directly.
33713388
private func visitImplTokenListSyntax(_ data: SyntaxData) -> Syntax {
33723389
let node = TokenListSyntax(data)
@@ -4847,6 +4864,8 @@ open class SyntaxRewriter {
48474864
return visitImplMacroDeclSyntax
48484865
case .macroExpansionDecl:
48494866
return visitImplMacroExpansionDeclSyntax
4867+
case .editorPlaceholderDecl:
4868+
return visitImplEditorPlaceholderDeclSyntax
48504869
case .tokenList:
48514870
return visitImplTokenListSyntax
48524871
case .attribute:
@@ -5380,6 +5399,8 @@ open class SyntaxRewriter {
53805399
return visitImplMacroDeclSyntax(data)
53815400
case .macroExpansionDecl:
53825401
return visitImplMacroExpansionDeclSyntax(data)
5402+
case .editorPlaceholderDecl:
5403+
return visitImplEditorPlaceholderDeclSyntax(data)
53835404
case .tokenList:
53845405
return visitImplTokenListSyntax(data)
53855406
case .attribute:

0 commit comments

Comments
 (0)