-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
fix(rust): add support for r# raw identifiers #3947
Conversation
src/languages/rust.js
Outdated
$pattern: hljs.IDENT_RE + '!?', | ||
$pattern: IDENT_RE + '!?', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think any of our literal list of keywords start with #r
(since I don't see that you've added any keywords) so if that's the case this should be reverted to reduce the work the keyword engine needs to do.
"overmatching" with $pattern for things that will never be found in the keyword lists only slows the engine down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment!
You're right, there are no keywords starting with r#
. I fixed that without clearly understanding the object.
I'll reflect and commit!
src/languages/rust.js
Outdated
const UNDERSCORE_IDENT_RE = '(r#)?[a-zA-Z_]\\w*'; | ||
const IDENT_RE = '(r#)?[a-zA-Z]\\w*'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const RAW_IDENTIFIER = '(r#)?'
const UNDERSCORE_INDENT_RE = RAW_IDENTIFIER + hljs.UNDERSCORE_INDENT_RE;
const INDENT_RE = RAW_IDENTIFIER + hljs.INDENT_RE
maybe above code looks better? (just suggestion 😄)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment!
That's a great suggestion. I think it looks much better! I'll reflect and commit!
src/languages/rust.js
Outdated
export default function(hljs) { | ||
// ============================================ | ||
// Added to support the r# keyword, which is a raw identifier in Rust. | ||
const RAW_IDENTIFIER = '(r#)?'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets use //
regex here (not string)... and pull in our regex
utils and use concat
for this... see abnf for an example.
https://github.com/highlightjs/highlight.js/blob/main/src/languages/abnf.js#L57
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've committed to reflect your request.
However, i found one more issue.
Looking at the above test, the r#
when the identifier is declared is working as desired, but the
println!("{}", r#try);
When the identifier is referenced again, try
appears as a reserved keyword again in r#try
.
This issue is addressed in
#3947 (comment)
Here, I found that changing the code back to the one I had reverted earlier fixed it.
- Code where the reference to the
r#try
identifier is not recognized
name: 'Rust',
aliases: [ 'rs' ],
keywords: {
$pattern: hljs.IDENT_RE + '!?',
type: TYPES,
keyword: KEYWORDS,
literal: LITERALS,
built_in: BUILTINS
},
- Code where the reference to the
r#try
identifier is recognized
name: 'Rust',
aliases: [ 'rs' ],
keywords: {
$pattern: IDENT_RE + '!?',
type: TYPES,
keyword: KEYWORDS,
literal: LITERALS,
built_in: BUILTINS
},
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last small thing and this is good to merge I think.
Thanks so much!
Build Size ReportChanges to minified artifacts in 3 files changedTotal change +35 B View Changes
|
Ok, now we're talking about false positives, which is a different situation entirely. I'm not sure we should use the keyword engine to consume these (but not match, and hence not apply scoping)... can we add a few failing tests for this case and I'll take another look? |
Good! I'll create a new issue for the new problem and figure out how to fix it. Please feel free to provide any additional feedback or suggestions for improvement in this PR. I'm open to making further corrections if needed. Thanks so much! |
#3924
Changes
In order to support raw identifiers in Rust, the regular expressions for
UNDERSCORE_IDENT_RE
andIDENT_RE
need to be modified to include the r# keyword. However, since modes.js is shared by other languages, we need to ensure that only rust supports the r# keyword.Original (modes.js)
Change (rust.js)
So, add the following code to rust.js to support the r# keyword only in rust.
Checklist
CHANGES.md