Skip to content

Document that Trivia cannot represent invalid UTF-8 #1490

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
Apr 5, 2023
Merged
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
12 changes: 12 additions & 0 deletions Sources/SwiftSyntax/Syntax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,12 @@ public extension SyntaxProtocol {
/// 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.
///
/// Note: `Trivia` is not able to represent invalid UTF-8 sequences. To get
/// the leading trivia text including all invalid UTF-8 sequences, use
/// ```
/// node.syntaxTextBytes.prefix(self.leadingTriviaLength.utf8Length)
/// ```
var leadingTrivia: Trivia {
get {
return raw.formLeadingTrivia()
Expand All @@ -483,6 +489,12 @@ public extension SyntaxProtocol {
/// 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.
///
/// Note: `Trivia` is not able to represent invalid UTF-8 sequences. To get
/// the leading trivia text including all invalid UTF-8 sequences, use
/// ```
/// node.syntaxTextBytes[(node.byteSize - node.trailingTriviaLength.utf8Length)...]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

node.syntaxTextBytes.suffix(node.trailingTriviaLength.utf8Length)

looks easier. But it doesn't really matter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there is no suffix on Array. But I took the suggestion for prefix.

/// ```
var trailingTrivia: Trivia {
get {
return raw.formTrailingTrivia()
Expand Down