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

Flatten highlights #3447

Closed
matklad opened this issue Mar 4, 2020 · 4 comments
Closed

Flatten highlights #3447

matklad opened this issue Mar 4, 2020 · 4 comments
Labels
E-has-instructions Issue has some instructions and pointers to code to get started E-medium

Comments

@matklad
Copy link
Member

matklad commented Mar 4, 2020

The highlight function currently returns a Vec<HighlightedRange>. However, those highlighting ranges can be nested. I believe that for something like #[cfg(feature = "foo")] we produce the range for the whole attribute, and then, separately, a range for a string literal.

It seems like all the clients of syntax highlighting actually want a flat list of highlights. We should refactor highlight so that it produces such flat list. One obvious way to do this is to post-process the current return type, but, given that internally we walk the tree in dfs manner, perhaps we can just not produce nested things in the first place?

The relevant code is here:

https://github.com/rust-analyzer/rust-analyzer/blob/a5da660bd5953c046db5f0bff854c00d830da9c8/crates/ra_ide/src/syntax_highlighting.rs#L34-L38

@matklad matklad added E-medium E-has-instructions Issue has some instructions and pointers to code to get started labels Mar 4, 2020
@ltentrup
Copy link
Contributor

ltentrup commented Apr 2, 2020

I have had a look at the specific code and I would like to work on it.
My first idea is to use a stack Vec<Vec<HighlightedRange>> mirroring the DFS traversal. When leaving a node, one can implement the flattening.
To implement the flattening itself, one has to clone HighlightedRange but it does not implement Clone:

https://github.com/rust-analyzer/rust-analyzer/blob/a5da660bd5953c046db5f0bff854c00d830da9c8/crates/ra_ide/src/syntax_highlighting.rs#L27-L32

I am not sure about the semantics of binding_hash, can it be cloned or must it be unique?

@lnicola
Copy link
Member

lnicola commented Apr 2, 2020

@matklad do we still want the rainbow stuff after #3820? It's only used in a test.

@matklad
Copy link
Member Author

matklad commented Apr 2, 2020 via email

bors bot added a commit that referenced this issue Apr 8, 2020
3826: Flatten nested highlight ranges during DFS traversal r=matklad a=ltentrup

Implements the flattening of nested highlights from #3447.


There is a caveat: I needed to add `Clone` to `HighlightedRange` to split highlight ranges  ~and the nesting does not appear in the syntax highlighting test (it does appear in the accidental-quadratic test but there it is not checked against a ground-truth)~.

I have added a test case for the example mentioned in #3447.

Co-authored-by: Leander Tentrup <[email protected]>
@kjeremy
Copy link
Contributor

kjeremy commented Apr 8, 2020

Did #3826 solve this?

@matklad matklad closed this as completed Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-has-instructions Issue has some instructions and pointers to code to get started E-medium
Projects
None yet
Development

No branches or pull requests

4 participants