rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.26k stars 1.61k forks source link

Rainbow brackets on type generics #14985

Open Seltyk opened 1 year ago

Seltyk commented 1 year ago

VS Code's built-in TypeScript syntax highlighting supports rainbow brackets for type generics, as evidenced by this repro.

type Test = Array<Array<Array<string>>>;

Admittedly, only the left angle brackets are rainbowified on my system, though I have seen correct coloring in real code.

rust-analyzer, does not rainbowify angle brackets, as evidenced by this repro

type Test = Option<(String, Vec<(bool, String)>)>

Clearly, it is possible to have rainbow type generics, and I would like to see that in future. It would make reading complex types (such as session types for network communication protocols) easier.
Of course, this Test type is a bit silly, and cargo clippy does warn about complex types, suggesting layered aliasing to make them easier to read and understand. However, the added readability of rainbow brackets should not be ignored.

Veykril commented 1 year ago

We don't host the textmate grammar for rust, that is upstreamed in VSCode. The rust extension there (which solely provides said grammar) would need to set appropriate balancedBracketScopes, something like

"balancedBracketScopes": [
                    "punctuation.brackets.angle.rust",
                    "punctuation.brackets.square.rust",
                    "punctuation.brackets.curly.rust",
                    "punctuation.brackets.round.rust",
                    "punctuation.brackets.attribute.rust"
                ]

Ideally the whole thing would work with semantic tokens as thats what rust-analyzer emits but VSCode doesn't allow that, neither does it respect semantic token to textmate token fallback to work with it. So there isn't really anything to do from our side here.

Edit: Actually nevermind, the rust extension would need to do what typescript does here, explicitly listing wrong scopes https://github.com/microsoft/vscode/blob/adf93c270acf019a7ab0385ff5d9a66eba7aafc3/extensions/typescript-basics/package.json#L69-L76 which is incompatible with macros unfortunately. So this is blocked on https://github.com/microsoft/vscode/issues/176644