Skip to content

Give SyntaxToken setters similar to Syntax nodes has for children #88

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
Feb 18, 2019
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
29 changes: 22 additions & 7 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/// A Syntax node represents a tree of nodes with tokens at the leaves.
/// Each node has accessors for its known children, and allows efficient
/// iteration over the children through its `children` property.
public protocol Syntax:
public protocol Syntax:
CustomStringConvertible, TextOutputStreamable {}

internal protocol _SyntaxBase: Syntax {
Expand Down Expand Up @@ -261,7 +261,7 @@ extension _SyntaxBase {
self.write(to: &s)
return s
}

/// Prints the raw value of this node to the provided stream.
/// - Parameter stream: The stream to which to print the raw tree.
public func write<Target>(to target: inout Target)
Expand Down Expand Up @@ -507,7 +507,7 @@ public struct TokenSyntax: _SyntaxBase, Hashable {
public var text: String {
return tokenKind.text
}

/// Returns a new TokenSyntax with its kind replaced
/// by the provided token kind.
public func withKind(_ tokenKind: TokenKind) -> TokenSyntax {
Expand Down Expand Up @@ -565,25 +565,40 @@ public struct TokenSyntax: _SyntaxBase, Hashable {

/// The leading trivia (spaces, newlines, etc.) associated with this token.
public var leadingTrivia: Trivia {
return raw.formTokenLeadingTrivia()!
get {
return raw.formTokenLeadingTrivia()!
}
set {
self = withLeadingTrivia(newValue)
}
}

/// The trailing trivia (spaces, newlines, etc.) associated with this token.
public var trailingTrivia: Trivia {
return raw.formTokenTrailingTrivia()!
get {
return raw.formTokenTrailingTrivia()!
}
set {
self = withTrailingTrivia(newValue)
}
}

/// The kind of token this node represents.
public var tokenKind: TokenKind {
return raw.formTokenKind()!
get {
return raw.formTokenKind()!
}
set {
self = withKind(newValue)
}
}

/// The length this node takes up spelled out in the source, excluding its
/// leading or trailing trivia.
public var contentLength: SourceLength {
return raw.tokenContentLength
}

/// The length this node's leading trivia takes up spelled out in source.
public var leadingTriviaLength: SourceLength {
return raw.tokenLeadingTriviaLength
Expand Down
11 changes: 11 additions & 0 deletions Tests/SwiftSyntaxTest/SyntaxFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,22 @@ public class SyntaxFactoryAPITestCase: XCTestCase {
let preSpacedTok = tok.withLeadingTrivia(.spaces(3))
XCTAssertEqual("\(preSpacedTok)", " struct")

var mutablePreSpacedTok = tok
mutablePreSpacedTok.leadingTrivia = .spaces(4)
XCTAssertEqual("\(mutablePreSpacedTok)", " struct")

let postSpacedTok = tok.withTrailingTrivia(.spaces(6))
XCTAssertEqual("\(postSpacedTok)", "struct ")

var mutablePostSpacedTok = tok
mutablePostSpacedTok.trailingTrivia = .spaces(3)
XCTAssertEqual("\(mutablePostSpacedTok)", "struct ")

let prePostSpacedTok = preSpacedTok.withTrailingTrivia(.spaces(4))
XCTAssertEqual("\(prePostSpacedTok)", " struct ")

mutablePreSpacedTok.trailingTrivia = .spaces(2)
XCTAssertEqual("\(mutablePreSpacedTok)", " struct ")
}

public func testFunctionCallSyntaxBuilder() {
Expand Down