Skip to content

Out of context highlighing of contextual keywords in csharp (starting with "file") #2805

@jofish920

Description

@jofish920

Following on from #2710, which pointed out that previously the csharp lexer was not properly identifying new contextual keywords as keywords, I looked at some contexts in which the current solution might overgeneralise. Here is an example (not good C#, but an effective illustration):

image

As can be seen (and confirmed by viewing the output through the raw formatter), the current code is treating almost every instance of "file" as a keyword.

In case it isn't clear in the image, the code used to produce the image above is as follows:

file record X(int file);
file record Y(int blah);

public static class Program {
    static int file(Func<int,int> file) => file(13);
    static int blah(Func<int,int> blah) => blah(13);

    public static void Main(string[] args) {
        int file = Program.file(file => 42);
        int blah = Program.blah(blah => 12);
        X x = new(9);
        Console.WriteLine($"{file}{blah}{x}{args}");
    }
}

I wonder if the results could be improved by only recognising "file" as a keyword if it is followed by something like "record", "class", or "enum", and how many of the other contextual keywords would be susceptible to similar handling.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions