Skip to content

More trivia changes - #220

Open
bit-aloo wants to merge 10 commits into
rust-analyzer:masterfrom
bit-aloo:2026-09-08-more-trivia-changes
Open

More trivia changes#220
bit-aloo wants to merge 10 commits into
rust-analyzer:masterfrom
bit-aloo:2026-09-08-more-trivia-changes

Conversation

@bit-aloo

@bit-aloo bit-aloo commented Sep 8, 2026

Copy link
Copy Markdown
Member

No description provided.

Comment thread src/green/token.rs Outdated
}

#[must_use]
pub fn splice_trivia<R, I>(&self, side: TriviaSide, range: R, replace_with: I) -> GreenToken

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm hesitant to offer such API because new rowan cannot have it; in it, the only way to create a new node or token is a from-scratch builder. So if you want this to aid with the migration I'll happily merge it, but it cannot be the final form.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Moved it to r-a

Comment thread src/ast.rs Outdated
/// Returns a [`SyntaxNodePtr`] for the node.
pub fn new(node: &SyntaxNode<L>) -> Self {
Self { kind: node.kind(), range: node.text_range() }
Self { kind: node.kind(), range: node.text_range_without_outer_trivia() }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is SyntaxNodePtr, of all things, using this method? It could maybe be useful for other things but SyntaxNodePtr only purpose in life is locating nodes; the range doesn't matter as long as it's unique, so why choose the less natural range?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yup agree. Removed it.

Comment thread src/api.rs Outdated
self.raw.index()
}

pub fn child_index(&self) -> Option<usize> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should probably remove index() and call this index() instead. What index() does with trivia is highly non-natural and I wouldn't even want to commit to it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Comment thread src/api.rs Outdated
self.raw.last_token().map(SyntaxToken::from)
}

pub fn next_structural_token(&self) -> Option<SyntaxToken<L>> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"Structural" isn't really descriptive. I suggest [next/prev]_non_trivia_token(). It's long but crystal clear.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed

Comment thread src/cursor.rs Outdated

pub fn first_token(&self) -> Option<SyntaxToken> {
self.first_child_or_token()?.first_token()
self.children_with_tokens().find_map(|child| child.first_token())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This doesn't include trivia, right? I missed this in the previous PR but we might want it to do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nope, we have new set of APIs now.

Comment thread src/cursor.rs Outdated
(None, Some(right)) => !right.is_trivia() && right.prev_structural_token().is_none(),
(None, None) => false,
};
adjacent.then(|| {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh this only works if they are adjacent. So at least give it a name like trivia_between_adjacent(), and consider making it assert they're adjacent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was horrible. I have new trivia_before and trivia after APIs exposed.

@bit-aloo
bit-aloo force-pushed the 2026-09-08-more-trivia-changes branch 2 times, most recently from eddf3c3 to a0b80bf Compare September 11, 2026 08:30
@bit-aloo
bit-aloo marked this pull request as ready for review September 11, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants