xref: /expo/docs/components/base/languages/groovy.ts (revision b32521e7)
1export function installGroovy(Prism: any) {
2  Prism.languages.groovy = Prism.languages.extend('clike', {
3    string: [
4      {
5        pattern: /("""|''')(?:[^\\]|\\[\s\S])*?\1|\$\/(?:\$\/\$|[\s\S])*?\/\$/,
6        greedy: true,
7      },
8      {
9        // TODO: Slash strings (e.g. /foo/) can contain line breaks but this will cause a lot of trouble with
10        // simple division (see JS regex), so find a fix maybe?
11        pattern: /(["'/])(?:\\.|(?!\1)[^\\\r\n])*\1/,
12        greedy: true,
13      },
14    ],
15    keyword:
16      /\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/,
17    number:
18      /\b(?:0b[01_]+|0x[\da-f_]+(?:\.[\da-f_p-]+)?|[\d_]+(?:\.[\d_]+)?(?:e[+-]?[\d]+)?)[glidf]?\b/i,
19    operator: {
20      pattern:
21        /(^|[^.])(?:~|==?~?|\?[.:]?|\*(?:[.=]|\*=?)?|\.[@&]|\.\.<|\.\.(?!\.)|-[-=>]?|\+[+=]?|!=?|<(?:<=?|=>?)?|>(?:>>?=?|=)?|&[&=]?|\|[|=]?|\/=?|\^=?|%=?)/,
22      lookbehind: true,
23    },
24    punctuation: /\.+|[{}[\];(),.:$]/,
25  });
26
27  Prism.languages.insertBefore('groovy', 'string', {
28    shebang: {
29      pattern: /#!.+/,
30      alias: 'comment',
31    },
32  });
33
34  Prism.languages.insertBefore('groovy', 'punctuation', {
35    'spock-block': /\b(?:setup|given|when|then|and|cleanup|expect|where):/,
36  });
37
38  Prism.languages.insertBefore('groovy', 'function', {
39    annotation: {
40      pattern: /(^|[^.])@\w+/,
41      lookbehind: true,
42      alias: 'punctuation',
43    },
44  });
45
46  // Handle string interpolation
47  Prism.hooks.add('wrap', (env: any) => {
48    if (env.language === 'groovy' && env.type === 'string') {
49      const delimiter = env.content[0];
50
51      if (delimiter !== "'") {
52        const pattern =
53          delimiter === '$' ? /([^$])(?:\$(?:\{.*?\}|[\w.]+))/ : /([^\\])(?:\$(?:\{.*?\}|[\w.]+))/;
54
55        // To prevent double HTML-encoding we have to decode env.content first
56        env.content = env.content.replace(/&lt;/g, '<').replace(/&amp;/g, '&');
57
58        env.content = Prism.highlight(env.content, {
59          expression: {
60            pattern,
61            lookbehind: true,
62            inside: Prism.languages.groovy,
63          },
64        });
65
66        env.classes.push(delimiter === '/' ? 'regex' : 'gstring');
67      }
68    }
69  });
70}
71