rust-analyzer / rowan

Apache License 2.0
689 stars 57 forks source link

Add the ability to make a SyntaxNode which is offset by a certain amount from the source code #73

Open RDambrosio016 opened 4 years ago

RDambrosio016 commented 4 years ago

Hello, i have been using rowan in my own project, it works absolutely great. However, when parsing something like a markdown file, i run into issues with text ranges because it assumes the root node starts at the start of the source. Which means i need to offset every single node, token, or element's range i make. It would be great if i could make a syntax node and tell rowan the range is actually offset by a certain amount, i tried to implement this myself but i ran into a good amount of issues, including:

Text would stay the same, as the green node will still have the right text, only text range of parent and children would be affected.

CAD97 commented 4 years ago

One possible way of kludging an offset together with the current API is to just put an extra green node in front with the desired offset length. Ofc, that requires a string of that length to back it, so it's not a great solution.

It in theory wouldn't be a difficult change to at a base offset to the syntax tree. In theory, "just:"

That should be all that's necessary to add a root offset to the syntax tree. That also should make it possible to "reroot" syntax trees, pulling out a subtree so the rest can potentially be reclaimed, though I don't know how useful that'd be in practice.

RDambrosio016 commented 4 years ago

I briefly asked @matklad about this over zulip, he brought up a great point.

matklad: green nodes are offset-less by design

matklad: that way, you don't have to change every green node when you type a space at the start of the file

Which makes a ton of sense, only syntax nodes should carry an offset. This change should not affect green nodes at all

CAD97 commented 4 years ago

That's why the plan I outline is just adding an offset to the root of the syntax tree; it doesn't touch the green tree.