Skip to content

Introduce a placeholder declaration to represent editor placeholders in member decl lists #1188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1677,4 +1677,15 @@ public let DECL_NODES: [Node] = [
collectionElementName: "AdditionalTrailingClosure")
]),

Node(name: "EditorPlaceholderDecl",
nameForDiagnostics: "editor placeholder",
kind: "Decl",
children: [
Child(name: "Identifier",
kind: "IdentifierToken",
tokenChoices: [
"Identifier"
])
]),

]
10 changes: 10 additions & 0 deletions Sources/SwiftParser/Declarations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ extension Parser {
return RawDeclSyntax(self.parseLetOrVarDeclaration(attrs, .missing(.keyword(.var))))
}

if self.currentToken.isEditorPlaceholder {
let placeholder = self.consumeAnyToken()
return RawDeclSyntax(
RawEditorPlaceholderDeclSyntax(
identifier: placeholder,
arena: self.arena
)
)
}

let isProbablyFuncDecl = self.at(any: [.identifier, .wildcard]) || self.at(anyIn: Operator.self) != nil

if isProbablyFuncDecl {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ allows Swift tools to parse, inspect, generate, and transform Swift source code.
- <doc:SwiftSyntax/PrecedenceGroupDeclSyntax>
- <doc:SwiftSyntax/MacroDeclSyntax>
- <doc:SwiftSyntax/MacroExpansionDeclSyntax>
- <doc:SwiftSyntax/EditorPlaceholderDeclSyntax>

### Statements

Expand Down
52 changes: 51 additions & 1 deletion Sources/SwiftSyntax/Raw/gyb_generated/RawSyntaxNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct RawDeclSyntax: RawDeclSyntaxNodeProtocol {

public static func isKindOf(_ raw: RawSyntax) -> Bool {
switch raw.kind {
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
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
default: return false
}
}
Expand Down Expand Up @@ -10301,6 +10301,56 @@ public struct RawMacroExpansionDeclSyntax: RawDeclSyntaxNodeProtocol {
}
}

@_spi(RawSyntax)
public struct RawEditorPlaceholderDeclSyntax: RawDeclSyntaxNodeProtocol {

@_spi(RawSyntax)
public var layoutView: RawSyntaxLayoutView {
return raw.layoutView!
}

public static func isKindOf(_ raw: RawSyntax) -> Bool {
return raw.kind == .editorPlaceholderDecl
}

public var raw: RawSyntax
init(raw: RawSyntax) {
assert(Self.isKindOf(raw))
self.raw = raw
}

public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
guard Self.isKindOf(other.raw) else { return nil }
self.init(raw: other.raw)
}

public init(
_ unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? = nil,
identifier: RawTokenSyntax,
_ unexpectedAfterIdentifier: RawUnexpectedNodesSyntax? = nil,
arena: __shared SyntaxArena
) {
let raw = RawSyntax.makeLayout(
kind: .editorPlaceholderDecl, uninitializedCount: 3, arena: arena) { layout in
layout.initialize(repeating: nil)
layout[0] = unexpectedBeforeIdentifier?.raw
layout[1] = identifier.raw
layout[2] = unexpectedAfterIdentifier?.raw
}
self.init(raw: raw)
}

public var unexpectedBeforeIdentifier: RawUnexpectedNodesSyntax? {
layoutView.children[0].map(RawUnexpectedNodesSyntax.init(raw:))
}
public var identifier: RawTokenSyntax {
layoutView.children[1].map(RawTokenSyntax.init(raw:))!
}
public var unexpectedAfterIdentifier: RawUnexpectedNodesSyntax? {
layoutView.children[2].map(RawUnexpectedNodesSyntax.init(raw:))
}
}

@_spi(RawSyntax)
public struct RawTokenListSyntax: RawSyntaxNodeProtocol {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1530,6 +1530,12 @@ func validateLayout(layout: RawSyntaxBuffer, as kind: SyntaxKind) {
assertNoError(kind, 15, verify(layout[15], as: RawMultipleTrailingClosureElementListSyntax?.self))
assertNoError(kind, 16, verify(layout[16], as: RawUnexpectedNodesSyntax?.self))
break
case .editorPlaceholderDecl:
assert(layout.count == 3)
assertNoError(kind, 0, verify(layout[0], as: RawUnexpectedNodesSyntax?.self))
assertNoError(kind, 1, verify(layout[1], as: RawTokenSyntax.self))
assertNoError(kind, 2, verify(layout[2], as: RawUnexpectedNodesSyntax?.self))
break
case .tokenList:
for (index, element) in layout.enumerated() {
assertNoError(kind, index, verify(element, as: RawTokenSyntax.self))
Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftSyntax/generated/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ extension Syntax {
.node(DocumentationAttributeArgumentSyntax.self),
.node(DocumentationAttributeArgumentsSyntax.self),
.node(DynamicReplacementArgumentsSyntax.self),
.node(EditorPlaceholderDeclSyntax.self),
.node(EditorPlaceholderExprSyntax.self),
.node(EffectsArgumentsSyntax.self),
.node(EnumCaseDeclSyntax.self),
Expand Down Expand Up @@ -457,6 +458,8 @@ extension SyntaxKind {
return DocumentationAttributeArgumentsSyntax.self
case .dynamicReplacementArguments:
return DynamicReplacementArgumentsSyntax.self
case .editorPlaceholderDecl:
return EditorPlaceholderDeclSyntax.self
case .editorPlaceholderExpr:
return EditorPlaceholderExprSyntax.self
case .effectsArguments:
Expand Down Expand Up @@ -984,6 +987,8 @@ extension SyntaxKind {
return "@_documentation arguments"
case .dynamicReplacementArguments:
return "@_dynamicReplacement argument"
case .editorPlaceholderDecl:
return "editor placeholder"
case .editorPlaceholderExpr:
return "editor placeholder"
case .effectsArguments:
Expand Down
8 changes: 8 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxAnyVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,14 @@ open class SyntaxAnyVisitor: SyntaxVisitor {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: EditorPlaceholderDeclSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}

override open func visitPost(_ node: EditorPlaceholderDeclSyntax) {
visitAnyPost(node._syntaxNode)
}

override open func visit(_ node: EditorPlaceholderExprSyntax) -> SyntaxVisitorContinueKind {
return visitAny(node._syntaxNode)
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftSyntax/generated/SyntaxBaseNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {

public init?<S: SyntaxProtocol>(_ node: S) {
switch node.raw.kind {
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:
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:
self._syntaxNode = node._syntaxNode
default:
return nil
Expand All @@ -82,7 +82,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
internal init(_ data: SyntaxData) {
#if DEBUG
switch data.raw.kind {
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:
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:
break
default:
fatalError("Unable to create DeclSyntax from \(data.raw.kind)")
Expand Down Expand Up @@ -123,6 +123,7 @@ public struct DeclSyntax: DeclSyntaxProtocol, SyntaxHashable {
.node(AssociatedtypeDeclSyntax.self),
.node(ClassDeclSyntax.self),
.node(DeinitializerDeclSyntax.self),
.node(EditorPlaceholderDeclSyntax.self),
.node(EnumCaseDeclSyntax.self),
.node(EnumDeclSyntax.self),
.node(ExtensionDeclSyntax.self),
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public enum SyntaxEnum {

case dynamicReplacementArguments(DynamicReplacementArgumentsSyntax)

case editorPlaceholderDecl(EditorPlaceholderDeclSyntax)

case editorPlaceholderExpr(EditorPlaceholderExprSyntax)

case effectsArguments(EffectsArgumentsSyntax)
Expand Down Expand Up @@ -715,6 +717,8 @@ public extension Syntax {
return .documentationAttributeArguments(DocumentationAttributeArgumentsSyntax(self)!)
case .dynamicReplacementArguments:
return .dynamicReplacementArguments(DynamicReplacementArgumentsSyntax(self)!)
case .editorPlaceholderDecl:
return .editorPlaceholderDecl(EditorPlaceholderDeclSyntax(self)!)
case .editorPlaceholderExpr:
return .editorPlaceholderExpr(EditorPlaceholderExprSyntax(self)!)
case .effectsArguments:
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ public enum SyntaxKind {

case dynamicReplacementArguments

case editorPlaceholderDecl

case editorPlaceholderExpr

case effectsArguments
Expand Down
14 changes: 14 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxTransform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,11 @@ public protocol SyntaxTransformVisitor {
/// - Returns: the sum of whatever the child visitors return.
func visit(_ node: DynamicReplacementArgumentsSyntax) -> ResultType

/// Visiting `EditorPlaceholderDeclSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: the sum of whatever the child visitors return.
func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType

/// Visiting `EditorPlaceholderExprSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: the sum of whatever the child visitors return.
Expand Down Expand Up @@ -1922,6 +1927,13 @@ extension SyntaxTransformVisitor {
visitAny(Syntax(node))
}

/// Visiting `EditorPlaceholderDeclSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: nil by default.
public func visit(_ node: EditorPlaceholderDeclSyntax) -> ResultType {
visitAny(Syntax(node))
}

/// Visiting `EditorPlaceholderExprSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: nil by default.
Expand Down Expand Up @@ -3321,6 +3333,8 @@ extension SyntaxTransformVisitor {
return visit(derived)
case .dynamicReplacementArguments(let derived):
return visit(derived)
case .editorPlaceholderDecl(let derived):
return visit(derived)
case .editorPlaceholderExpr(let derived):
return visit(derived)
case .effectsArguments(let derived):
Expand Down
25 changes: 25 additions & 0 deletions Sources/SwiftSyntax/generated/SyntaxVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,18 @@ open class SyntaxVisitor {
open func visitPost(_ node: DynamicReplacementArgumentsSyntax) {
}

/// Visiting `EditorPlaceholderDeclSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
open func visit(_ node: EditorPlaceholderDeclSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}

/// The function called after visiting `EditorPlaceholderDeclSyntax` and its descendents.
/// - node: the node we just finished visiting.
open func visitPost(_ node: EditorPlaceholderDeclSyntax) {
}

/// Visiting `EditorPlaceholderExprSyntax` specifically.
/// - Parameter node: the node we are visiting.
/// - Returns: how should we continue visiting.
Expand Down Expand Up @@ -4095,6 +4107,17 @@ open class SyntaxVisitor {
visitPost(node)
}

/// Implementation detail of doVisit(_:_:). Do not call directly.
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) {
let node = EditorPlaceholderDeclSyntax(data)
let needsChildren = (visit(node) == .visitChildren)
// Avoid calling into visitChildren if possible.
if needsChildren && !node.raw.layoutView!.children.isEmpty {
visitChildren(node)
}
visitPost(node)
}

/// Implementation detail of doVisit(_:_:). Do not call directly.
private func visitImplEditorPlaceholderExprSyntax(_ data: SyntaxData) {
let node = EditorPlaceholderExprSyntax(data)
Expand Down Expand Up @@ -6197,6 +6220,8 @@ open class SyntaxVisitor {
visitImplDocumentationAttributeArgumentsSyntax(data)
case .dynamicReplacementArguments:
visitImplDynamicReplacementArgumentsSyntax(data)
case .editorPlaceholderDecl:
visitImplEditorPlaceholderDeclSyntax(data)
case .editorPlaceholderExpr:
visitImplEditorPlaceholderExprSyntax(data)
case .effectsArguments:
Expand Down
27 changes: 27 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4848,6 +4848,33 @@ public enum SyntaxFactory {
return MacroExpansionDeclSyntax(data)
}
}
@available(*, deprecated, message: "Use initializer on EditorPlaceholderDeclSyntax")
public static func makeEditorPlaceholderDecl(_ unexpectedBeforeIdentifier: UnexpectedNodesSyntax? = nil, identifier: TokenSyntax, _ unexpectedAfterIdentifier: UnexpectedNodesSyntax? = nil) -> EditorPlaceholderDeclSyntax {
let layout: [RawSyntax?] = [
unexpectedBeforeIdentifier?.raw,
identifier.raw,
unexpectedAfterIdentifier?.raw,
]
return withExtendedLifetime(SyntaxArena()) { arena in
let raw = RawSyntax.makeLayout(kind: SyntaxKind.editorPlaceholderDecl,
from: layout, arena: arena)
let data = SyntaxData.forRoot(raw)
return EditorPlaceholderDeclSyntax(data)
}
}

@available(*, deprecated, message: "Use initializer on EditorPlaceholderDeclSyntax")
public static func makeBlankEditorPlaceholderDecl(presence: SourcePresence = .present) -> EditorPlaceholderDeclSyntax {
return withExtendedLifetime(SyntaxArena()) { arena in
let data = SyntaxData.forRoot(RawSyntax.makeLayout(kind: .editorPlaceholderDecl,
from: [
nil,
RawSyntax.makeMissingToken(kind: TokenKind.identifier(""), arena: arena),
nil,
], arena: arena))
return EditorPlaceholderDeclSyntax(data)
}
}
@available(*, deprecated, message: "Use initializer on TokenListSyntax")
public static func makeTokenList(
_ elements: [TokenSyntax]) -> TokenListSyntax {
Expand Down
21 changes: 21 additions & 0 deletions Sources/SwiftSyntax/gyb_generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,13 @@ open class SyntaxRewriter {
return DeclSyntax(visitChildren(node))
}

/// Visit a `EditorPlaceholderDeclSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
open func visit(_ node: EditorPlaceholderDeclSyntax) -> DeclSyntax {
return DeclSyntax(visitChildren(node))
}

/// Visit a `TokenListSyntax`.
/// - Parameter node: the node that is being visited
/// - Returns: the rewritten node
Expand Down Expand Up @@ -3367,6 +3374,16 @@ open class SyntaxRewriter {
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
private func visitImplEditorPlaceholderDeclSyntax(_ data: SyntaxData) -> Syntax {
let node = EditorPlaceholderDeclSyntax(data)
// Accessing _syntaxNode directly is faster than calling Syntax(node)
visitPre(node._syntaxNode)
defer { visitPost(node._syntaxNode) }
if let newNode = visitAny(node._syntaxNode) { return newNode }
return Syntax(visit(node))
}

/// Implementation detail of visit(_:). Do not call directly.
private func visitImplTokenListSyntax(_ data: SyntaxData) -> Syntax {
let node = TokenListSyntax(data)
Expand Down Expand Up @@ -4847,6 +4864,8 @@ open class SyntaxRewriter {
return visitImplMacroDeclSyntax
case .macroExpansionDecl:
return visitImplMacroExpansionDeclSyntax
case .editorPlaceholderDecl:
return visitImplEditorPlaceholderDeclSyntax
case .tokenList:
return visitImplTokenListSyntax
case .attribute:
Expand Down Expand Up @@ -5380,6 +5399,8 @@ open class SyntaxRewriter {
return visitImplMacroDeclSyntax(data)
case .macroExpansionDecl:
return visitImplMacroExpansionDeclSyntax(data)
case .editorPlaceholderDecl:
return visitImplEditorPlaceholderDeclSyntax(data)
case .tokenList:
return visitImplTokenListSyntax(data)
case .attribute:
Expand Down
Loading