11function highlightDotty ( hljs ) {
22
33 // identifiers
4- const camelCaseId = / [ a - z ] [ $ \w ] * /
54 const capitalizedId = / \b [ A - Z ] [ $ \w ] * \b /
65 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 } +|\`.+?\`)` )
910
1011 // numbers
1112 const hexDigit = '[a-fA-F0-9]'
@@ -19,26 +20,18 @@ function highlightDotty(hljs) {
1920 // Regular Keywords
2021 // The "soft" keywords (e.g. 'using') are added later where necessary
2122 const alwaysKeywords = {
22- $pattern : / ( \w + | \? = > | \? { 1 , 3 } | = > > | = > | < : | > : | _ | < - | \. n n ) / ,
23+ $pattern : / ( \w + | \? = > | \? { 1 , 3 } | = > > | = > | < : | > : | _ | # | < - | \. n n ) / ,
2324 keyword :
2425 'abstract case catch class def do else enum export extends final finally for given ' +
2526 '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 =>> => ?=> <: >: _ ? <- # ' ,
2728 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'
2930 }
3031 const modifiers = 'abstract|final|implicit|override|private|protected|sealed'
3132
3233 // 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 ) ) ) /
4235
4336 // all the keywords + soft keywords, separated by spaces
4437 function withSoftKeywords ( kwd ) {
@@ -50,6 +43,43 @@ function highlightDotty(hljs) {
5043 }
5144 }
5245
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+
5383 const PROBABLY_TYPE = {
5484 className : 'type' ,
5585 begin : capitalizedId ,
@@ -62,6 +92,7 @@ function highlightDotty(hljs) {
6292 relevance : 0
6393 }
6494
95+ // type parameters within [square brackets]
6596 const TPARAMS = {
6697 begin : / \[ / , end : / \] / ,
6798 keywords : {
@@ -121,6 +152,7 @@ function highlightDotty(hljs) {
121152 ]
122153 }
123154
155+ // "string" or """string""", with or without interpolation
124156 const STRING = {
125157 className : 'string' ,
126158 variants : [
@@ -206,66 +238,78 @@ function highlightDotty(hljs) {
206238 begin : / - (? = \S ) / , end : / \s / ,
207239 } ,
208240 {
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+ ]
211250 }
212251 ]
213252 } )
214253
215254 // Methods
216255 const METHOD = {
217256 className : 'function' ,
218- begin : `((${ modifiers } |transparent|inline) +)*def` , end : / = | \n / ,
257+ begin : `((${ modifiers } |transparent|inline|infix ) +)*def` , end : / = \s | \n / ,
219258 excludeEnd : true ,
220259 relevance : 5 ,
221- keywords : withSoftKeywords ( 'inline transparent' ) ,
260+ keywords : withSoftKeywords ( 'inline infix transparent' ) ,
222261 contains : [
223262 hljs . C_LINE_COMMENT_MODE ,
224263 hljs . C_BLOCK_COMMENT_MODE ,
225- titleFor ( 'def' ) ,
226264 TPARAMS ,
227265 CTX_PARAMS ,
228266 PARAMS ,
229- PROBABLY_TYPE
267+ TYPED , // prevents the ":" (declared type) to become a title
268+ PROBABLY_TYPE ,
269+ TITLE
230270 ]
231271 }
232272
233273 // Variables & Constants
234274 const VAL = {
235- beginKeywords : 'val var' , end : / [ = : ; \n ] / ,
275+ beginKeywords : 'val var' , end : / [ = : ; \n / ] / ,
236276 excludeEnd : true ,
237277 contains : [
238278 hljs . C_LINE_COMMENT_MODE ,
239279 hljs . C_BLOCK_COMMENT_MODE ,
240- titleFor ( '(val|var)' )
280+ TITLE2
241281 ]
242282 }
243283
244284 // Type declarations
245285 const TYPEDEF = {
246286 className : 'typedef' ,
247- begin : `((${ modifiers } |opaque) +)*type` , end : / [ = ; \n ] / ,
287+ begin : `((${ modifiers } |opaque) +)*type` , end : / [ = ; \n ] | ? [ < > ] : / ,
248288 excludeEnd : true ,
249289 keywords : withSoftKeywords ( 'opaque' ) ,
250290 contains : [
251291 hljs . C_LINE_COMMENT_MODE ,
252292 hljs . C_BLOCK_COMMENT_MODE ,
253- titleFor ( 'type' ) ,
254- PROBABLY_TYPE
293+ PROBABLY_TYPE ,
294+ TITLE ,
255295 ]
256296 }
257297
258298 // Given instances
259299 const GIVEN = {
260- begin : / g i v e n / , end : / [ = ; \n ] / ,
300+ begin : / g i v e n / , end : / = | [ = ; \n ] / ,
261301 excludeEnd : true ,
262302 keywords : 'given using with' ,
263303 contains : [
264304 hljs . C_LINE_COMMENT_MODE ,
265305 hljs . C_BLOCK_COMMENT_MODE ,
266- titleFor ( 'given' ) ,
267306 PARAMS ,
268- PROBABLY_TYPE
307+ {
308+ begin : 'as' ,
309+ keywords : 'as'
310+ } ,
311+ PROBABLY_TYPE ,
312+ TITLE
269313 ]
270314 }
271315
@@ -318,20 +362,32 @@ function highlightDotty(hljs) {
318362 className : 'class' ,
319363 begin : `((${ modifiers } |open|case|transparent) +)*(class|trait|enum|object|package object)` , end : templateDeclEnd ,
320364 keywords : withSoftKeywords ( 'open transparent' ) ,
365+ excludeEnd : true ,
321366 contains : [
322367 hljs . C_LINE_COMMENT_MODE ,
323368 hljs . C_BLOCK_COMMENT_MODE ,
324- titleFor ( '(class|trait|object|enum)' ) ,
325369 TPARAMS ,
326370 CTX_PARAMS ,
327371 PARAMS ,
328372 EXTENDS_PARENT ,
329373 WITH_MIXIN ,
330374 DERIVES_TYPECLASS ,
375+ TITLE ,
331376 PROBABLY_TYPE
332377 ]
333378 }
334379
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+
335391 // Case in enum
336392 const ENUM_CASE = {
337393 begin : / c a s e (? ! .* = > ) / , end : / \n / ,
@@ -340,22 +396,18 @@ function highlightDotty(hljs) {
340396 contains : [
341397 hljs . C_LINE_COMMENT_MODE ,
342398 hljs . C_BLOCK_COMMENT_MODE ,
343- {
344- // case A, B, C
345- className : 'title' ,
346- begin : `(?<=(case|,) *)${ id . source } `
347- } ,
348399 PARAMS ,
349400 EXTENDS_PARENT ,
350401 WITH_MIXIN ,
351402 DERIVES_TYPECLASS ,
403+ TITLE ,
352404 PROBABLY_TYPE
353405 ]
354406 }
355407
356408 // Case in pattern matching
357409 const MATCH_CASE = {
358- begin : / c a s e / , end : / = > / ,
410+ begin : / c a s e / , end : / = > | \n / ,
359411 keywords : 'case' ,
360412 excludeEnd : true ,
361413 contains : [
@@ -393,6 +445,7 @@ function highlightDotty(hljs) {
393445 METHOD ,
394446 VAL ,
395447 TYPEDEF ,
448+ PACKAGE ,
396449 CLASS ,
397450 GIVEN ,
398451 EXTENSION ,
0 commit comments