Skip to content

Commit 30a58f2

Browse files
committed
Improve documentation comments of StringLiteralSegmentListSyntax
1 parent 97cc869 commit 30a58f2

File tree

1 file changed

+103
-6
lines changed

1 file changed

+103
-6
lines changed

Sources/SwiftSyntax/generated/SyntaxCollections.swift

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,21 +1397,56 @@ public struct SpecializeAttributeArgumentListSyntax: SyntaxCollection, SyntaxHas
13971397
public static let syntaxKind = SyntaxKind.specializeAttributeArgumentList
13981398
}
13991399

1400+
/// A collection of string literal segments representing both plain text and interpolated expressions.
1401+
///
1402+
/// This collection is used to represent the contents of string literals, including both static text segments and interpolated expressions.
1403+
/// For example, in the string literal `"Hello \(name)"`, the collection would contain a string segment for `"Hello "` followed by an expression segment for `\(name)`.
1404+
///
1405+
/// ```swift
1406+
/// let segments = StringLiteralSegmentListSyntax([
1407+
/// .stringSegment(.init(content: .stringSegment("Hello "))),
1408+
/// .expressionSegment(/* interpolation expression */)
1409+
/// ])
1410+
/// ```
1411+
///
1412+
/// This type is commonly used in macros for:
1413+
/// - Building string literals with both static text and interpolations
1414+
/// - Validating string literal contents at compile time
1415+
/// - Analyzing or transforming string interpolations
1416+
///
14001417
/// ### Children
1401-
///
1418+
///
14021419
/// (``StringSegmentSyntax`` | ``ExpressionSegmentSyntax``) `*`
14031420
///
14041421
/// ### Contained in
1405-
///
1422+
///
14061423
/// - ``StringLiteralExprSyntax``.``StringLiteralExprSyntax/segments``
14071424
public struct StringLiteralSegmentListSyntax: SyntaxCollection, SyntaxHashable {
1425+
/// Represents either a literal string segment or an interpolated expression segment within a string literal.
1426+
///
1427+
/// This enum is used to handle the two possible types of segments that can appear in a string literal:
1428+
/// - Plain text segments (like `"Hello "` in `"Hello \(name)"`)
1429+
/// - Expression segments (like `\(name)` in `"Hello \(name)"`)
1430+
///
1431+
/// Example:
1432+
/// ```swift
1433+
/// let segments = [
1434+
/// StringLiteralSegmentListSyntax.Element.stringSegment(.init(content: .stringSegment("Hello "))),
1435+
/// StringLiteralSegmentListSyntax.Element.expressionSegment(/* expression */)
1436+
/// ]
1437+
/// ```
14081438
public enum Element: SyntaxChildChoices, SyntaxHashable {
1409-
/// A literal segment inside a string segment.
1410-
///
1439+
/// A literal text segment inside a string literal.
1440+
///
1441+
/// This case represents static text content like `"Hello "` in `"Hello \(name)"`.
1442+
///
14111443
/// - SeeAlso: ``ExpressionSegmentSyntax``
14121444
case stringSegment(StringSegmentSyntax)
1413-
/// An interpolated expression inside a string literal.
1414-
///
1445+
1446+
/// An interpolated expression segment inside a string literal.
1447+
///
1448+
/// This case represents interpolated expressions like `\(name)` in `"Hello \(name)"`.
1449+
///
14151450
/// - SeeAlso: ``StringSegmentSyntax``
14161451
case expressionSegment(ExpressionSegmentSyntax)
14171452

@@ -1424,14 +1459,59 @@ public struct StringLiteralSegmentListSyntax: SyntaxCollection, SyntaxHashable {
14241459
}
14251460
}
14261461

1462+
/// Creates a new Element wrapping a string segment.
1463+
///
1464+
/// This initializer is used when adding literal text content to a string literal. The string segment should be created using `.stringSegment()` to ensure proper formatting.
1465+
///
1466+
/// Example from warning macro:
1467+
/// ```swift
1468+
/// let element = StringLiteralSegmentListSyntax.Element(
1469+
/// StringSegmentSyntax(content: .stringSegment("warning: invalid configuration"))
1470+
/// )
1471+
/// ```
1472+
///
1473+
/// - Parameters:
1474+
/// - node: The string segment to wrap in this element.
14271475
public init(_ node: StringSegmentSyntax) {
14281476
self = .stringSegment(node)
14291477
}
14301478

1479+
/// Creates a new Element wrapping an expression segment.
1480+
///
1481+
/// This initializer is used when adding interpolated expressions to a string literal.
1482+
///
1483+
/// Example from diagnostic message:
1484+
/// ```swift
1485+
/// let element = StringLiteralSegmentListSyntax.Element(
1486+
/// ExpressionSegmentSyntax(
1487+
/// expressions: ExprListSyntax([
1488+
/// ExprSyntax("errorValue")
1489+
/// ])
1490+
/// )
1491+
/// )
1492+
/// ```
1493+
///
1494+
/// - Parameters:
1495+
/// - node: The expression segment to wrap in this element.
14311496
public init(_ node: ExpressionSegmentSyntax) {
14321497
self = .expressionSegment(node)
14331498
}
14341499

1500+
/// Creates a new Element from an existing syntax node by attempting to cast it to either a string or expression segment.
1501+
///
1502+
/// Example from string literal parsing:
1503+
/// ```swift
1504+
/// guard
1505+
/// let stringLiteral = node.as(StringLiteralExprSyntax.self),
1506+
/// let firstSegment = stringLiteral.segments.first,
1507+
/// case .stringSegment(let segment) = firstSegment
1508+
/// else {
1509+
/// throw CustomError.message("Expected string literal")
1510+
/// }
1511+
/// ```
1512+
///
1513+
/// - Parameters:
1514+
/// - node: The syntax node to attempt to convert into an element. Must be either a StringSegmentSyntax or ExpressionSegmentSyntax.
14351515
public init?(_ node: __shared some SyntaxProtocol) {
14361516
if let node = node.as(StringSegmentSyntax.self) {
14371517
self = .stringSegment(node)
@@ -1493,6 +1573,23 @@ public struct StringLiteralSegmentListSyntax: SyntaxCollection, SyntaxHashable {
14931573

14941574
public let _syntaxNode: Syntax
14951575

1576+
/// Creates a list of string literal segments from an array of elements.
1577+
///
1578+
/// Used to create lists that represent both plain text and interpolated expressions in a string literal. Common in macros where you need to create or modify string literals.
1579+
///
1580+
/// Example:
1581+
/// ```swift
1582+
/// let stringLiteral = StringLiteralExprSyntax(
1583+
/// openingQuote: .stringQuoteToken(),
1584+
/// segments: StringLiteralSegmentListSyntax([
1585+
/// .stringSegment(.init(content: .stringSegment("Hello")))
1586+
/// ]),
1587+
/// closingQuote: .stringQuoteToken()
1588+
/// )
1589+
/// ```
1590+
///
1591+
/// - Parameters:
1592+
/// - node: The syntax node to create the list from. Must be of kind `.stringLiteralSegmentList`.
14961593
public init?(_ node: some SyntaxProtocol) {
14971594
guard node.raw.kind == .stringLiteralSegmentList else {
14981595
return nil

0 commit comments

Comments
 (0)