Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3717f1d

Browse files
authoredFeb 4, 2021
Merge pull request #224 from chbk/separators
Fix missing scopes on separators and `self`, differentiate `->`
2 parents dd2de8e + 43d4aa0 commit 3717f1d

File tree

2 files changed

+267
-120
lines changed

2 files changed

+267
-120
lines changed
 

‎grammars/ruby.cson‎

Lines changed: 130 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -60,60 +60,76 @@
6060
'''
6161
'patterns': [
6262
{
63-
'captures':
64-
'1':
63+
'begin': '\\bclass\\b'
64+
'beginCaptures':
65+
'0':
6566
'name': 'keyword.control.class.ruby'
66-
'2':
67-
'name': 'entity.name.type.class.ruby'
68-
'4':
69-
'name': 'entity.other.inherited-class.ruby'
70-
'5':
71-
'name': 'punctuation.separator.inheritance.ruby'
72-
'6':
73-
'name': 'variable.other.object.ruby'
74-
'7':
75-
'name': 'punctuation.definition.variable.ruby'
76-
'match': '''(?x)
77-
^\\s*(class)\\s+
78-
(
79-
(
80-
[.a-zA-Z0-9_:]+
81-
(\\s*(<)\\s*[.a-zA-Z0-9_:]+)? # class A < B
82-
)
83-
|
84-
((<<)\\s*[.a-zA-Z0-9_:]+) # class << C
85-
)
86-
'''
67+
'end': '\\s*$|(?![\\s\\w.:<])'
8768
'name': 'meta.class.ruby'
69+
'patterns': [
70+
{
71+
'match': '[A-Z]\\w*'
72+
'name': 'entity.name.type.class.ruby'
73+
}
74+
{
75+
'include': '#separators'
76+
}
77+
{
78+
'begin': '(<<)\\s*'
79+
'beginCaptures':
80+
'1':
81+
'name': 'punctuation.definition.variable.ruby'
82+
'contentName': 'variable.other.object.ruby'
83+
'end': '(?=$)|(?![\\s\\w.:])'
84+
'patterns': [
85+
{
86+
'match': '[A-Z]\\w*'
87+
'name': 'entity.name.type.class.ruby'
88+
}
89+
{
90+
'include': '#separators'
91+
}
92+
]
93+
}
94+
{
95+
'begin': '(<)\\s*'
96+
'beginCaptures':
97+
'1':
98+
'name': 'punctuation.separator.inheritance.ruby'
99+
'contentName': 'entity.other.inherited-class.ruby'
100+
'end': '(?=$)|(?![\\s\\w.:])'
101+
'patterns': [
102+
{
103+
'match': '[A-Z]\\w*'
104+
'name': 'entity.name.type.class.ruby'
105+
}
106+
{
107+
'include': '#separators'
108+
}
109+
]
110+
}
111+
]
88112
}
89113
{
90-
'captures':
91-
'1':
114+
'begin': '\\bmodule\\b'
115+
'beginCaptures':
116+
'0':
92117
'name': 'keyword.control.module.ruby'
93-
'2':
94-
'name': 'entity.name.type.module.ruby'
95-
'3':
96-
'name': 'entity.other.inherited-class.module.first.ruby'
97-
'4':
98-
'name': 'punctuation.separator.inheritance.ruby'
99-
'5':
100-
'name': 'entity.other.inherited-class.module.second.ruby'
101-
'6':
102-
'name': 'punctuation.separator.inheritance.ruby'
103-
'7':
104-
'name': 'entity.other.inherited-class.module.third.ruby'
105-
'8':
106-
'name': 'punctuation.separator.inheritance.ruby'
107-
'match': '''(?x)
108-
^\\s*(module)\\s+
109-
(
110-
([A-Z]\\w*(::))?
111-
([A-Z]\\w*(::))?
112-
([A-Z]\\w*(::))*
113-
[A-Z]\\w*
114-
)
115-
'''
118+
'end': '\\s*$|(?![\\s\\w.:])'
116119
'name': 'meta.module.ruby'
120+
'patterns': [
121+
{
122+
'match': '[A-Z]\\w*(?=::)'
123+
'name': 'entity.other.inherited-class.module.ruby'
124+
}
125+
{
126+
'match': '[A-Z]\\w*'
127+
'name': 'entity.name.type.module.ruby'
128+
}
129+
{
130+
'include': '#separators'
131+
}
132+
]
117133
}
118134
{
119135
'comment': 'else if is a common mistake carried over from other languages. it works if you put in a second end, but it’s never what you want.'
@@ -247,7 +263,8 @@
247263
(?=def\\b) # optimization to help Oniguruma fail fast
248264
(?<=^|\\s)(def)\\s+
249265
(
250-
(?>[a-zA-Z_]\\w*(?>\\.|::))? # method prefix
266+
(?:(self)(\\.|::))?
267+
(?>[a-zA-Z_]\\w*(?>(\\.|::)))* # method prefix
251268
(?> # method name
252269
[a-zA-Z_]\\w*(?>[?!]|=(?!>))?
253270
|
@@ -262,6 +279,13 @@
262279
'2':
263280
'name': 'entity.name.function.ruby'
264281
'3':
282+
'name': 'variable.language.self.ruby'
283+
'4':
284+
'name': 'punctuation.separator.method.ruby'
285+
'5':
286+
'comment': 'Unfortunately, the repeated pattern (\\.|::)* only captures the last occurring separator'
287+
'name': 'punctuation.separator.method.ruby'
288+
'6':
265289
'name': 'punctuation.definition.parameters.ruby'
266290
'comment': 'The method pattern comes from the symbol pattern. See there for an explanation.'
267291
'end': '\\)'
@@ -291,14 +315,19 @@
291315
}
292316
]
293317
}
318+
{
319+
'match': ','
320+
'name': 'punctuation.separator.delimiter.ruby'
321+
}
294322
]
295323
}
296324
{
297325
'begin': '''(?x)
298326
(?=def\\b) # optimization to help Oniguruma fail fast
299327
(?<=^|\\s)(def)\\s+
300328
(
301-
(?>[a-zA-Z_]\\w*(?>\\.|::))? # method prefix
329+
(?:(self)(\\.|::))?
330+
(?>[a-zA-Z_]\\w*(?>(\\.|::)))* # method prefix
302331
(?> # method name
303332
[a-zA-Z_]\\w*(?>[?!]|=(?!>))?
304333
|
@@ -313,6 +342,13 @@
313342
'name': 'keyword.control.def.ruby'
314343
'2':
315344
'name': 'entity.name.function.ruby'
345+
'3':
346+
'name': 'variable.language.self.ruby'
347+
'4':
348+
'name': 'punctuation.separator.method.ruby'
349+
'5':
350+
'comment': 'Unfortunately, the repeated pattern (\\.|::)* only captures the last occurring separator'
351+
'name': 'punctuation.separator.method.ruby'
316352
'comment': 'same as the previous rule, but without parentheses around the arguments'
317353
'end': '$'
318354
'name': 'meta.function.method.with-arguments.ruby'
@@ -338,6 +374,10 @@
338374
}
339375
]
340376
}
377+
{
378+
'match': ','
379+
'name': 'punctuation.separator.delimiter.ruby'
380+
}
341381
]
342382
}
343383
{
@@ -346,14 +386,22 @@
346386
'name': 'keyword.control.def.ruby'
347387
'3':
348388
'name': 'entity.name.function.ruby'
389+
'4':
390+
'name': 'variable.language.self.ruby'
391+
'5':
392+
'name': 'punctuation.separator.method.ruby'
393+
'6':
394+
'comment': 'Unfortunately, the repeated pattern (\\.|::)* only captures the last occurring separator'
395+
'name': 'punctuation.separator.method.ruby'
349396
'comment': ' the optional name is just to catch the def also without a method-name'
350397
'match': '''(?x)
351398
(?=def\\b) # optimization to help Oniguruma fail fast
352399
(?<=^|\\s)(def)\\b
353400
(
354401
\\s+
355402
(
356-
(?>[a-zA-Z_]\\w*(?>\\.|::))? # method prefix
403+
(?:(self)(\\.|::))?
404+
(?>[a-zA-Z_]\\w*(?>(\\.|::)))* # method prefix
357405
(?> # method name
358406
[a-zA-Z_]\\w*(?>[?!]|=(?!>))?
359407
|
@@ -1878,12 +1926,11 @@
18781926
]
18791927
}
18801928
{
1881-
'match': '=>'
1882-
'name': 'punctuation.separator.key-value'
1929+
'include': '#separators'
18831930
}
18841931
{
18851932
'match': '->'
1886-
'name': 'support.function.kernel.ruby'
1933+
'name': 'support.function.kernel.arrow.ruby'
18871934
}
18881935
{
18891936
'match': '<<=|%=|&{1,2}=|\\*=|\\*\\*=|\\+=|-=|\\^=|\\|{1,2}=|<<'
@@ -1902,13 +1949,6 @@
19021949
'match': '(?<=^|[ \\t])!|&&|\\|\\||\\^'
19031950
'name': 'keyword.operator.logical.ruby'
19041951
}
1905-
{
1906-
'comment': 'Safe navigation operator'
1907-
'match': '(&\\.)\\s*(?![A-Z])'
1908-
'captures':
1909-
'1':
1910-
'name': 'punctuation.separator.method.ruby'
1911-
}
19121952
{
19131953
'match': '(%|&|\\*\\*|\\*|\\+|-|/)'
19141954
'name': 'keyword.operator.arithmetic.ruby'
@@ -1921,33 +1961,6 @@
19211961
'match': '\\||~|>>'
19221962
'name': 'keyword.operator.other.ruby'
19231963
}
1924-
{
1925-
'match': ';'
1926-
'name': 'punctuation.separator.statement.ruby'
1927-
}
1928-
{
1929-
'match': ','
1930-
'name': 'punctuation.separator.object.ruby'
1931-
}
1932-
{
1933-
'comment': 'Mark as namespace separator if double colons followed by capital letter'
1934-
'match': '(::)\\s*(?=[A-Z])'
1935-
'captures':
1936-
'1':
1937-
'name': 'punctuation.separator.namespace.ruby'
1938-
}
1939-
{
1940-
'comment': 'Mark as method separator if double colons not followed by capital letter'
1941-
'match': '(\\.|::)\\s*(?![A-Z])'
1942-
'captures':
1943-
'1':
1944-
'name': 'punctuation.separator.method.ruby'
1945-
}
1946-
{
1947-
'comment': 'Must come after method and constant separators to prefer double colons'
1948-
'match': ':'
1949-
'name': 'punctuation.separator.other.ruby'
1950-
}
19511964
{
19521965
'match': '{'
19531966
'name': 'punctuation.section.scope.begin.ruby'
@@ -2283,6 +2296,35 @@
22832296
'name': 'comment.line.number-sign.ruby'
22842297
}
22852298
]
2299+
'separators':
2300+
'patterns': [
2301+
{
2302+
'match': '=>'
2303+
'name': 'punctuation.separator.key-value.ruby'
2304+
}
2305+
{
2306+
'match': ';'
2307+
'name': 'punctuation.terminator.statement.ruby'
2308+
}
2309+
{
2310+
'match': ','
2311+
'name': 'punctuation.separator.delimiter.ruby'
2312+
}
2313+
{
2314+
'match': '(::)(?=\\s*[A-Z])'
2315+
'captures':
2316+
'1':
2317+
'name': 'punctuation.separator.namespace.ruby'
2318+
}
2319+
{
2320+
'match': '&?\\.|::'
2321+
'name': 'punctuation.separator.method.ruby'
2322+
}
2323+
{
2324+
'match': ':'
2325+
'name': 'punctuation.separator.other.ruby'
2326+
}
2327+
]
22862328
'yard':
22872329
'patterns': [
22882330
{

0 commit comments

Comments
 (0)
This repository has been archived.