-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Conversation
…trivia. Although libSyntax is designed in a way that trivia is attached to tokens, we shouldn't restrict clients to access trivia only from a token. An example can be doc-comment, which conceptually attaches to a declaration rather than the start token of a declaration, like at-attributes.
@swift-ci please smoke test |
@harlanhaskins @rintaro Any objection to this? |
PositionTests.test("Trivias") { | ||
expectDoesNotThrow({ | ||
var l = [CodeBlockItemSyntax]() | ||
let leading = Trivia.newlines(1).appending(TriviaPiece.backticks(1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be cleaner as:
let leading = Trivia(pieces: [
.newlines(1),
.backticks(1),
.docLineComment("/// some comment")
])
let leading = Trivia.newlines(1).appending(TriviaPiece.backticks(1)) | ||
.appending(TriviaPiece.docLineComment("/// some comment")) | ||
let trailing = Trivia.docLineComment("/// This is comment\n") | ||
let idx = 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably would be a good idea to Make the ReturnStmt
here then just say
let l = [CodeBlockItemSyntax](repeating: returnStmt, count: 5)
for _ in 0...idx { | ||
l.append(SyntaxFactory.makeCodeBlockItem( | ||
item: SyntaxFactory.makeReturnStmt( | ||
returnKeyword: SyntaxFactory.makeToken(.returnKeyword, presence: .present) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReturnStmtSyntax {
$0.useReturnKeyword(
SyntaxFactory.makeReturnKeyword(
leadingTrivia: leading,
trailingTrivia: trailing))
}
@swift-ci please smoke test |
tools/SwiftSyntax/Syntax.swift
Outdated
/// The textual byte length of this node exluding leading and trailing trivia. | ||
public var byteSizeAfterTrimmingTrivia: Int { | ||
return data.byteSize - | ||
(leadingTrivia == nil ? 0 : leadingTrivia!.byteSize) - |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should probably be leadingTrivia?.byteSize ?? 0
to avoid the force-unwrap.
@swift-ci please smoke test |
🎉 |
…trivia. (swiftlang#15278) Although libSyntax is designed in a way that trivia is attached to tokens, we shouldn't restrict clients to access trivia only from a token. An example can be doc-comment, which conceptually attaches to a declaration rather than the start token of a declaration, like at-attributes.
Although libSyntax is designed in a way that trivia is attached to
tokens, we shouldn't restrict clients to access trivia only from a token.
An example can be doc-comment, which conceptually attaches to a declaration rather
than the start token of a declaration, like at-attributes.