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