Skip to content

Commit dd44630

Browse files
authored
Merge pull request swiftlang#88 from hartbit/syntax-token-setters
Give SyntaxToken setters similar to Syntax nodes has for children
2 parents e835cb1 + 99afd1d commit dd44630

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

Sources/SwiftSyntax/Syntax.swift

+22-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/// A Syntax node represents a tree of nodes with tokens at the leaves.
1414
/// Each node has accessors for its known children, and allows efficient
1515
/// iteration over the children through its `children` property.
16-
public protocol Syntax:
16+
public protocol Syntax:
1717
CustomStringConvertible, TextOutputStreamable {}
1818

1919
internal protocol _SyntaxBase: Syntax {
@@ -261,7 +261,7 @@ extension _SyntaxBase {
261261
self.write(to: &s)
262262
return s
263263
}
264-
264+
265265
/// Prints the raw value of this node to the provided stream.
266266
/// - Parameter stream: The stream to which to print the raw tree.
267267
public func write<Target>(to target: inout Target)
@@ -507,7 +507,7 @@ public struct TokenSyntax: _SyntaxBase, Hashable {
507507
public var text: String {
508508
return tokenKind.text
509509
}
510-
510+
511511
/// Returns a new TokenSyntax with its kind replaced
512512
/// by the provided token kind.
513513
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
@@ -565,25 +565,40 @@ public struct TokenSyntax: _SyntaxBase, Hashable {
565565

566566
/// The leading trivia (spaces, newlines, etc.) associated with this token.
567567
public var leadingTrivia: Trivia {
568-
return raw.formTokenLeadingTrivia()!
568+
get {
569+
return raw.formTokenLeadingTrivia()!
570+
}
571+
set {
572+
self = withLeadingTrivia(newValue)
573+
}
569574
}
570575

571576
/// The trailing trivia (spaces, newlines, etc.) associated with this token.
572577
public var trailingTrivia: Trivia {
573-
return raw.formTokenTrailingTrivia()!
578+
get {
579+
return raw.formTokenTrailingTrivia()!
580+
}
581+
set {
582+
self = withTrailingTrivia(newValue)
583+
}
574584
}
575585

576586
/// The kind of token this node represents.
577587
public var tokenKind: TokenKind {
578-
return raw.formTokenKind()!
588+
get {
589+
return raw.formTokenKind()!
590+
}
591+
set {
592+
self = withKind(newValue)
593+
}
579594
}
580595

581596
/// The length this node takes up spelled out in the source, excluding its
582597
/// leading or trailing trivia.
583598
public var contentLength: SourceLength {
584599
return raw.tokenContentLength
585600
}
586-
601+
587602
/// The length this node's leading trivia takes up spelled out in source.
588603
public var leadingTriviaLength: SourceLength {
589604
return raw.tokenLeadingTriviaLength

Tests/SwiftSyntaxTest/SyntaxFactory.swift

+11
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,22 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
7575
let preSpacedTok = tok.withLeadingTrivia(.spaces(3))
7676
XCTAssertEqual("\(preSpacedTok)", " struct")
7777

78+
var mutablePreSpacedTok = tok
79+
mutablePreSpacedTok.leadingTrivia = .spaces(4)
80+
XCTAssertEqual("\(mutablePreSpacedTok)", " struct")
81+
7882
let postSpacedTok = tok.withTrailingTrivia(.spaces(6))
7983
XCTAssertEqual("\(postSpacedTok)", "struct ")
8084

85+
var mutablePostSpacedTok = tok
86+
mutablePostSpacedTok.trailingTrivia = .spaces(3)
87+
XCTAssertEqual("\(mutablePostSpacedTok)", "struct ")
88+
8189
let prePostSpacedTok = preSpacedTok.withTrailingTrivia(.spaces(4))
8290
XCTAssertEqual("\(prePostSpacedTok)", " struct ")
91+
92+
mutablePreSpacedTok.trailingTrivia = .spaces(2)
93+
XCTAssertEqual("\(mutablePreSpacedTok)", " struct ")
8394
}
8495

8596
public func testFunctionCallSyntaxBuilder() {

0 commit comments

Comments
 (0)