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

Fix nondeterministic highlighting #3275

Merged

Conversation

A-Walrus
Copy link
Contributor

Resolves #2986.
Resolves #3230.
Probably resolves #2484 as well, although I didn't have the code to copy paste in order to check :P

This is done by preferring matches in the beginning, i.e. for keyword.function, keyword is a better match than function.

Explanation of original problem:

Source for non-determinism: HashMap traverse order is different each time.

According to comments:

Consumers of these queries can choose to recognize highlights with different levels of specificity.
For example, the string function.builtin will match against function.method.builtin
and function.builtin.constructor, but will not match function.method.

keyword.function can therefore match both keyword and function.

  • Both of these matches have the same match_len which is what is used to rank them
  • The list of scopes is traversed in order to find the match with the best match_len, in the
    random order it came from the HashMap.

This is done by prefering matches in the begining, ie for
`keyword.function`, `keyword` is a better match than `function`.
@A-Walrus

This comment was marked as resolved.

A-Walrus added 2 commits July 31, 2022 16:12
Fixes possible edgecase with something like `function.method.builtin`
and the queries `function.builtin` and `function.method`
@archseer
Copy link
Member

archseer commented Aug 1, 2022

There's an easier fix here: I think the original intends for matches to be from the start of the string. So keyword.function matches keyword, keyword.function, but not function.

Also change comments to match new behaviour
Copy link
Member

@the-mikedavis the-mikedavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks for the fix!

I think the old behavior may have been intentional but I think the new way is more straightforward and intuitive (plus less edge-case-y)

And thanks for updating the docs too :)

@the-mikedavis the-mikedavis added this to the 22.08 milestone Aug 3, 2022
@archseer archseer merged commit cfa8825 into helix-editor:master Aug 5, 2022
@archseer
Copy link
Member

archseer commented Aug 5, 2022

Thanks for fixing this! I'm really glad this was resolved 🎉

thomasskk pushed a commit to thomasskk/helix that referenced this pull request Sep 9, 2022
* Fix nondeterministic highlighting

This is done by prefering matches in the begining, ie for
`keyword.function`, `keyword` is a better match than `function`.

* Use all positions and not just leftmost

Fixes possible edgecase with something like `function.method.builtin`
and the queries `function.builtin` and `function.method`

* Switch to bitmask for slightly better performance

* Make matches from the start of string

Also change comments to match new behaviour
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants