1
1
function highlightDotty ( hljs ) {
2
2
3
3
// identifiers
4
- const camelCaseId = / [ a - z ] [ $ \w ] * /
5
4
const capitalizedId = / \b [ A - Z ] [ $ \w ] * \b /
6
5
const alphaId = / [ a - z A - Z $ _ ] [ $ \w ] * /
7
- const op = / [ ^ \s \w \d , " ' ( ) [ \] { } ] + /
8
- const id = new RegExp ( `(${ alphaId . source } ((?<=_)${ op . source } )?|${ op . source } |\`.*?\`)` )
6
+ const op1 = / [ ^ \s \w \d , ; " ' ( ) [ \] { } = : ] /
7
+ const op2 = / [ ^ \s \w \d , ; " ' ( ) [ \] { } ] /
8
+ const id = new RegExp ( `(${ alphaId . source } (_${ op2 . source } +)?|${ op2 . source } {2,}|${ op1 . source } +|\`.+?\`)` )
9
9
10
10
// numbers
11
11
const hexDigit = '[a-fA-F0-9]'
@@ -19,26 +19,18 @@ function highlightDotty(hljs) {
19
19
// Regular Keywords
20
20
// The "soft" keywords (e.g. 'using') are added later where necessary
21
21
const alwaysKeywords = {
22
- $pattern : / ( \w + | \? = > | \? { 1 , 3 } | = > > | = > | < : | > : | _ | < - | \. n n ) / ,
22
+ $pattern : / ( \w + | \? = > | \? { 1 , 3 } | = > > | = > | < : | > : | _ | # | < - | \. n n ) / ,
23
23
keyword :
24
24
'abstract case catch class def do else enum export extends final finally for given ' +
25
25
'if implicit import lazy match new object package private protected override return ' +
26
- 'sealed then throw trait true try type val var while with yield =>> => ?=> <: >: _ ? <-' ,
26
+ 'sealed then throw trait true try type val var while with yield =>> => ?=> <: >: _ ? <- # ' ,
27
27
literal : 'true false null this super' ,
28
- built_in : '??? asInstanceOf isInstanceOf assert implicitly locally summon .nn'
28
+ built_in : '??? asInstanceOf isInstanceOf assert implicitly locally summon valueOf .nn'
29
29
}
30
30
const modifiers = 'abstract|final|implicit|override|private|protected|sealed'
31
31
32
32
// End of class, enum, etc. header
33
- const templateDeclEnd = / ( \/ [ / * ] | { | : * \n | \n (? ! * ( e x t e n d s | w i t h | d e r i v e s ) ) ) /
34
-
35
- // name <title>
36
- function titleFor ( name ) {
37
- return {
38
- className : 'title' ,
39
- begin : `(?<=${ name } )${ id . source } `
40
- }
41
- }
33
+ const templateDeclEnd = / ( \/ [ / * ] | { | : (? = * \n ) | \n (? ! * ( e x t e n d s | w i t h | d e r i v e s ) ) ) /
42
34
43
35
// all the keywords + soft keywords, separated by spaces
44
36
function withSoftKeywords ( kwd ) {
@@ -50,6 +42,43 @@ function highlightDotty(hljs) {
50
42
}
51
43
}
52
44
45
+ // title inside of a complex token made of several parts (e.g. class)
46
+ const TITLE = {
47
+ className : 'title' ,
48
+ begin : id ,
49
+ returnEnd : true ,
50
+ keywords : alwaysKeywords . keyword ,
51
+ literal : alwaysKeywords . literal ,
52
+ built_in : alwaysKeywords . built_in
53
+ }
54
+
55
+ // title that goes to the end of a simple token (e.g. val)
56
+ const TITLE2 = {
57
+ className : 'title' ,
58
+ begin : id ,
59
+ excludeEnd : true ,
60
+ endsWithParent : true
61
+ }
62
+
63
+ const TYPED = {
64
+ begin : / : (? = [ a - z A - Z ( ) ? ] ) / ,
65
+ end : / \/ \/ | \/ \* | \n / ,
66
+ endsWithParent : true ,
67
+ returnEnd : true ,
68
+ contains : [
69
+ {
70
+ // works better than the usual way of defining keyword,
71
+ // in this specific situation
72
+ className : 'keyword' ,
73
+ begin : / \? \= > | = > > | [ = : ] [ > < ] | \? / ,
74
+ } ,
75
+ {
76
+ className : 'type' ,
77
+ begin : alphaId
78
+ }
79
+ ]
80
+ }
81
+
53
82
const PROBABLY_TYPE = {
54
83
className : 'type' ,
55
84
begin : capitalizedId ,
@@ -62,6 +91,7 @@ function highlightDotty(hljs) {
62
91
relevance : 0
63
92
}
64
93
94
+ // type parameters within [square brackets]
65
95
const TPARAMS = {
66
96
begin : / \[ / , end : / \] / ,
67
97
keywords : {
@@ -121,6 +151,7 @@ function highlightDotty(hljs) {
121
151
]
122
152
}
123
153
154
+ // "string" or """string""", with or without interpolation
124
155
const STRING = {
125
156
className : 'string' ,
126
157
variants : [
@@ -206,66 +237,78 @@ function highlightDotty(hljs) {
206
237
begin : / - (? = \S ) / , end : / \s / ,
207
238
} ,
208
239
{
209
- className : 'link' ,
210
- begin : / (?< = \[ .* ?\] ) \( / , end : / \) / ,
240
+ begin : / \[ .* ?\] \( / , end : / \) / ,
241
+ contains : [
242
+ {
243
+ // mark as "link" only the URL
244
+ className : 'link' ,
245
+ begin : / .* ?/ ,
246
+ endsWithParent : true
247
+ }
248
+ ]
211
249
}
212
250
]
213
251
} )
214
252
215
253
// Methods
216
254
const METHOD = {
217
255
className : 'function' ,
218
- begin : `((${ modifiers } |transparent|inline) +)*def` , end : / = | \n / ,
256
+ begin : `((${ modifiers } |transparent|inline|infix ) +)*def` , end : / = \s | \n / ,
219
257
excludeEnd : true ,
220
258
relevance : 5 ,
221
- keywords : withSoftKeywords ( 'inline transparent' ) ,
259
+ keywords : withSoftKeywords ( 'inline infix transparent' ) ,
222
260
contains : [
223
261
hljs . C_LINE_COMMENT_MODE ,
224
262
hljs . C_BLOCK_COMMENT_MODE ,
225
- titleFor ( 'def' ) ,
226
263
TPARAMS ,
227
264
CTX_PARAMS ,
228
265
PARAMS ,
229
- PROBABLY_TYPE
266
+ TYPED , // prevents the ":" (declared type) to become a title
267
+ PROBABLY_TYPE ,
268
+ TITLE
230
269
]
231
270
}
232
271
233
272
// Variables & Constants
234
273
const VAL = {
235
- beginKeywords : 'val var' , end : / [ = : ; \n ] / ,
274
+ beginKeywords : 'val var' , end : / [ = : ; \n / ] / ,
236
275
excludeEnd : true ,
237
276
contains : [
238
277
hljs . C_LINE_COMMENT_MODE ,
239
278
hljs . C_BLOCK_COMMENT_MODE ,
240
- titleFor ( '(val|var)' )
279
+ TITLE2
241
280
]
242
281
}
243
282
244
283
// Type declarations
245
284
const TYPEDEF = {
246
285
className : 'typedef' ,
247
- begin : `((${ modifiers } |opaque) +)*type` , end : / [ = ; \n ] / ,
286
+ begin : `((${ modifiers } |opaque) +)*type` , end : / [ = ; \n ] | ? [ < > ] : / ,
248
287
excludeEnd : true ,
249
288
keywords : withSoftKeywords ( 'opaque' ) ,
250
289
contains : [
251
290
hljs . C_LINE_COMMENT_MODE ,
252
291
hljs . C_BLOCK_COMMENT_MODE ,
253
- titleFor ( 'type' ) ,
254
- PROBABLY_TYPE
292
+ PROBABLY_TYPE ,
293
+ TITLE2 ,
255
294
]
256
295
}
257
296
258
297
// Given instances
259
298
const GIVEN = {
260
- begin : / g i v e n / , end : / [ = ; \n ] / ,
299
+ begin : / g i v e n / , end : / = | [ = ; \n ] / ,
261
300
excludeEnd : true ,
262
301
keywords : 'given using with' ,
263
302
contains : [
264
303
hljs . C_LINE_COMMENT_MODE ,
265
304
hljs . C_BLOCK_COMMENT_MODE ,
266
- titleFor ( 'given' ) ,
267
305
PARAMS ,
268
- PROBABLY_TYPE
306
+ {
307
+ begin : 'as' ,
308
+ keywords : 'as'
309
+ } ,
310
+ PROBABLY_TYPE ,
311
+ TITLE
269
312
]
270
313
}
271
314
@@ -318,20 +361,32 @@ function highlightDotty(hljs) {
318
361
className : 'class' ,
319
362
begin : `((${ modifiers } |open|case|transparent) +)*(class|trait|enum|object|package object)` , end : templateDeclEnd ,
320
363
keywords : withSoftKeywords ( 'open transparent' ) ,
364
+ excludeEnd : true ,
321
365
contains : [
322
366
hljs . C_LINE_COMMENT_MODE ,
323
367
hljs . C_BLOCK_COMMENT_MODE ,
324
- titleFor ( '(class|trait|object|enum)' ) ,
325
368
TPARAMS ,
326
369
CTX_PARAMS ,
327
370
PARAMS ,
328
371
EXTENDS_PARENT ,
329
372
WITH_MIXIN ,
330
373
DERIVES_TYPECLASS ,
374
+ TITLE ,
331
375
PROBABLY_TYPE
332
376
]
333
377
}
334
378
379
+ // package declaration with a content
380
+ const PACKAGE = {
381
+ className : 'package' ,
382
+ begin : / p a c k a g e (? = \w + * [: { \n] ) / , end : / [: { \n] / ,
383
+ excludeEnd : true ,
384
+ keywords : alwaysKeywords ,
385
+ contains : [
386
+ TITLE
387
+ ]
388
+ }
389
+
335
390
// Case in enum
336
391
const ENUM_CASE = {
337
392
begin : / c a s e (? ! .* = > ) / , end : / \n / ,
@@ -340,22 +395,18 @@ function highlightDotty(hljs) {
340
395
contains : [
341
396
hljs . C_LINE_COMMENT_MODE ,
342
397
hljs . C_BLOCK_COMMENT_MODE ,
343
- {
344
- // case A, B, C
345
- className : 'title' ,
346
- begin : `(?<=(case|,) *)${ id . source } `
347
- } ,
348
398
PARAMS ,
349
399
EXTENDS_PARENT ,
350
400
WITH_MIXIN ,
351
401
DERIVES_TYPECLASS ,
402
+ TITLE ,
352
403
PROBABLY_TYPE
353
404
]
354
405
}
355
406
356
407
// Case in pattern matching
357
408
const MATCH_CASE = {
358
- begin : / c a s e / , end : / = > / ,
409
+ begin : / c a s e / , end : / = > | \n / ,
359
410
keywords : 'case' ,
360
411
excludeEnd : true ,
361
412
contains : [
@@ -393,6 +444,7 @@ function highlightDotty(hljs) {
393
444
METHOD ,
394
445
VAL ,
395
446
TYPEDEF ,
447
+ PACKAGE ,
396
448
CLASS ,
397
449
GIVEN ,
398
450
EXTENSION ,
0 commit comments