Stream: t-compiler/rust-analyzer

Topic: Questions about the rowan crate


Jack (Dec 03 2020 at 21:56, on Zulip):

As I understand it, the rowan crate is developed by the rust-analyzer team, right? I had a few questions regarding it.
Mainly, is there a way I can change the "position" of a token? I've just been doing "some code".into() when passing the text argument of GreenNodeBuilder::token, which doesn't appear to do what I want

Lukas Wirth (Dec 03 2020 at 22:09, on Zulip):

I don't think GreenNodes even know about positions. Only SyntaxNodes.

Lukas Wirth (Dec 03 2020 at 22:14, on Zulip):

And from what I can see you cant arbitrarily set a tokens or nodes position as its based on the entire tree, but I might be wrong.

Jack (Dec 03 2020 at 22:14, on Zulip):

Looking at the output of dbg!(&builder), I think you're right. I had assumed that it might have been something that was part of SmolStr.

Jack (Dec 03 2020 at 22:16, on Zulip):

Any ideas on how I might be able to go about something similar? I want to be able to display the position of errors in source code, just like rust.

Lukas Wirth (Dec 03 2020 at 22:17, on Zulip):

Why do you need to change the position for that?

Lukas Wirth (Dec 03 2020 at 22:18, on Zulip):

you want to insert the error messages into the source?

Jack (Dec 03 2020 at 22:30, on Zulip):

No, what I want is to be able to accurately figure out where a token came from in the source code

Lukas Wirth (Dec 03 2020 at 22:33, on Zulip):

What you want is https://docs.rs/rowan/0.10.0/rowan/api/struct.SyntaxNode.html#method.text_range and https://docs.rs/rowan/0.10.0/rowan/api/struct.SyntaxToken.html#method.text_range then I believe

Lukas Wirth (Dec 03 2020 at 22:34, on Zulip):

Those TextRanges contain the offset and the size the text of the node spans

Jack (Dec 03 2020 at 22:37, on Zulip):

Right, but only within text that has been turned into some token. If i want to ignore whitespace, then all of the positions of keywords will be distorted

Lukas Wirth (Dec 03 2020 at 22:46, on Zulip):

Well, SyntaxNodes only know their positions based on the entire text of the tree they belong to. If you want to ignore whitespace and have positions according to the whitespace-less text you would have to reconstruct the tree without the whitespace tokens.

Jack (Dec 03 2020 at 22:48, on Zulip):

Ah alright. Thanks!

Last update: Jul 29 2021 at 08:15UTC