-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(ruby) symbols, string interpolation, class names with underscores #4213
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
jimtng
wants to merge
9
commits into
highlightjs:main
Choose a base branch
from
jimtng:ruby-class-underscore
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 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
67644b9
fix(ruby) symbols, string interpolation, class names with underscores
jimtng 1ce45f3
remove negative lookbehind
jimtng 0be49c6
reduce relevance of string interpolation
jimtng f53b568
remove relevance
jimtng 094e626
match single-letter class name
jimtng 2017467
match single char constant
jimtng 34ee845
match class inheritance
jimtng 1e751a9
clean up last commas
jimtng eace348
remove the use of lookbehind
jimtng 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,7 @@ export default function(hljs) { | |
const regex = hljs.regex; | ||
const RUBY_METHOD_RE = '([a-zA-Z_]\\w*[!?=]?|[-+~]@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?)'; | ||
// TODO: move concepts like CAMEL_CASE into `modes.js` | ||
const CLASS_NAME_RE = regex.either( | ||
/\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+)*/) | ||
const CLASS_NAME_RE = /\b([A-Z]+[a-z0-9_]*)+[A-Z]*/; | ||
// very popular ruby built-ins that one might even assume | ||
// are actual keywords (despite that not being the case) | ||
const PSEUDO_KWS = [ | ||
|
@@ -120,19 +114,15 @@ export default function(hljs) { | |
className: 'subst', | ||
begin: /#\{/, | ||
end: /\}/, | ||
keywords: RUBY_KEYWORDS | ||
keywords: RUBY_KEYWORDS, | ||
}; | ||
const STRING = { | ||
const STRING_INTERPOLABLE = { | ||
className: 'string', | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
], | ||
variants: [ | ||
{ | ||
begin: /'/, | ||
end: /'/ | ||
}, | ||
{ | ||
begin: /"/, | ||
end: /"/ | ||
|
@@ -142,45 +132,37 @@ export default function(hljs) { | |
end: /`/ | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\(/, | ||
joshgoebel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end: /\)/ | ||
begin: /%[QWx]?\(/, | ||
end: /\)/, | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\[/, | ||
end: /\]/ | ||
begin: /%[QWx]?\[/, | ||
end: /\]/, | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\{/, | ||
end: /\}/ | ||
begin: /%[QWx]?\{/, | ||
end: /\}/, | ||
}, | ||
{ | ||
begin: /%[qQwWx]?</, | ||
end: />/ | ||
begin: /%[QWx]?</, | ||
end: />/, | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\//, | ||
end: /\// | ||
begin: /%[QWx]?\//, | ||
end: /\//, | ||
}, | ||
{ | ||
begin: /%[qQwWx]?%/, | ||
end: /%/ | ||
begin: /%[QWx]?%/, | ||
end: /%/, | ||
}, | ||
{ | ||
begin: /%[qQwWx]?-/, | ||
end: /-/ | ||
begin: /%[QWx]?-/, | ||
end: /-/, | ||
}, | ||
{ | ||
begin: /%[qQwWx]?\|/, | ||
end: /\|/ | ||
begin: /%[QWx]?\|/, | ||
end: /\|/, | ||
}, | ||
// 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` | ||
{ begin: /\B\?(\\\d{1,3})/ }, | ||
{ begin: /\B\?(\\x[A-Fa-f0-9]{1,2})/ }, | ||
{ begin: /\B\?(\\u\{?[A-Fa-f0-9]{1,6}\}?)/ }, | ||
{ 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 | ||
|
@@ -202,6 +184,55 @@ export default function(hljs) { | |
} | ||
] | ||
}; | ||
const STRING_NONINTERPOLABLE = { | ||
className: 'string', | ||
variants: [ | ||
{ | ||
begin: /'/, | ||
end: /'/ | ||
}, | ||
{ | ||
begin: /%[qw]?\(/, | ||
end: /\)/, | ||
}, | ||
{ | ||
begin: /%[qw]?\[/, | ||
end: /\]/, | ||
}, | ||
{ | ||
begin: /%[qw]?\{/, | ||
end: /\}/, | ||
}, | ||
{ | ||
begin: /%[qw]?</, | ||
end: />/, | ||
}, | ||
{ | ||
begin: /%[qw]?\//, | ||
end: /\//, | ||
}, | ||
{ | ||
begin: /%[qw]?%/, | ||
end: /%/, | ||
}, | ||
{ | ||
begin: /%[qw]?-/, | ||
end: /-/, | ||
}, | ||
{ | ||
begin: /%[qw]?\|/, | ||
end: /\|/, | ||
}, | ||
// 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` | ||
{ begin: /\B\?(\\\d{1,3})/ }, | ||
{ begin: /\B\?(\\x[A-Fa-f0-9]{1,2})/ }, | ||
{ begin: /\B\?(\\u\{?[A-Fa-f0-9]{1,6}\}?)/ }, | ||
{ begin: /\B\?(\\M-\\C-|\\M-\\c|\\c\\M-|\\M-|\\C-\\M-)[\x20-\x7e]/ }, | ||
{ begin: /\B\?\\(c|C-)[\x20-\x7e]/ }, | ||
{ begin: /\B\?\\?\S/ } | ||
] | ||
}; | ||
|
||
// Ruby syntax is underdocumented, but this grammar seems to be accurate | ||
// as of version 2.7.2 (confirmed with (irb and `Ripper.sexp(...)`) | ||
|
@@ -246,7 +277,7 @@ export default function(hljs) { | |
const INCLUDE_EXTEND = { | ||
match: [ | ||
/(include|extend)\s+/, | ||
CLASS_NAME_WITH_NAMESPACE_RE | ||
CLASS_NAME_RE | ||
], | ||
scope: { | ||
2: "title.class" | ||
|
@@ -259,15 +290,15 @@ export default function(hljs) { | |
{ | ||
match: [ | ||
/class\s+/, | ||
CLASS_NAME_WITH_NAMESPACE_RE, | ||
CLASS_NAME_RE, | ||
/\s+<\s+/, | ||
CLASS_NAME_WITH_NAMESPACE_RE | ||
CLASS_NAME_RE | ||
] | ||
}, | ||
{ | ||
match: [ | ||
/\b(class|module)\s+/, | ||
CLASS_NAME_WITH_NAMESPACE_RE | ||
CLASS_NAME_RE | ||
] | ||
} | ||
], | ||
|
@@ -301,7 +332,7 @@ export default function(hljs) { | |
const OBJECT_CREATION = { | ||
relevance: 0, | ||
match: [ | ||
CLASS_NAME_WITH_NAMESPACE_RE, | ||
CLASS_NAME_RE, | ||
/\.new[. (]/ | ||
], | ||
scope: { | ||
|
@@ -317,7 +348,8 @@ export default function(hljs) { | |
}; | ||
|
||
const RUBY_DEFAULT_CONTAINS = [ | ||
STRING, | ||
STRING_INTERPOLABLE, | ||
STRING_NONINTERPOLABLE, | ||
CLASS_DEFINITION, | ||
INCLUDE_EXTEND, | ||
OBJECT_CREATION, | ||
|
@@ -326,20 +358,28 @@ export default function(hljs) { | |
METHOD_DEFINITION, | ||
{ | ||
// swallow namespace qualifiers before symbols | ||
begin: hljs.IDENT_RE + '::' }, | ||
begin: '::' | ||
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. Yeah I think this works. |
||
}, | ||
{ | ||
className: 'symbol', | ||
begin: hljs.UNDERSCORE_IDENT_RE + '(!|\\?)?:', | ||
begin: hljs.UNDERSCORE_IDENT_RE + '(!|\\?)?:(?!:)', | ||
relevance: 0 | ||
}, | ||
{ | ||
className: 'symbol', | ||
begin: ':(?!\\s)', | ||
contains: [ | ||
STRING, | ||
{ begin: RUBY_METHOD_RE } | ||
{ begin: /'/, end: /'/ }, | ||
{ | ||
begin: /"/, end: /"/, | ||
contains: [ | ||
hljs.BACKSLASH_ESCAPE, | ||
SUBST | ||
] | ||
}, | ||
{ begin: hljs.UNDERSCORE_IDENT_RE } | ||
], | ||
relevance: 0 | ||
relevance: 1 | ||
}, | ||
NUMBER, | ||
{ | ||
|
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<span class="hljs-title class_">Class</span> | ||
<span class="hljs-title class_">ClassName</span> | ||
<span class="hljs-title class_">Class_Name</span> | ||
<span class="hljs-title class_">ClassNAME</span> | ||
<span class="hljs-title class_">ClassName</span>::<span class="hljs-title class_">With</span>::<span class="hljs-title class_">Namespace</span> | ||
<span class="hljs-title class_">ClassName</span>::<span class="hljs-title class_">With</span>.method | ||
::<span class="hljs-title class_">TopLevel</span>::<span class="hljs-title class_">Class</span> | ||
|
||
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Class</span>::<span class="hljs-title class_">Name</span> < <span class="hljs-title class_">With</span>::<span class="hljs-title class_">Inheritance</span> | ||
<span class="hljs-keyword">end</span> | ||
|
||
<span class="hljs-keyword">class</span> <span class="hljs-title class_">A</span>::<span class="hljs-title class_">B</span> < <span class="hljs-title class_">C</span>::<span class="hljs-title class_">D</span> | ||
<span class="hljs-keyword">end</span> |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Class | ||
ClassName | ||
Class_Name | ||
ClassNAME | ||
ClassName::With::Namespace | ||
ClassName::With.method | ||
::TopLevel::Class | ||
|
||
class Class::Name < With::Inheritance | ||
end | ||
|
||
class A::B < C::D | ||
end |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<span class="hljs-symbol">:symbol</span> | ||
<span class="hljs-symbol">:Symbol</span> | ||
<span class="hljs-symbol">:_leading</span> | ||
<span class="hljs-symbol">:trailing_</span> | ||
<span class="hljs-symbol">:contains_underscore</span> | ||
<span class="hljs-symbol">:symbol_CAPS</span> | ||
<span class="hljs-symbol">:"string symbol"</span> | ||
<span class="hljs-symbol">:"interpolated <span class="hljs-subst">#{test}</span>"</span> | ||
<span class="hljs-symbol">:'string symbol'</span> | ||
<span class="hljs-symbol">:'not interpolated #{test}'</span> | ||
method <span class="hljs-symbol">:symbol</span> | ||
method(<span class="hljs-symbol">:symbol</span>) | ||
method(&<span class="hljs-symbol">:symbol</span>) | ||
assign=<span class="hljs-symbol">:symbol</span> | ||
assign = <span class="hljs-symbol">:symbol</span> | ||
<span class="hljs-symbol">:symbol</span>, others | ||
<span class="hljs-symbol">:</span>1notasymbol | ||
<span class="hljs-symbol">:</span><span class="hljs-string">%q[notasymbol]</span> | ||
|
||
::notsymbol | ||
|
||
<span class="hljs-symbol">hash_symbol:</span> value |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
:symbol | ||
:Symbol | ||
:_leading | ||
:trailing_ | ||
:contains_underscore | ||
:symbol_CAPS | ||
:"string symbol" | ||
:"interpolated #{test}" | ||
:'string symbol' | ||
:'not interpolated #{test}' | ||
method :symbol | ||
method(:symbol) | ||
method(&:symbol) | ||
assign=:symbol | ||
assign = :symbol | ||
:symbol, others | ||
:1notasymbol | ||
:%q[notasymbol] | ||
|
||
::notsymbol | ||
|
||
hash_symbol: value |
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.
Uh oh!
There was an error while loading. Please reload this page.