tree-sitter / tree-sitter-c-sharp

C# Grammar for tree-sitter
MIT License
177 stars 47 forks source link

Highlight members within a class differently than local variables #325

Closed rudiejd closed 4 months ago

rudiejd commented 4 months ago

Minimal example:

class Example {
    private readonly int _member;
    public Example(int member) {
        _member = member;
    }
}

We should have some way to highlight the members within the member assignment differently than we highlight the other local variables (for example, the member argument to Example default constructor.

highlighting in neovim using this parser: image

highlighting in jetbrains rider: image

damieng commented 4 months ago

tree-sitter-c-sharp is just a parser - we tokenize the language keywords. These are all just identifiers to us as per the C# Language Specification. We can't know if _member is a variable, parameter, instance variable etc. without a compilation model to resolve variables recursively within scopes.

That is well beyond what tree-sitter and indeed any C# parser is capable off and into compiler territory. To get that you'd need to switch to a language server with a real compiler such as one backed by Roslyn.