Skip to content

Commit 454c729

Browse files
committed
Introduce a placeholder declaration to represent editor placeholders in member decl lists
1 parent 8b21dbf commit 454c729

File tree

20 files changed

+357
-3
lines changed

20 files changed

+357
-3
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,4 +1614,15 @@ public let DECL_NODES: [Node] = [
16141614
collectionElementName: "AdditionalTrailingClosure")
16151615
]),
16161616

1617+
Node(name: "EditorPlaceholderDecl",
1618+
nameForDiagnostics: "editor placeholder",
1619+
kind: "Decl",
1620+
children: [
1621+
Child(name: "Identifier",
1622+
kind: "IdentifierToken",
1623+
tokenChoices: [
1624+
"Identifier"
1625+
])
1626+
]),
1627+
16171628
]

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(.varKeyword)))
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, .wildcardKeyword]) || 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
@@ -102,6 +102,7 @@ extension Syntax {
102102
.node(DifferentiableAttributeArgumentsSyntax.self),
103103
.node(DiscardAssignmentExprSyntax.self),
104104
.node(DoStmtSyntax.self),
105+
.node(EditorPlaceholderDeclSyntax.self),
105106
.node(EditorPlaceholderExprSyntax.self),
106107
.node(EnumCaseDeclSyntax.self),
107108
.node(EnumCaseElementListSyntax.self),
@@ -448,6 +449,8 @@ extension SyntaxKind {
448449
return DiscardAssignmentExprSyntax.self
449450
case .doStmt:
450451
return DoStmtSyntax.self
452+
case .editorPlaceholderDecl:
453+
return EditorPlaceholderDeclSyntax.self
451454
case .editorPlaceholderExpr:
452455
return EditorPlaceholderExprSyntax.self
453456
case .enumCaseDecl:
@@ -965,6 +968,8 @@ extension SyntaxKind {
965968
return nil
966969
case .doStmt:
967970
return "'do' statement"
971+
case .editorPlaceholderDecl:
972+
return "editor placeholder"
968973
case .editorPlaceholderExpr:
969974
return "editor placeholder"
970975
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
721721
visitAnyPost(node._syntaxNode)
722722
}
723723

724+
override open func visit(_ node: EditorPlaceholderDeclSyntax) -> SyntaxVisitorContinueKind {
725+
return visitAny(node._syntaxNode)
726+
}
727+
728+
override open func visitPost(_ node: EditorPlaceholderDeclSyntax) {
729+
visitAnyPost(node._syntaxNode)
730+
}
731+
724732
override open func visit(_ node: EditorPlaceholderExprSyntax) -> SyntaxVisitorContinueKind {
725733
return visitAny(node._syntaxNode)
726734
}

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
@@ -184,6 +184,8 @@ public enum SyntaxEnum {
184184

185185
case doStmt(DoStmtSyntax)
186186

187+
case editorPlaceholderDecl(EditorPlaceholderDeclSyntax)
188+
187189
case editorPlaceholderExpr(EditorPlaceholderExprSyntax)
188190

189191
case enumCaseDecl(EnumCaseDeclSyntax)
@@ -701,6 +703,8 @@ public extension Syntax {
701703
return .discardAssignmentExpr(DiscardAssignmentExprSyntax(self)!)
702704
case .doStmt:
703705
return .doStmt(DoStmtSyntax(self)!)
706+
case .editorPlaceholderDecl:
707+
return .editorPlaceholderDecl(EditorPlaceholderDeclSyntax(self)!)
704708
case .editorPlaceholderExpr:
705709
return .editorPlaceholderExpr(EditorPlaceholderExprSyntax(self)!)
706710
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxKind.swift

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

185185
case doStmt
186186

187+
case editorPlaceholderDecl
188+
187189
case editorPlaceholderExpr
188190

189191
case enumCaseDecl

Sources/SwiftSyntax/generated/SyntaxTransform.swift

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

439+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
440+
/// - Parameter node: the node we are visiting.
441+
/// - Returns: the sum of whatever the child visitors return.
442+
func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType
443+
439444
/// Visiting `EditorPlaceholderExprSyntax` specifically.
440445
/// - Parameter node: the node we are visiting.
441446
/// - Returns: the sum of whatever the child visitors return.
@@ -1883,6 +1888,13 @@ extension SyntaxTransformVisitor {
18831888
visitAny(Syntax(node))
18841889
}
18851890

1891+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
1892+
/// - Parameter node: the node we are visiting.
1893+
/// - Returns: nil by default.
1894+
public func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType {
1895+
visitAny(Syntax(node))
1896+
}
1897+
18861898
/// Visiting `EditorPlaceholderExprSyntax` specifically.
18871899
/// - Parameter node: the node we are visiting.
18881900
/// - Returns: nil by default.
@@ -3257,6 +3269,8 @@ extension SyntaxTransformVisitor {
32573269
return visit(derived)
32583270
case .doStmt(let derived):
32593271
return visit(derived)
3272+
case .editorPlaceholderDecl(let derived):
3273+
return visit(derived)
32603274
case .editorPlaceholderExpr(let derived):
32613275
return visit(derived)
32623276
case .enumCaseDecl(let derived):

Sources/SwiftSyntax/generated/SyntaxVisitor.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,18 @@ open class SyntaxVisitor {
10241024
open func visitPost(_ node: DoStmtSyntax) {
10251025
}
10261026

1027+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
1028+
/// - Parameter node: the node we are visiting.
1029+
/// - Returns: how should we continue visiting.
1030+
open func visit(_ node: EditorPlaceholderDeclSyntax) -> SyntaxVisitorContinueKind {
1031+
return .visitChildren
1032+
}
1033+
1034+
/// The function called after visiting `EditorPlaceholderDeclSyntax` and its descendents.
1035+
/// - node: the node we just finished visiting.
1036+
open func visitPost(_ node: EditorPlaceholderDeclSyntax) {
1037+
}
1038+
10271039
/// Visiting `EditorPlaceholderExprSyntax` specifically.
10281040
/// - Parameter node: the node we are visiting.
10291041
/// - Returns: how should we continue visiting.
@@ -4013,6 +4025,17 @@ open class SyntaxVisitor {
40134025
visitPost(node)
40144026
}
40154027

4028+
/// Implementation detail of doVisit(_:_:). Do not call directly.
4029+
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) {
4030+
let node = EditorPlaceholderDeclSyntax(data)
4031+
let needsChildren = (visit(node) == .visitChildren)
4032+
// Avoid calling into visitChildren if possible.
4033+
if needsChildren && !node.raw.layoutView!.children.isEmpty {
4034+
visitChildren(node)
4035+
}
4036+
visitPost(node)
4037+
}
4038+
40164039
/// Implementation detail of doVisit(_:_:). Do not call directly.
40174040
private func visitImplEditorPlaceholderExprSyntax(_ data: SyntaxData) {
40184041
let node = EditorPlaceholderExprSyntax(data)
@@ -6078,6 +6101,8 @@ open class SyntaxVisitor {
60786101
visitImplDiscardAssignmentExprSyntax(data)
60796102
case .doStmt:
60806103
visitImplDoStmtSyntax(data)
6104+
case .editorPlaceholderDecl:
6105+
visitImplEditorPlaceholderDeclSyntax(data)
60816106
case .editorPlaceholderExpr:
60826107
visitImplEditorPlaceholderExprSyntax(data)
60836108
case .enumCaseDecl:

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
@@ -3332,6 +3339,16 @@ open class SyntaxRewriter {
33323339
return Syntax(visit(node))
33333340
}
33343341

3342+
/// Implementation detail of visit(_:). Do not call directly.
3343+
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) -> Syntax {
3344+
let node = EditorPlaceholderDeclSyntax(data)
3345+
// Accessing _syntaxNode directly is faster than calling Syntax(node)
3346+
visitPre(node._syntaxNode)
3347+
defer { visitPost(node._syntaxNode) }
3348+
if let newNode = visitAny(node._syntaxNode) { return newNode }
3349+
return Syntax(visit(node))
3350+
}
3351+
33353352
/// Implementation detail of visit(_:). Do not call directly.
33363353
private func visitImplTokenListSyntax(_ data: SyntaxData) -> Syntax {
33373354
let node = TokenListSyntax(data)
@@ -4762,6 +4779,8 @@ open class SyntaxRewriter {
47624779
return visitImplMacroDeclSyntax
47634780
case .macroExpansionDecl:
47644781
return visitImplMacroExpansionDeclSyntax
4782+
case .editorPlaceholderDecl:
4783+
return visitImplEditorPlaceholderDeclSyntax
47654784
case .tokenList:
47664785
return visitImplTokenListSyntax
47674786
case .customAttribute:
@@ -5285,6 +5304,8 @@ open class SyntaxRewriter {
52855304
return visitImplMacroDeclSyntax(data)
52865305
case .macroExpansionDecl:
52875306
return visitImplMacroExpansionDeclSyntax(data)
5307+
case .editorPlaceholderDecl:
5308+
return visitImplEditorPlaceholderDeclSyntax(data)
52885309
case .tokenList:
52895310
return visitImplTokenListSyntax(data)
52905311
case .customAttribute:

0 commit comments

Comments
 (0)