-
Notifications
You must be signed in to change notification settings - Fork 3.7k
(java) .class
should not detect as keyword
#2428
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
Comments
diff --git a/src/languages/java.js b/src/languages/java.js
index c70b99b0..9b73fb40 100644
--- a/src/languages/java.js
+++ b/src/languages/java.js
@@ -2,6 +2,7 @@
Language: Java
Author: Vsevolod Solovyov <[email protected]>
Category: common, enterprise
+Website: https://www.java.com/
*/
function(hljs) { Interesting... you may have found a parser regression of some kind. |
e31cccd#diff-5d9139cb5fca768116544c986e8c2c8cL177 Note: To be clear this commit is NOT the breaking change - merely the snippet of code that provided the prior behavior (that finally got removed cause it was dead code). The actual breaking change would have been the large PR that fixed look-ahead regexs by reworking big pieces of the parser's internals. So weird. Looks like sort of a poor mans negative look-behind to prevent keywords from triggering when following "." but a pretty hidden/strange way of going about it. This only worked in the older parser because of how it had to run every regex test twice essentially ... so the parser would first see the match ( The new parser does all this in a single pass... so we'll need a different fix. Really wish JS had negative look-behinds more readily available. |
class
and =
in two arguments for a funciton.class
, should not be keyword
.class
, should not be keyword.class
, should not be keyword
.class
, should not be keyword.class
should not detect as keyword
Curious: Is it common to overload the keyword |
@taoalpha We're discussing this one. If you're looking for a quick-fix and can build your own build from source the following patch would resolve the issue temporarily (just for Java): diff --git a/src/languages/java.js b/src/languages/java.js
index f349a76a..7a08a36b 100644
--- a/src/languages/java.js
+++ b/src/languages/java.js
@@ -74,6 +74,7 @@ export default function(hljs) {
hljs.C_BLOCK_COMMENT_MODE,
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE,
+ hljs.METHOD_GUARD,
{
className: 'class',
beginKeywords: 'class interface', end: /[{;=]/, excludeEnd: true, |
I'm wondering whether
Meaning that Look how IntelliJ IDEA highlights the above example: |
I'd argue it's actually method dispatch there instead of a "language keyword", but a lot of that would depend on how the exact grammar is defining "keyword". I think with how Java uses keywords I'd vote no on this. I think you're getting too hung up on the exact wording... but you might be right. I'm not certain of my stance. If you are correct that simply means we'd need to add @taoalpha You want to weigh in on this? |
I also wouldn't base our opinion solely on that or we'd have to go and change a lot of languages. We often highlight a lot more or less than the "official" docs (or environment) for said language does - and you'd think if there was going to be any canonical "best" highlighting (for a given community) it would be with the project docs itself. For an example see the currently open issue on Twig. I can go either way on this one, I'm not holding a strong opinion, I just want to be clear I don't think it's 100% clear cut either way. |
I'm not sure either. (And of course, that IntelliJ screenshot is no more than an example. I saw that GitHub doesn't highlight I'm concerned with the special handling of an individual character (dot). Let's imagine there is a language with |
First off, lets be clear this was a legacy decision (that beginKeywords has magic I don't see another choice unless we're just going to say "that behavior was misguided, we're removing it on purpose now". Which I suppose we could do, but I think that'd be the first intentional large grammar change we've made.
I don't think so. I think the In the future all of this will be possible with look-ahead and look-behind, so you can imagine doing it all with nothing more than the regex engine, but we're not there yet. I'd say the reason this hasn't been spotted before now is that it's not super common for a language to use method names that shadow keywords - but it's possible in many languages (Ruby, JS, Java, just to name a few). |
So the immediate question is what is the best short-term fix for this... adding So it's clear my proposal:
|
Ah, that was it. I had a feeling that I missed something important. |
Except it's not just Java, it's many grammars... and I kind of agree but just think we currently lack the proper tools to "fix" them nicely. :-)
When we have those tools at our disposal we can always revisit this and rip this hard coded behavior out. |
Describe the issue
when have
class
key word and=
in the arguments of a function, it will break the highlighting. A minimal code to reproduce this issue on 9.18.1 as follow:while the code example works fine for 9.15.1.
9.18.1: http://jsfiddle.net/p9r1yd36/
9.15.1: http://jsfiddle.net/p9r1yd36/1/
Expected behavior
https://imgur.com/RTUfqny
Actual behavior
https://imgur.com/YU7ZDcj
The text was updated successfully, but these errors were encountered: