-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(ruby) don't highlight interpolation in single quoted strings #4178
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
Open
verhovsky
wants to merge
3
commits into
highlightjs:main
Choose a base branch
from
verhovsky:ruby-strings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,7 @@ export default function(hljs) { | |
/\b([A-Z]+[a-z0-9]+)+/, | ||
// ends in caps | ||
/\b([A-Z]+[a-z0-9]+)+[A-Z]+/, | ||
) | ||
; | ||
); | ||
const CLASS_NAME_WITH_NAMESPACE_RE = regex.concat(CLASS_NAME_RE, /(::\w+)*/) | ||
// very popular ruby built-ins that one might even assume | ||
// are actual keywords (despite that not being the case) | ||
|
@@ -122,56 +121,128 @@ export default function(hljs) { | |
end: /\}/, | ||
keywords: RUBY_KEYWORDS | ||
}; | ||
const STRING = { | ||
|
||
function string_variants(prefix, delimiters) { | ||
return delimiters.map((d) => { | ||
return { | ||
begin: regex.concat(prefix, regex.escape(d.charAt(0))), | ||
end: regex.escape(d.charAt(1)) | ||
} | ||
}) | ||
} | ||
|
||
const STRING_DELIMITERS = [ | ||
"()", | ||
"[]", | ||
"{}", | ||
"<>", | ||
"\\/\\/", | ||
"%%", | ||
"--" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we want to flush this out a fit further while we're here? |
||
]; | ||
|
||
const SINGLE_QUOTED_STRING = { | ||
className: 'string', | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
], | ||
contains: [ hljs.BACKSLASH_ESCAPE ], | ||
variants: [ | ||
{ | ||
begin: /'/, | ||
end: /'/ | ||
}, | ||
...string_variants("%q", STRING_DELIMITERS) | ||
] | ||
} | ||
|
||
const DOUBLE_QUOTED_STRING = { | ||
className: 'string', | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
], | ||
variants: [ | ||
{ | ||
begin: /"/, | ||
end: /"/ | ||
}, | ||
...string_variants("%Q", STRING_DELIMITERS) | ||
] | ||
} | ||
|
||
// TODO: continue to break these out into smaller more discrete modes | ||
const OLD_STRINGS_TOO_MANY_VARIANTS = { | ||
className: 'string', | ||
contains: [ hljs.BACKSLASH_ESCAPE ], | ||
variants: [ | ||
{ | ||
begin: /`/, | ||
end: /`/ | ||
end: /`/, | ||
contains: [ | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\(/, | ||
end: /\)/ | ||
begin: /%[wWx]?\(/, | ||
end: /\)/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\[/, | ||
end: /\]/ | ||
begin: /%[wWx]?\[/, | ||
end: /\]/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\{/, | ||
end: /\}/ | ||
begin: /%[wWx]?\{/, | ||
end: /\}/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?</, | ||
end: />/ | ||
begin: /%[wWx]?</, | ||
end: />/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\//, | ||
end: /\// | ||
begin: /%[wWx]?\//, | ||
end: /\//, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?%/, | ||
end: /%/ | ||
begin: /%[wWx]?%/, | ||
end: /%/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?-/, | ||
end: /-/ | ||
begin: /%[wWx]?-/, | ||
end: /-/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\|/, | ||
end: /\|/ | ||
begin: /%[wWx]?\|/, | ||
end: /\|/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
// in the following expressions, \B in the beginning suppresses recognition of ?-sequences | ||
// where ? is the last character of a preceding identifier, as in: `func?4` | ||
|
@@ -181,27 +252,35 @@ export default function(hljs) { | |
{ begin: /\B\?(\\M-\\C-|\\M-\\c|\\c\\M-|\\M-|\\C-\\M-)[\x20-\x7e]/ }, | ||
{ begin: /\B\?\\(c|C-)[\x20-\x7e]/ }, | ||
{ begin: /\B\?\\?\S/ }, | ||
// heredocs | ||
{ | ||
// this guard makes sure that we have an entire heredoc and not a false | ||
// positive (auto-detect, etc.) | ||
begin: regex.concat( | ||
/<<[-~]?'?/, | ||
regex.lookahead(/(\w+)(?=\W)[^\n]*\n(?:[^\n]*\n)*?\s*\1\b/) | ||
), | ||
] | ||
}; | ||
|
||
const HEREDOC = { | ||
scope: "string", | ||
// this guard makes sure that we have an entire heredoc and not a false | ||
// positive (auto-detect, etc.) | ||
begin: regex.concat( | ||
/<<[-~]?'?/, | ||
regex.lookahead(/(\w+)(?=\W)[^\n]*\n(?:[^\n]*\n)*?\s*\1\b/) | ||
), | ||
contains: [ | ||
hljs.END_SAME_AS_BEGIN({ | ||
begin: /(\w+)/, | ||
end: /(\w+)/, | ||
contains: [ | ||
hljs.END_SAME_AS_BEGIN({ | ||
begin: /(\w+)/, | ||
end: /(\w+)/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}) | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
} | ||
}) | ||
] | ||
}; | ||
} | ||
|
||
const STRINGS = [ | ||
SINGLE_QUOTED_STRING, | ||
DOUBLE_QUOTED_STRING, | ||
HEREDOC, | ||
OLD_STRINGS_TOO_MANY_VARIANTS | ||
] | ||
|
||
// Ruby syntax is underdocumented, but this grammar seems to be accurate | ||
// as of version 2.7.2 (confirmed with (irb and `Ripper.sexp(...)`) | ||
|
@@ -316,8 +395,34 @@ export default function(hljs) { | |
scope: "title.class" | ||
}; | ||
|
||
const SYMBOL = { | ||
className: 'symbol', | ||
variants: [ | ||
{ | ||
begin: regex.concat(/:/, RUBY_METHOD_RE) | ||
}, | ||
{ | ||
begin: /:"/, | ||
end: /"/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ | ||
begin: /:'/, | ||
end: /'/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE | ||
] | ||
} | ||
], | ||
relevance: 0 | ||
}; | ||
|
||
const RUBY_DEFAULT_CONTAINS = [ | ||
STRING, | ||
SYMBOL, | ||
...STRINGS, | ||
CLASS_DEFINITION, | ||
INCLUDE_EXTEND, | ||
OBJECT_CREATION, | ||
|
@@ -332,15 +437,6 @@ export default function(hljs) { | |
begin: hljs.UNDERSCORE_IDENT_RE + '(!|\\?)?:', | ||
relevance: 0 | ||
}, | ||
{ | ||
className: 'symbol', | ||
begin: ':(?!\\s)', | ||
contains: [ | ||
STRING, | ||
{ begin: RUBY_METHOD_RE } | ||
], | ||
relevance: 0 | ||
}, | ||
NUMBER, | ||
{ | ||
// negative-look forward attempts to prevent false matches like: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did you mean
d.charAt(1)
won't work because it's 4 characters long.https://docs.ruby-lang.org/en/3.1/syntax/literals_rdoc.html#label-Percent+Literals says
Which seems like something regex can't handle.