Skip to content

Commit 9722a71

Browse files
committed
Add an introducer property to DeclGroupSyntax
It can be useful to grab the introducer of a particular declaration, add a property for this purpose to `DeclGroupSyntax`. Fixes #2535. Resolves rdar://124247480.
1 parent fc656d5 commit 9722a71

File tree

5 files changed

+119
-10
lines changed

5 files changed

+119
-10
lines changed

CodeGeneration/Sources/SyntaxSupport/Traits.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public let TRAITS: [Trait] = [
4747
children: [
4848
Child(name: "attributes", kind: .node(kind: .attributeList)),
4949
Child(name: "modifiers", kind: .node(kind: .declModifierList)),
50+
Child(
51+
name: "introducer",
52+
kind: .token(choices: [.keyword(.actor), .keyword(.class), .keyword(.enum), .keyword(.extension), .keyword(.protocol), .keyword(.struct)]),
53+
documentation: "The token that introduces this declaration, eg. `class` for a class declaration."
54+
),
5055
Child(name: "inheritanceClause", kind: .node(kind: .inheritanceClause), isOptional: true),
5156
Child(
5257
name: "genericWhereClause",

Release Notes/600.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- `String.isValidIdentifier(for:)`
3636
- Description: `SwiftParser` adds an extension on `String` to check if it can be used as an identifier in a given context.
3737
- Pull Request: https://github.com/apple/swift-syntax/pull/2434
38-
38+
3939
- `MacroDeclSyntax.expand`
4040
- the `expand(argumentList:definition:replacements:)` method gains a new parameter 'genericReplacements:' that is defaulted to an empty array.
4141
- The method's signature is now `expand(argumentList:definition:replacements:genericReplacements:)`
@@ -62,10 +62,15 @@
6262
- Description: `IncrementalEdit` used to store the range that was replaced and the length of the replacement but not the replacement bytes by itself. `IncrementalEdit` now has a `replacement` property that contains the replacement bytes.
6363
- Pull Request: https://github.com/apple/swift-syntax/pull/2527
6464

65+
- `DeclGroupSyntax.introducer`
66+
- Description: The `DeclGroupSyntax` trait has an extra `introducer` property, ie. the keyword that introduces the declaration.
67+
- Issue: https://github.com/apple/sourcekit-lsp/issues/2535
68+
- Pull Request: https://github.com/apple/swift-syntax/pull/2539
69+
6570
## API Behavior Changes
6671

6772
## Deprecations
68-
73+
6974
- Child Choice Node Casts
7075
- Description: `is`, `as`, and `cast` methods for types not contained in the choice node are marked as deprecated. The deprecated methods will emit a warning, indicating that the cast will always fail.
7176
- Issue: https://github.com/apple/swift-syntax/issues/2092
@@ -81,7 +86,7 @@
8186
- `MacroExpansion{Error|Warning|FixIt}Message` moved to the `SwiftSyntaxMacros` module
8287
- Description: Move the `MacroExpansion{Error|Warning|FixIt}Message` types from the `SwiftSyntaxMacroExpansion` module to `SwiftSyntaxMacros`. Deprecated typealiases in `SwiftSyntaxMacroExpansion` forward to `SwiftSyntaxMacros`.
8388
- Pull request: https://github.com/apple/swift-syntax/pull/2338
84-
- Notes: The expansion diagnostic messages were defined in `SwiftSyntaxMacroExpansion`, which is intended as an implementation detail of the plugin server and should not need to be imported by macros.
89+
- Notes: The expansion diagnostic messages were defined in `SwiftSyntaxMacroExpansion`, which is intended as an implementation detail of the plugin server and should not need to be imported by macros.
8590

8691
- `EditorPlaceholderDeclSyntax` and `EditorPlaceholderExprSyntax`:
8792
- Description: `EditorPlaceholderDeclSyntax` and `EditorPlaceholderExprSyntax` are now deprecated and placeholders are instead parsed as identifiers within a `MissingDeclSyntax` or `DeclReferenceExprSyntax`.
@@ -97,15 +102,15 @@
97102
- Effect specifiers:
98103
- Description: The `unexpectedAfterThrowsSpecifier` node of the various effect specifiers has been removed.
99104
- Pull request: https://github.com/apple/swift-syntax/pull/2219
100-
- Migration steps: Check `unexpectedBetweenThrowsSpecifierAndThrownError` and `unexpectedAfterThrownError` instead.
105+
- Migration steps: Check `unexpectedBetweenThrowsSpecifierAndThrownError` and `unexpectedAfterThrownError` instead.
101106

102107
- `SyntaxKind` removed conformance to `CaseIterable`
103-
- Description: `SyntaxKind` no longer conforms to `CaseIterable` since there is no good use case to iterate over all syntax kinds.
104-
- Pull request: https://github.com/apple/swift-syntax/pull/2292
108+
- Description: `SyntaxKind` no longer conforms to `CaseIterable` since there is no good use case to iterate over all syntax kinds.
109+
- Pull request: https://github.com/apple/swift-syntax/pull/2292
105110
- Migration steps: Exhaustively check all the syntax nodes that your program supports. There should be no use case to iterate over all cases in `SyntaxKind`.
106111

107112
- `IntegerLiteralExprSyntax.Radix` removed conformance to `CaseIterable`
108-
- Description: `IntegerLiteralExprSyntax.Radix` no longer conforms to `CaseIterable` since there is no good use case to iterate over all radix kinds.
113+
- Description: `IntegerLiteralExprSyntax.Radix` no longer conforms to `CaseIterable` since there is no good use case to iterate over all radix kinds.
109114
- Pull request: https://github.com/apple/swift-syntax/pull/2292
110115
- Migration steps: There should be no use case to iterate over all cases in `IntegerLiteralExprSyntax.Radix`.
111116

@@ -119,7 +124,7 @@
119124
- The new classification case covers the first names of parameters in function-like declarations and the label of arguments in function-like calls.
120125
- Pull request: https://github.com/apple/swift-syntax/pull/2375
121126
- Migration steps: In exhaustive switches over `SyntaxClassification`, cover the new case.
122-
127+
123128
- `SyntaxEnum` and `SyntaxKind` gained new cases: `throwsClause`
124129
- The new cases cover the newly introduced `ThrowsClauseSyntax`
125130
- Pull request: https://github.com/apple/swift-syntax/pull/2379
@@ -129,10 +134,10 @@
129134
- Description: The new property provides the lexical context in which the macro is expanded, and has several paired API changes. Types that conform to `MacroExpansionContext` will need to implement this property. Additionally, the `HostToPluginMessage` cases `expandFreestandingMacro` and `expandAttachedMacro` now include an optional `lexicalContext`. Finally, the `SyntaxProtocol.expand(macros:in:indentationWidth:)` syntactic expansion operation has been deprecated in favor of a new version `expand(macros:contextGenerator:indentationWidth:)` that takes a function produces a new macro expansion context for each expansion.
130135
- Pull request: https://github.com/apple/swift-syntax/pull/1554
131136
- Migration steps: Add the new property `lexicalContext` to any `MacroExpansionContext`-conforming types. If implementing the host-to-plugin message protocol, add support for `lexicalContext`. For macro expansion operations going through `SyntaxProtocol.expand`, provide a context generator that creates a fresh context including the lexical context.
132-
137+
133138
- `TriviaPiece.isBackslash` in `SwiftParserDiagnostics` removed
134139
- Description: `TriviaPiece.isBackslash` was not intended to be public API.
135-
- Pull request: xxx
140+
- Pull request: https://github.com/apple/swift-syntax/pull/2531
136141
- Migration steps: Use `if case .backslash = triviaPiece` instead
137142

138143
## Template

Sources/SwiftSyntax/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_swift_syntax_library(SwiftSyntax
1414
BumpPtrAllocator.swift
1515
CommonAncestor.swift
1616
Convenience.swift
17+
CustomTraits.swift
1718
MemoryLayout.swift
1819
MissingNodeInitializers.swift
1920
SourceEdit.swift
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
extension ActorDeclSyntax {
14+
var introducer: TokenSyntax {
15+
get {
16+
return actorKeyword
17+
}
18+
set {
19+
actorKeyword = newValue
20+
}
21+
}
22+
}
23+
24+
extension ClassDeclSyntax {
25+
var introducer: TokenSyntax {
26+
get {
27+
return classKeyword
28+
}
29+
set {
30+
classKeyword = newValue
31+
}
32+
}
33+
}
34+
35+
extension EnumDeclSyntax {
36+
var introducer: TokenSyntax {
37+
get {
38+
return enumKeyword
39+
}
40+
set {
41+
enumKeyword = newValue
42+
}
43+
}
44+
}
45+
46+
extension ExtensionDeclSyntax {
47+
var introducer: TokenSyntax {
48+
get {
49+
return extensionKeyword
50+
}
51+
set {
52+
extensionKeyword = newValue
53+
}
54+
}
55+
}
56+
57+
extension ProtocolDeclSyntax {
58+
var introducer: TokenSyntax {
59+
get {
60+
return protocolKeyword
61+
}
62+
set {
63+
protocolKeyword = newValue
64+
}
65+
}
66+
}
67+
68+
extension StructDeclSyntax {
69+
var introducer: TokenSyntax {
70+
get {
71+
return structKeyword
72+
}
73+
set {
74+
structKeyword = newValue
75+
}
76+
}
77+
}
78+
79+
//==========================================================================//
80+
// IMPORTANT: If you are tempted to add an extension here, please insert //
81+
// it in alphabetical order above //
82+
//==========================================================================//

Sources/SwiftSyntax/generated/SyntaxTraits.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public protocol DeclGroupSyntax: SyntaxProtocol, DeclSyntaxProtocol {
7272
set
7373
}
7474

75+
/// The token that introduces this declaration, eg. `class` for a class declaration.
76+
///
77+
/// ### Tokens
78+
///
79+
/// For syntax trees generated by the parser, this is guaranteed to be one of the following kinds:
80+
/// - `actor`
81+
/// - `class`
82+
/// - `enum`
83+
/// - `extension`
84+
/// - `protocol`
85+
/// - `struct`
86+
var introducer: TokenSyntax {
87+
get
88+
set
89+
}
90+
7591
var inheritanceClause: InheritanceClauseSyntax? {
7692
get
7793
set

0 commit comments

Comments
 (0)