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
(csharp) add support for @identifier
style identifiers
#1843
Comments
Please provide a fuller example (a jsfiddle would be preferable). Are you even specifying Quick template: |
Taking your template, it'd look as follows: https://jsfiddle.net/gzqdomnj/1/ All three variables should have the same highlighting as identifiers, not keywords. This basically is a problem that there's no real parsing going on and contextual keywords are lumped in with normal keywords. Not sure whether there's a way around this, though, given how hljs operates. |
Well, with hljs you have to attack problems one thing at a time, try to make the part of the grammar you're having issues with more specific. Like in this case we'd consider trying to teach it what a variable declaration looks like. Then when it saw one it wouldn't be thinking about keywords. You could probably teach it keywords don't start with @... But that wouldn't necessarily prevent your variables from getting highlighted later in the codebase as keywords again. |
Can you so easily name variables after reserved words in Csharp? I think that's why this isn't a bigger problem because in most languages you can only use keywords as keywords. If doable, is that common? |
C# has two kinds of keywords: "normal" keywords, which is basically the set of keywords C# 1 started out with. Everything added after that are contextual keywords, which behave like keywords in their respective contexts, but can still be used as identifiers (they're not reserved). This has the benefit that all pre-existing code continues to work. The following code is legal, although I'd say no one would write that:
It can still be somewhat common for a limited set of contextual keywords that make for good variable names. Things like Additionally, prepending I guess the last part should be doable easily, trying to figure out all contexts where an identifier can appear to solve the other problem might be tricky without actually doing a lot of parsing already (and there's still ambiguities, of course, e.g. when naming a type |
To add to the last point, variable declarations are not the only place that can have identifiers. We have
That'd be a lot of syntax, of which most of it is different. |
I think the best to hope for here is to fix common cases:
This would probably allow to INVALID things such as Does C# haver other common things in the same vein like JS? const, let, var... if we fix var it'd be easier to fix those also I imagine. |
@ygra Could you contribute a small snippet of real sample code with a few different uses of @[keyword] as variables/identifiers? I think focusing on fixing that since it's an easy pattern would be a good start and resolve this issue for now. |
I'm not sure what sample code you need here. Any token consisting of valid identifier characters starting with
are identifiers and can only occur in places where an identifier can occur, e.g. var @class = new MyClass();
Console.WriteLine("{0}, {1}", @var, @foo)
... |
Perfect. So if we just add an exception for |
I think it should, indeed. |
@identifier
style identifiers
E.g.
group
is highlighted as if it were a keyword, even though it's an identifier. This probably extends to all other contextual keywords as well, likevar
,where
,from
,select
, etc.Sample:
https://jsfiddle.net/gzqdomnj/1/
The text was updated successfully, but these errors were encountered: