Trying to understand Trivia in SwiftSyntax

i want to detect line comments of the form // snippet., where the line comment is the only entity (besides whitespace) in its line.

i’ve currently got some code that traverses the Trivia like this:

func visit(token:TokenSyntax)
    self.visit(trivia: token.leadingTrivia, at: token.position)
    self.visit(trivia: token.trailingTrivia, at: token.endPositionBeforeTrailingTrivia)

can i ignore the trailingTrivia?

Since you're only interested in comments that are on their own line, yes. Trailing trivia includes all trivia up to the next token (if the next token is on the same line), or up to but not including the LF/CR/CRLF if the next token is on a later line. In the latter case, the leading trivia of that next token contains the line comments that preceded it.


how does SwiftSyntax represent Trivia at the end of the file, with no “next token”? is this stable/documented anywhere?

Every file is terminated by an eof token, which maintains the trivia invariant above.