UNPKG

2.24 kBJavaScriptView Raw
1Prism.languages.groovy = Prism.languages.extend('clike', {
2 'string': [
3 {
4 pattern: /("""|''')(?:[^\\]|\\[\s\S])*?\1|\$\/(?:\$\/\$|[\s\S])*?\/\$/,
5 greedy: true
6 },
7 {
8 // TODO: Slash strings (e.g. /foo/) can contain line breaks but this will cause a lot of trouble with
9 // simple division (see JS regex), so find a fix maybe?
10 pattern: /(["'/])(?:\\.|(?!\1)[^\\\r\n])*\1/,
11 greedy: true
12 }
13 ],
14 'keyword': /\b(?:as|def|in|abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|do|double|else|enum|extends|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|native|new|package|private|protected|public|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|trait|transient|try|void|volatile|while)\b/,
15 'number': /\b(?:0b[01_]+|0x[\da-f_]+(?:\.[\da-f_p\-]+)?|[\d_]+(?:\.[\d_]+)?(?:e[+-]?[\d]+)?)[glidf]?\b/i,
16 'operator': {
17 pattern: /(^|[^.])(?:~|==?~?|\?[.:]?|\*(?:[.=]|\*=?)?|\.[@&]|\.\.<|\.\.(?!\.)|-[-=>]?|\+[+=]?|!=?|<(?:<=?|=>?)?|>(?:>>?=?|=)?|&[&=]?|\|[|=]?|\/=?|\^=?|%=?)/,
18 lookbehind: true
19 },
20 'punctuation': /\.+|[{}[\];(),.:$]/
21});
22
23Prism.languages.insertBefore('groovy', 'string', {
24 'shebang': {
25 pattern: /#!.+/,
26 alias: 'comment'
27 }
28});
29
30Prism.languages.insertBefore('groovy', 'punctuation', {
31 'spock-block': /\b(?:setup|given|when|then|and|cleanup|expect|where):/
32});
33
34Prism.languages.insertBefore('groovy', 'function', {
35 'annotation': {
36 pattern: /(^|[^.])@\w+/,
37 lookbehind: true,
38 alias: 'punctuation'
39 }
40});
41
42// Handle string interpolation
43Prism.hooks.add('wrap', function(env) {
44 if (env.language === 'groovy' && env.type === 'string') {
45 var delimiter = env.content[0];
46
47 if (delimiter != "'") {
48 var pattern = /([^\\])(?:\$(?:\{.*?\}|[\w.]+))/;
49 if (delimiter === '$') {
50 pattern = /([^\$])(?:\$(?:\{.*?\}|[\w.]+))/;
51 }
52
53 // To prevent double HTML-encoding we have to decode env.content first
54 env.content = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
55
56 env.content = Prism.highlight(env.content, {
57 'expression': {
58 pattern: pattern,
59 lookbehind: true,
60 inside: Prism.languages.groovy
61 }
62 });
63
64 env.classes.push(delimiter === '/' ? 'regex' : 'gstring');
65 }
66 }
67});