Skip to content

Commit 9662db2

Browse files
committed
Introduce a placeholder declaration to represent editor placeholders in member decl lists
1 parent cc9b4b6 commit 9662db2

File tree

19 files changed

+343
-3
lines changed

19 files changed

+343
-3
lines changed

CodeGeneration/Sources/SyntaxSupport/gyb_generated/DeclNodes.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,4 +1594,15 @@ public let DECL_NODES: [Node] = [
15941594
collectionElementName: "AdditionalTrailingClosure")
15951595
]),
15961596

1597+
Node(name: "EditorPlaceholderDecl",
1598+
nameForDiagnostics: "editor placeholder",
1599+
kind: "Decl",
1600+
children: [
1601+
Child(name: "Identifier",
1602+
kind: "IdentifierToken",
1603+
tokenChoices: [
1604+
"Identifier"
1605+
])
1606+
]),
1607+
15971608
]

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
@@ -74,6 +74,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
7474
- <doc:SwiftSyntax/PrecedenceGroupDeclSyntax>
7575
- <doc:SwiftSyntax/MacroDeclSyntax>
7676
- <doc:SwiftSyntax/MacroExpansionDeclSyntax>
77+
- <doc:SwiftSyntax/EditorPlaceholderDeclSyntax>
7778

7879
### Statements
7980

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
}
@@ -10417,6 +10417,56 @@ public struct RawMacroExpansionDeclSyntax: RawDeclSyntaxNodeProtocol {
1041710417
}
1041810418
}
1041910419

10420+
@_spi(RawSyntax)
10421+
public struct RawEditorPlaceholderDeclSyntax: RawDeclSyntaxNodeProtocol {
10422+
10423+
@_spi(RawSyntax)
10424+
public var layoutView: RawSyntaxLayoutView {
10425+
return raw.layoutView!
10426+
}
10427+
10428+
public static func isKindOf(_ raw: RawSyntax) -> Bool {
10429+
return raw.kind == .editorPlaceholderDecl
10430+
}
10431+
10432+
public var raw: RawSyntax
10433+
init(raw: RawSyntax) {
10434+
assert(Self.isKindOf(raw))
10435+
self.raw = raw
10436+
}
10437+
10438+
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
10439+
guard Self.isKindOf(other.raw) else { return nil }
10440+
self.init(raw: other.raw)
10441+
}
10442+
10443+
public init(
10444+
_ unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? = nil,
10445+
identifier: RawTokenSyntax,
10446+
_ unexpectedAfterIdentifier: RawUnexpectedNodesSyntax? = nil,
10447+
arena: __shared SyntaxArena
10448+
) {
10449+
let raw = RawSyntax.makeLayout(
10450+
kind: .editorPlaceholderDecl, uninitializedCount: 3, arena: arena) { layout in
10451+
layout.initialize(repeating: nil)
10452+
layout[0] = unexpectedBeforeIdentifier?.raw
10453+
layout[1] = identifier.raw
10454+
layout[2] = unexpectedAfterIdentifier?.raw
10455+
}
10456+
self.init(raw: raw)
10457+
}
10458+
10459+
public var unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? {
10460+
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
10461+
}
10462+
public var identifier: RawTokenSyntax {
10463+
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
10464+
}
10465+
public var unexpectedAfterIdentifier: RawUnexpectedNodesSyntax? {
10466+
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
10467+
}
10468+
}
10469+
1042010470
@_spi(RawSyntax)
1042110471
public struct RawTokenListSyntax: RawSyntaxNodeProtocol {
1042210472

Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxValidation.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,12 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
15451545
assertNoError(kind, 15, verify(layout[15], as: RawMultipleTrailingClosureElementListSyntax?.self))
15461546
assertNoError(kind, 16, verify(layout[16], as: RawUnexpectedNodesSyntax?.self))
15471547
break
1548+
case .editorPlaceholderDecl:
1549+
assert(layout.count == 3)
1550+
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
1551+
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self))
1552+
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
1553+
break
15481554
case .tokenList:
15491555
for (index, element) in layout.enumerated() {
15501556
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
@@ -105,6 +105,7 @@ extension Syntax {
105105
.node(DifferentiableAttributeArgumentsSyntax.self),
106106
.node(DiscardAssignmentExprSyntax.self),
107107
.node(DoStmtSyntax.self),
108+
.node(EditorPlaceholderDeclSyntax.self),
108109
.node(EditorPlaceholderExprSyntax.self),
109110
.node(EnumCaseDeclSyntax.self),
110111
.node(EnumCaseElementListSyntax.self),
@@ -463,6 +464,8 @@ extension SyntaxKind {
463464
return DiscardAssignmentExprSyntax.self
464465
case .doStmt:
465466
return DoStmtSyntax.self
467+
case .editorPlaceholderDecl:
468+
return EditorPlaceholderDeclSyntax.self
466469
case .editorPlaceholderExpr:
467470
return EditorPlaceholderExprSyntax.self
468471
case .enumCaseDecl:
@@ -998,6 +1001,8 @@ extension SyntaxKind {
9981001
return nil
9991002
case .doStmt:
10001003
return "'do' statement"
1004+
case .editorPlaceholderDecl:
1005+
return "editor placeholder"
10011006
case .editorPlaceholderExpr:
10021007
return "editor placeholder"
10031008
case .enumCaseDecl:

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

191191
case doStmt(DoStmtSyntax)
192192

193+
case editorPlaceholderDecl(EditorPlaceholderDeclSyntax)
194+
193195
case editorPlaceholderExpr(EditorPlaceholderExprSyntax)
194196

195197
case enumCaseDecl(EnumCaseDeclSyntax)
@@ -725,6 +727,8 @@ public extension Syntax {
725727
return .discardAssignmentExpr(DiscardAssignmentExprSyntax(self)!)
726728
case .doStmt:
727729
return .doStmt(DoStmtSyntax(self)!)
730+
case .editorPlaceholderDecl:
731+
return .editorPlaceholderDecl(EditorPlaceholderDeclSyntax(self)!)
728732
case .editorPlaceholderExpr:
729733
return .editorPlaceholderExpr(EditorPlaceholderExprSyntax(self)!)
730734
case .enumCaseDecl:

Sources/SwiftSyntax/generated/SyntaxKind.swift

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

191191
case doStmt
192192

193+
case editorPlaceholderDecl
194+
193195
case editorPlaceholderExpr
194196

195197
case enumCaseDecl

Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4906,6 +4906,33 @@ public enum SyntaxFactory {
49064906
return MacroExpansionDeclSyntax(data)
49074907
}
49084908
}
4909+
@available(*, deprecated, message: "Use initializer on EditorPlaceholderDeclSyntax")
4910+
public static func makeEditorPlaceholderDecl(_ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil, identifier: TokenSyntax, _ unexpectedAfterIdentifier: UnexpectedNodesSyntax? = nil) -> EditorPlaceholderDeclSyntax {
4911+
let layout: [RawSyntax?] = [
4912+
unexpectedBeforeIdentifier?.raw,
4913+
identifier.raw,
4914+
unexpectedAfterIdentifier?.raw,
4915+
]
4916+
return withExtendedLifetime(SyntaxArena()) { arena in
4917+
let raw = RawSyntax.makeLayout(kind: SyntaxKind.editorPlaceholderDecl,
4918+
from: layout, arena: arena)
4919+
let data = SyntaxData.forRoot(raw)
4920+
return EditorPlaceholderDeclSyntax(data)
4921+
}
4922+
}
4923+
4924+
@available(*, deprecated, message: "Use initializer on EditorPlaceholderDeclSyntax")
4925+
public static func makeBlankEditorPlaceholderDecl(presence: SourcePresence = .present) -> EditorPlaceholderDeclSyntax {
4926+
return withExtendedLifetime(SyntaxArena()) { arena in
4927+
let data = SyntaxData.forRoot(RawSyntax.makeLayout(kind: .editorPlaceholderDecl,
4928+
from: [
4929+
nil,
4930+
RawSyntax.makeMissingToken(kind: TokenKind.identifier(""), arena: arena),
4931+
nil,
4932+
], arena: arena))
4933+
return EditorPlaceholderDeclSyntax(data)
4934+
}
4935+
}
49094936
@available(*, deprecated, message: "Use initializer on TokenListSyntax")
49104937
public static func makeTokenList(
49114938
_ elements: [TokenSyntax]) -> TokenListSyntax {

Sources/SwiftSyntax/gyb_generated/SyntaxRewriter.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,13 @@ open class SyntaxRewriter {
10521052
return DeclSyntax(visitChildren(node))
10531053
}
10541054

1055+
/// Visit a `EditorPlaceholderDeclSyntax`.
1056+
/// - Parameter node: the node that is being visited
1057+
/// - Returns: the rewritten node
1058+
open func visit(_ node: EditorPlaceholderDeclSyntax) -> DeclSyntax {
1059+
return DeclSyntax(visitChildren(node))
1060+
}
1061+
10551062
/// Visit a `TokenListSyntax`.
10561063
/// - Parameter node: the node that is being visited
10571064
/// - Returns: the rewritten node
@@ -3415,6 +3422,16 @@ open class SyntaxRewriter {
34153422
return Syntax(visit(node))
34163423
}
34173424

3425+
/// Implementation detail of visit(_:). Do not call directly.
3426+
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) -> Syntax {
3427+
let node = EditorPlaceholderDeclSyntax(data)
3428+
// Accessing _syntaxNode directly is faster than calling Syntax(node)
3429+
visitPre(node._syntaxNode)
3430+
defer { visitPost(node._syntaxNode) }
3431+
if let newNode = visitAny(node._syntaxNode) { return newNode }
3432+
return Syntax(visit(node))
3433+
}
3434+
34183435
/// Implementation detail of visit(_:). Do not call directly.
34193436
private func visitImplTokenListSyntax(_ data: SyntaxData) -> Syntax {
34203437
let node = TokenListSyntax(data)
@@ -4919,6 +4936,8 @@ open class SyntaxRewriter {
49194936
return visitImplMacroDeclSyntax
49204937
case .macroExpansionDecl:
49214938
return visitImplMacroExpansionDeclSyntax
4939+
case .editorPlaceholderDecl:
4940+
return visitImplEditorPlaceholderDeclSyntax
49224941
case .tokenList:
49234942
return visitImplTokenListSyntax
49244943
case .nonEmptyTokenList:
@@ -5460,6 +5479,8 @@ open class SyntaxRewriter {
54605479
return visitImplMacroDeclSyntax(data)
54615480
case .macroExpansionDecl:
54625481
return visitImplMacroExpansionDeclSyntax(data)
5482+
case .editorPlaceholderDecl:
5483+
return visitImplEditorPlaceholderDeclSyntax(data)
54635484
case .tokenList:
54645485
return visitImplTokenListSyntax(data)
54655486
case .nonEmptyTokenList:

Sources/SwiftSyntax/gyb_generated/SyntaxTransform.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,10 @@ public protocol SyntaxTransformVisitor {
607607
/// - Parameter node: the node we are visiting.
608608
/// - Returns: the sum of whatever the child visitors return.
609609
func visit(_ node: MacroExpansionDeclSyntax) -> ResultType
610+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
611+
/// - Parameter node: the node we are visiting.
612+
/// - Returns: the sum of whatever the child visitors return.
613+
func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType
610614
/// Visiting `TokenListSyntax` specifically.
611615
/// - Parameter node: the node we are visiting.
612616
/// - Returns: the sum of whatever the child visitors return.
@@ -1964,6 +1968,12 @@ extension SyntaxTransformVisitor {
19641968
public func visit(_ node: MacroExpansionDeclSyntax) -> ResultType {
19651969
visitAny(Syntax(node))
19661970
}
1971+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
1972+
/// - Parameter node: the node we are visiting.
1973+
/// - Returns: nil by default.
1974+
public func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType {
1975+
visitAny(Syntax(node))
1976+
}
19671977
/// Visiting `TokenListSyntax` specifically.
19681978
/// - Parameter node: the node we are visiting.
19691979
/// - Returns: nil by default.
@@ -2965,6 +2975,8 @@ extension SyntaxTransformVisitor {
29652975
return visit(derived)
29662976
case .macroExpansionDecl(let derived):
29672977
return visit(derived)
2978+
case .editorPlaceholderDecl(let derived):
2979+
return visit(derived)
29682980
case .tokenList(let derived):
29692981
return visit(derived)
29702982
case .nonEmptyTokenList(let derived):

Sources/SwiftSyntax/gyb_generated/SyntaxVisitor.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,16 @@ open class SyntaxVisitor {
15111511
/// The function called after visiting `MacroExpansionDeclSyntax` and its descendents.
15121512
/// - node: the node we just finished visiting.
15131513
open func visitPost(_ node: MacroExpansionDeclSyntax) {}
1514+
/// Visiting `EditorPlaceholderDeclSyntax` specifically.
1515+
/// - Parameter node: the node we are visiting.
1516+
/// - Returns: how should we continue visiting.
1517+
open func visit(_ node: EditorPlaceholderDeclSyntax) -> SyntaxVisitorContinueKind {
1518+
return .visitChildren
1519+
}
1520+
1521+
/// The function called after visiting `EditorPlaceholderDeclSyntax` and its descendents.
1522+
/// - node: the node we just finished visiting.
1523+
open func visitPost(_ node: EditorPlaceholderDeclSyntax) {}
15141524
/// Visiting `TokenListSyntax` specifically.
15151525
/// - Parameter node: the node we are visiting.
15161526
/// - Returns: how should we continue visiting.
@@ -4310,6 +4320,17 @@ open class SyntaxVisitor {
43104320
visitPost(node)
43114321
}
43124322

4323+
/// Implementation detail of doVisit(_:_:). Do not call directly.
4324+
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) {
4325+
let node = EditorPlaceholderDeclSyntax(data)
4326+
let needsChildren = (visit(node) == .visitChildren)
4327+
// Avoid calling into visitChildren if possible.
4328+
if needsChildren && !node.raw.layoutView!.children.isEmpty {
4329+
visitChildren(node)
4330+
}
4331+
visitPost(node)
4332+
}
4333+
43134334
/// Implementation detail of doVisit(_:_:). Do not call directly.
43144335
private func visitImplTokenListSyntax(_ data: SyntaxData) {
43154336
let node = TokenListSyntax(data)
@@ -5903,6 +5924,8 @@ open class SyntaxVisitor {
59035924
visitImplMacroDeclSyntax(data)
59045925
case .macroExpansionDecl:
59055926
visitImplMacroExpansionDeclSyntax(data)
5927+
case .editorPlaceholderDecl:
5928+
visitImplEditorPlaceholderDeclSyntax(data)
59065929
case .tokenList:
59075930
visitImplTokenListSyntax(data)
59085931
case .nonEmptyTokenList:

0 commit comments

Comments
 (0)