Open RDambrosio016 opened 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:"
Kind::Root(GreenNode)
to Kind::Root { node: GreenNode, offset: TextSize }
Kind
. If so, investigate finagling the definition to re-add a niche to keep the size the same. (Idea: child index probably can be guaranteed to fit in u16
.)Kind::Root(green)
to Kind::Root { node: green, .. }
, as well as the root constructor to take a root offset. (Or make a new constructor that takes an offset, and the existing one uses a zero offset.)SyntaxNode::text_range
to match the inner kind and read the root offset, rather than using the as_child
helper. Probably add a SyntaxNode::offset(&self) -> TextSize
helper.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.
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
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.
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:
raw_range
internal methodText would stay the same, as the green node will still have the right text, only text range of parent and children would be affected.