Skip to content
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

token_first_child_in_range, token_last_child_in_range, token_child_for_offset limited at the end of the document #256

Open
DivineDominion opened this issue Jul 6, 2024 · 1 comment

Comments

@DivineDominion
Copy link
Contributor

DivineDominion commented Jul 6, 2024

This is another use of the AST as a data source:

Performing tree traversal, I noticed that an empty document will produce a DOC_START_TOKEN + BLOCK_EMPTY, both with a range of start = 0 and length = 0.

This defeats tree traversal implementations in token_first_child_in_range, token_last_child_in_range, token_child_for_offset that can't descend into empty tokens.

I've been working with this more intensely the past months and to my surprise found that an offset and sub-range based check work well like so:

  1. Does a character range _R_ include offset _O_? A check for inclusion entails that the range is not empty and the offset falls chiefly inside it: R.start <= O && R.start + R.length > O
  2. Does a character range _Ra_ contain character range _Rb_? A check for subrange overlap will falsely (= my argument) reject the same range iff both are empty. (0..<0) and (0..<0) would not overlap at the moment.

For the subrange check I found that this works fine (again, to my surprise), including equal ranges:

    (Ra.start <= Rb.start
        && Ra.start + Ra.length  >= Rb.start + Rb.length)

Consequences:

  • the current approach can't traverse empty trees, as a DOC_START_TOKEN + BLOCK_EMPTY with length of 0 cannot be drilled down into;
  • the end-of-file position is always past everything: past the document range, and past any of its tokens, so you never know the context of the blinking cursor at the very end
@DivineDominion DivineDominion changed the title token_first_child_in_range, token_last_child_in_range, token_child_for_offset can't traverse empty trees token_first_child_in_range, token_last_child_in_range, token_child_for_offset limited at the end of the document Jul 6, 2024
@fletcher
Copy link
Owner

fletcher commented Jul 6, 2024

I'm not sure I understand what the problem is here. What are you trying to accomplish?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants