rust-lang / rust-analyzer

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

Syntax highlighting in VSCode is messed up #16402

Open coolCucumber-cat opened 8 months ago

coolCucumber-cat commented 8 months ago

rust-analyzer version: 0.3.1807-standalone (9d8889cdf 2024-01-11)

rustc version: 1.75.0 (82e1608df 2023-12-21)

Syntax highlighting looks like this: image Unreadable and ugly. The attribute start #[ and the angle bracket < and > need to be recognised as being brackets.

davidbarsky commented 8 months ago

Unreadable and ugly.

I get your frustration, but please be kinder in the future.

The attribute start #[ and the angle bracket < and > need to be recognised as being brackets.

It's not clear to me what your complaint is. Are you saying that the attribute start/ends and and the angle brackets near/above at pub struct Vec should be highlighted yellow?

coolCucumber-cat commented 8 months ago

@davidbarsky VSCode needs to recognise that #[ and < are opening brackets and that > is a closing bracket. I forgot to mention this is because of bracket pair colourisation, where VSCode highlights alternating bracket pairs in yellow, purple and blue (then goes back to yellow) to make the code more readable. So the angle brackets after pub struct Vec would be yellow, then the ones after buf: RawVec would be purple etc.

Veykril commented 8 months ago

The bracket categorization is done by the rust textmate grammar which is not part of rust-analyzer but VSCode itself. So that has to be fixed there.

coolCucumber-cat commented 8 months ago

@Veykril And where would that be?

Veykril commented 8 months ago

The grammar is here https://github.com/microsoft/vscode/blob/8494a40f51580026ba241617eb6847ff585b8f70/extensions/rust/syntaxes/rust.tmLanguage.json

I don't know the process of contributing to that though. And then https://github.com/rust-lang/rust-analyzer/issues/14985#issuecomment-1577478634 has to be done as well (which goes into this file https://github.com/microsoft/vscode/blob/8494a40f51580026ba241617eb6847ff585b8f70/extensions/rust/language-configuration.json)

coolCucumber-cat commented 8 months ago

I'm not authorised to create pull requests or issues in the main vscode issues, so if you are already a contributor there, you just have to replace the contents of extensions/rust/language-configuration.json with this:

{
    "comments": {
        "lineComment": "//",
        "blockComment": [ "/*", "*/" ]
    },
    "brackets": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"],
        ["<", ">"],
        ["#[", "]"],
        ["#![", "]"]
    ],
    "autoClosingPairs": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"],
        ["<", ">"],
        ["#[", "]"],
        ["#![", "]"],
        { "open": "\"", "close": "\"", "notIn": ["string"] }
    ],
    "surroundingPairs": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"],
        ["\"", "\""],
        ["<", ">"]
    ],
    "indentationRules": {
        "increaseIndentPattern": "^.*\\{[^}\"']*$|^.*\\([^\\)\"']*$",
        "decreaseIndentPattern": "^\\s*(\\s*\\/[*].*[*]\\/\\s*)*[})]"
    },
    "folding": {
        "markers": {
            "start": "^\\s*//\\s*#?region\\b",
            "end": "^\\s*//\\s*#?endregion\\b"
        }
    }
}
coolCucumber-cat commented 8 months ago

I found the source of the issue and it's not in the VSCode repo, it's here, in editors/code/language-configuration.json. The problem is that it can't tell angle brackets and other symbols apart. I know that TypeScript can do this somehow, but I can't find where.

Young-Flash commented 8 months ago

with

"rust-analyzer.semanticHighlighting.punctuation.enable": true,
"rust-analyzer.semanticHighlighting.punctuation.specialization.enable": true,

angle can be recognize as the corresponding semantic token type

image

Veykril commented 8 months ago

Yes we would need to do what the typescript extension does, but what that does is tightly coupled with the textmate grammar, as you need to exclude the scopes of the non-delimiterlike angle bracket usages, see https://github.com/microsoft/vscode/blob/adf93c270acf019a7ab0385ff5d9a66eba7aafc3/extensions/typescript-basics/package.json#L69-L76 We can't do that without landing change to the textmate grammar as all angle brackets are merely tagged with punctuation.brackets.angle.rust.

coolCucumber-cat commented 8 months ago

@Young-Flash I managed to make it be like on your screen, but how can I make the bracket pair colourisation work with that knowledge?

coolCucumber-cat commented 8 months ago

@Veykril I think this would be a good idea, even without bracket pair colourisation. If someone wanted to colour angle bracket pairs, but not singular angle brackets, like comparisons, you would need this for that (or at least make it a lot easier)

Young-Flash commented 8 months ago

@Young-Flash I managed to make it be like on your screen, but how can I make the bracket pair colourisation work with that knowledge?

image

what I have in my vscode settings.json

{
    "rust-analyzer.semanticHighlighting.punctuation.enable": true,
    "rust-analyzer.semanticHighlighting.punctuation.specialization.enable": true,
    "editor.semanticTokenColorCustomizations": {
        "rules": {
            "bracket": "#22ff00",
        }
    },
}
coolCucumber-cat commented 8 months ago

@Young-Flash Ok thanks but that doesn't help me get bracket colour pair colourisation. It seems to work just as well to just set the style of punctuation.brackets.angle.rust.