Skip to content

SwiftSyntax: allow any Syntax nodes to access their leading/trailing trivia. #15278

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 3 commits into from
Mar 15, 2018
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
31 changes: 31 additions & 0 deletions test/SwiftSyntax/AbsolutePosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,35 @@ PositionTests.test("Recursion") {
})
}

PositionTests.test("Trivias") {
expectDoesNotThrow({
let leading = Trivia(pieces: [
.newlines(1),
.backticks(1),
.docLineComment("/// some comment")
])
let trailing = Trivia.docLineComment("/// This is comment\n")
let idx = 5
let items : [CodeBlockItemSyntax] =
[CodeBlockItemSyntax](repeating: CodeBlockItemSyntax {
$0.useItem(ReturnStmtSyntax {
$0.useReturnKeyword(
SyntaxFactory.makeReturnKeyword(
leadingTrivia: leading,
trailingTrivia: trailing))
})}, count: idx + 1)
let root = SyntaxFactory.makeSourceFile(
statements: SyntaxFactory.makeCodeBlockItemList(items),
eofToken: SyntaxFactory.makeToken(.eof, presence: .present))

expectEqual(root.leadingTrivia!.count, 3)
expectEqual(root.trailingTrivia!.count, 0)
let state = root.statements[idx]
expectEqual(state.leadingTrivia!.count, 3)
expectEqual(state.trailingTrivia!.count, 1)
expectEqual(state.leadingTrivia!.byteSize + state.trailingTrivia!.byteSize
+ state.byteSizeAfterTrimmingTrivia, state.byteSize)
})
}

runAllTests()
36 changes: 27 additions & 9 deletions tools/SwiftSyntax/RawSyntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,40 @@ extension RawSyntax {
}
}

func accumulateLeadingTrivia(_ pos: AbsolutePosition) -> Bool {
var leadingTrivia: Trivia? {
switch self {
case .node(_, let layout, _):
for child in layout {
guard let child = child else { continue }
if child.accumulateLeadingTrivia(pos) {
return true
}
guard let result = child.leadingTrivia else { continue }
return result
}
return false
return nil
case let .token(_, leadingTrivia, _, presence):
guard case .present = presence else { return false }
for piece in leadingTrivia {
piece.accumulateAbsolutePosition(pos)
guard case .present = presence else { return nil }
return leadingTrivia
}
}

var trailingTrivia: Trivia? {
switch self {
case .node(_, let layout, _):
for child in layout.reversed() {
guard let child = child else { continue }
guard let result = child.trailingTrivia else { continue }
return result
}
return true
return nil
case let .token(_, _, trailingTrivia, presence):
guard case .present = presence else { return nil }
return trailingTrivia
}
}

func accumulateLeadingTrivia(_ pos: AbsolutePosition) {
guard let trivia = leadingTrivia else { return }
for piece in trivia {
piece.accumulateAbsolutePosition(pos)
}
}

Expand Down
20 changes: 20 additions & 0 deletions tools/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ extension Syntax {
return data.byteSize
}

/// The leading trivia of this syntax node. Leading trivia is attached to
/// the first token syntax contained by this node. Without such token, this
/// property will return nil.
public var leadingTrivia: Trivia? {
return raw.leadingTrivia
}

/// The trailing trivia of this syntax node. Trailing trivia is attached to
/// the last token syntax contained by this node. Without such token, this
/// property will return nil.
public var trailingTrivia: Trivia? {
return raw.trailingTrivia
}

/// The textual byte length of this node exluding leading and trailing trivia.
public var byteSizeAfterTrimmingTrivia: Int {
return data.byteSize - (leadingTrivia?.byteSize ?? 0) -
(trailingTrivia?.byteSize ?? 0)
}

/// The root of the tree in which this node resides.
public var root: Syntax {
return makeSyntax(root: _root, data: _root)
Expand Down