@@ -10,15 +10,71 @@ Category: common
1010export default function ( hljs ) {
1111 const regex = hljs . regex ;
1212 const RUBY_METHOD_RE = '([a-zA-Z_]\\w*[!?=]?|[-+~]@|<<|>>|=~|===?|<=>|[<>]=?|\\*\\*|[-/+%^&*~`|]|\\[\\]=?)' ;
13+ // TODO: move concepts like CAMEL_CASE into `modes.js`
14+ const CLASS_NAME_RE = regex . either (
15+ / \b ( [ A - Z ] + [ a - z 0 - 9 ] + ) + / ,
16+ // ends in caps
17+ / \b ( [ A - Z ] + [ a - z 0 - 9 ] + ) + [ A - Z ] + / ,
18+ )
19+ ;
20+ const CLASS_NAME_WITH_NAMESPACE_RE = regex . concat ( CLASS_NAME_RE , / ( : : \w + ) * / )
1321 const RUBY_KEYWORDS = {
14- keyword :
15- 'and then defined module in return redo if BEGIN retry end for self when '
16- + 'next until do begin unless END rescue else break undef not super class case '
17- + 'require yield alias while ensure elsif or include attr_reader attr_writer attr_accessor '
18- + '__FILE__' ,
19- built_in : 'proc lambda' ,
20- literal :
21- 'true false nil'
22+ "variable.constant" : [
23+ "__FILE__" ,
24+ "__LINE__"
25+ ] ,
26+ "variable.language" : [
27+ "self" ,
28+ "super" ,
29+ ] ,
30+ keyword : [
31+ "alias" ,
32+ "and" ,
33+ "attr_accessor" ,
34+ "attr_reader" ,
35+ "attr_writer" ,
36+ "begin" ,
37+ "BEGIN" ,
38+ "break" ,
39+ "case" ,
40+ "class" ,
41+ "defined" ,
42+ "do" ,
43+ "else" ,
44+ "elsif" ,
45+ "end" ,
46+ "END" ,
47+ "ensure" ,
48+ "for" ,
49+ "if" ,
50+ "in" ,
51+ "include" ,
52+ "module" ,
53+ "next" ,
54+ "not" ,
55+ "or" ,
56+ "redo" ,
57+ "require" ,
58+ "rescue" ,
59+ "retry" ,
60+ "return" ,
61+ "then" ,
62+ "undef" ,
63+ "unless" ,
64+ "until" ,
65+ "when" ,
66+ "while" ,
67+ "yield" ,
68+ ] ,
69+ built_in : [
70+ "proc" ,
71+ "lambda"
72+ ] ,
73+ literal : [
74+ "true" ,
75+ "false" ,
76+ "nil"
77+ ]
2278 } ;
2379 const YARDOCTAG = {
2480 className : 'doctag' ,
@@ -42,7 +98,7 @@ export default function(hljs) {
4298 relevance : 10
4399 }
44100 ) ,
45- hljs . COMMENT ( '^__END__' , '\\n$' )
101+ hljs . COMMENT ( '^__END__' , hljs . MATCH_NOTHING_RE )
46102 ] ;
47103 const SUBST = {
48104 className : 'subst' ,
@@ -156,49 +212,82 @@ export default function(hljs) {
156212 } ;
157213
158214 const PARAMS = {
159- className : 'params' ,
160- begin : '\\(' ,
161- end : '\\)' ,
162- endsParent : true ,
215+ variants : [
216+ {
217+ match : / \( \) / ,
218+ } ,
219+ {
220+ className : 'params' ,
221+ begin : / \( / ,
222+ end : / (? = \) ) / ,
223+ excludeBegin : true ,
224+ endsParent : true ,
225+ keywords : RUBY_KEYWORDS ,
226+ }
227+ ]
228+ } ;
229+
230+ const CLASS_DEFINITION = {
231+ variants : [
232+ {
233+ match : [
234+ / c l a s s \s + / ,
235+ CLASS_NAME_WITH_NAMESPACE_RE ,
236+ / \s + < \s + / ,
237+ CLASS_NAME_WITH_NAMESPACE_RE
238+ ]
239+ } ,
240+ {
241+ match : [
242+ / c l a s s \s + / ,
243+ CLASS_NAME_WITH_NAMESPACE_RE
244+ ]
245+ }
246+ ] ,
247+ scope : {
248+ 2 : "title.class" ,
249+ 4 : "title.class.inherited"
250+ } ,
163251 keywords : RUBY_KEYWORDS
164252 } ;
165253
254+ const UPPER_CASE_CONSTANT = {
255+ relevance : 0 ,
256+ match : / \b [ A - Z ] [ A - Z _ 0 - 9 ] + \b / ,
257+ className : "variable.constant"
258+ } ;
259+
260+ const METHOD_DEFINITION = {
261+ match : [
262+ / d e f / , / \s + / ,
263+ RUBY_METHOD_RE
264+ ] ,
265+ scope : {
266+ 1 : "keyword" ,
267+ 3 : "title.function"
268+ } ,
269+ contains : [
270+ PARAMS
271+ ]
272+ } ;
273+
274+ const OBJECT_CREATION = {
275+ relevance : 0 ,
276+ match : [
277+ CLASS_NAME_WITH_NAMESPACE_RE ,
278+ / \. n e w [ ( ] /
279+ ] ,
280+ scope : {
281+ 1 : "title.class"
282+ }
283+ } ;
284+
166285 const RUBY_DEFAULT_CONTAINS = [
167286 STRING ,
168- {
169- className : 'class' ,
170- beginKeywords : 'class module' ,
171- end : '$|;' ,
172- illegal : / = / ,
173- contains : [
174- hljs . inherit ( hljs . TITLE_MODE , { begin : '[A-Za-z_]\\w*(::\\w+)*(\\?|!)?' } ) ,
175- {
176- begin : '<\\s*' ,
177- contains : [
178- {
179- begin : '(' + hljs . IDENT_RE + '::)?' + hljs . IDENT_RE ,
180- // we already get points for <, we don't need poitns
181- // for the name also
182- relevance : 0
183- }
184- ]
185- }
186- ] . concat ( COMMENT_MODES )
187- } ,
188- {
189- className : 'function' ,
190- // def method_name(
191- // def method_name;
192- // def method_name (end of line)
193- begin : regex . concat ( / d e f \s + / , regex . lookahead ( RUBY_METHOD_RE + "\\s*(\\(|;|$)" ) ) ,
194- relevance : 0 , // relevance comes from kewords
195- keywords : "def" ,
196- end : '$|;' ,
197- contains : [
198- hljs . inherit ( hljs . TITLE_MODE , { begin : RUBY_METHOD_RE } ) ,
199- PARAMS
200- ] . concat ( COMMENT_MODES )
201- } ,
287+ CLASS_DEFINITION ,
288+ OBJECT_CREATION ,
289+ UPPER_CASE_CONSTANT ,
290+ METHOD_DEFINITION ,
202291 {
203292 // swallow namespace qualifiers before symbols
204293 begin : hljs . IDENT_RE + '::' } ,
@@ -227,6 +316,8 @@ export default function(hljs) {
227316 className : 'params' ,
228317 begin : / \| / ,
229318 end : / \| / ,
319+ excludeBegin : true ,
320+ excludeEnd : true ,
230321 relevance : 0 , // this could be a lot of things (in other languages) other than params
231322 keywords : RUBY_KEYWORDS
232323 } ,
0 commit comments