fletcher / fletcher/MultiMarkdown-6

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

Open
#256 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
678
Forks
94
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.