Conversation
also implements token_at_offset TODO and adds a test
src/cursor.rs
Outdated
| } | ||
| } else { | ||
| left.token_at_offset(offset) | ||
| unreachable!() |
There was a problem hiding this comment.
This should theoretically be able to be reached if there's a node with zero children... Right?
There was a problem hiding this comment.
for nodes without children that shouldn't be reached because of
Lines 452 to 454 in be215ee
There was a problem hiding this comment.
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 |
|
is this still valid? |
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 returnsTokenAtOffset::Between("+", "")`.I've added a simple test, and I've also run the
rust-analyzertests with the modifiedrowancrate.Something to take into account with this implementation is that if
token_at_offsetis called in a non root node withoffsetat the start or end of the node,TokenAtOffset::Betweenwill be returned with a token from outside this node.