-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
take empty tokens into account #51
base: master
Are you sure you want to change the base?
Conversation
also implements token_at_offset TODO and adds a test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! I don't know exactly about the pros/cons of supporting empty tokens, I'll let @matklad decide on that :)
src/cursor.rs
Outdated
} | ||
} else { | ||
left.token_at_offset(offset) | ||
unreachable!() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should theoretically be able to be reached if there's a node with zero children... Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for nodes without children that shouldn't be reached because of
Lines 452 to 454 in be215ee
if range.is_empty() { | |
return TokenAtOffset::None; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did find another issue related to empty nodes. I fixed it and added a couple of tests. 2bfdd9e
I am very hesitant about supporting empty tokens, as they open all kinds of nasty edge cases. In particular, with empty tokens, Initially (way back in I'd love to go further and even forbid empty internal nodes, but that unfortunately hits a really horrible edge-case: an empty file. I am even wondering if the API would be better if we had an explicit EMPTY_FILE token for this case.... |
But returning I realise there could be arbitrary many empty tokens, but Other alternative would be to disallow them when building them. |
Yeah, we currently "soft-disalow" them, in that code assumes that tokens are non-empty. We don't have any active assetions though, because nothing actively breaks with empty tokens. If one has to have empty tokens, one can write custom algorithms which do something resonable |
When I first learned indexes, I learned them like the cursor of a text editor:
Per this understanding of the offset (cursor position), then For example, given the tokens |
This is usefull if you are creating empty tokens to represent virtual tokens that were added to make the ast make sense.
Before, for the tokens
["A", "+", ""]
callingtoken_at_offset(2)
would returnTokenAtOffset::Single("+")``. Now it returns
TokenAtOffset::Between("+", "")`.I've added a simple test, and I've also run the
rust-analyzer
tests with the modifiedrowan
crate.Something to take into account with this implementation is that if
token_at_offset
is called in a non root node withoffset
at the start or end of the node,TokenAtOffset::Between
will be returned with a token from outside this node.