Skip to content

Commit

Permalink
Groovy: Minor improvements (#2036)
Browse files Browse the repository at this point in the history
This makes some minor improvements to the Groovy language definition.

- Triple quoted strings now support backslash escapes
- A single `.` will now be highlighted as `punctuation` instead of `operator` to be consistent with other languages.
  • Loading branch information
RunDevelopment authored Sep 2, 2019
1 parent 3fdb7d5 commit fb61833
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
16 changes: 9 additions & 7 deletions components/prism-groovy.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
Prism.languages.groovy = Prism.languages.extend('clike', {
'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/,
'string': [
{
pattern: /("""|''')[\s\S]*?\1|(?:\$\/)(?:\$\/\$|[\s\S])*?\/\$/,
pattern: /("""|''')(?:[^\\]|\\[\s\S])*?\1|\$\/(?:\$\/\$|[\s\S])*?\/\$/,
greedy: true
},
{
pattern: /(["'\/])(?:\\.|(?!\1)[^\\\r\n])*\1/,
// TODO: Slash strings (e.g. /foo/) can contain line breaks but this will cause a lot of trouble with
// simple division (see JS regex), so find a fix maybe?
pattern: /(["'/])(?:\\.|(?!\1)[^\\\r\n])*\1/,
greedy: true
}
],
'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/,
'number': /\b(?:0b[01_]+|0x[\da-f_]+(?:\.[\da-f_p\-]+)?|[\d_]+(?:\.[\d_]+)?(?:e[+-]?[\d]+)?)[glidf]?\b/i,
'operator': {
pattern: /(^|[^.])(?:~|==?~?|\?[.:]?|\*(?:[.=]|\*=?)?|\.[@&]|\.\.<|\.{1,2}(?!\.)|-[-=>]?|\+[+=]?|!=?|<(?:<=?|=>?)?|>(?:>>?=?|=)?|&[&=]?|\|[|=]?|\/=?|\^=?|%=?)/,
pattern: /(^|[^.])(?:~|==?~?|\?[.:]?|\*(?:[.=]|\*=?)?|\.[@&]|\.\.<|\.\.(?!\.)|-[-=>]?|\+[+=]?|!=?|<(?:<=?|=>?)?|>(?:>>?=?|=)?|&[&=]?|\|[|=]?|\/=?|\^=?|%=?)/,
lookbehind: true
},
'punctuation': /\.+|[{}[\];(),:$]/
'punctuation': /\.+|[{}[\];(),.:$]/
});

Prism.languages.insertBefore('groovy', 'string', {
Expand All @@ -31,9 +33,9 @@ Prism.languages.insertBefore('groovy', 'punctuation', {

Prism.languages.insertBefore('groovy', 'function', {
'annotation': {
alias: 'punctuation',
pattern: /(^|[^.])@\w+/,
lookbehind: true
lookbehind: true,
alias: 'punctuation'
}
});

Expand Down
2 changes: 1 addition & 1 deletion components/prism-groovy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/languages/groovy/operator_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
= == =~ ==~
? ?. ?:
* ** *. *= **=
. .@ .&
.@ .&
5..8
5..<8
- -- -= ->
Expand All @@ -24,7 +24,7 @@
["operator", "="], ["operator", "=="], ["operator", "=~"], ["operator", "==~"],
["operator", "?"], ["operator", "?."], ["operator", "?:"],
["operator", "*"], ["operator", "**"], ["operator", "*."], ["operator", "*="], ["operator", "**="],
["operator", "."], ["operator", ".@"], ["operator", ".&"],
["operator", ".@"], ["operator", ".&"],
["number", "5"], ["operator", ".."], ["number", "8"],
["number", "5"], ["operator", "..<"], ["number", "8"],
["operator", "-"], ["operator", "--"], ["operator", "-="], ["operator", "->"],
Expand All @@ -42,4 +42,4 @@

----------------------------------------------------

Checks for all operators.
Checks for all operators.
8 changes: 6 additions & 2 deletions tests/languages/groovy/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ bar/$
'''foo
/* comment */
bar'''
'''hell\'''o'''
"""foo
// comment
bar"""
"""hell\"""o"""

----------------------------------------------------

Expand All @@ -51,10 +53,12 @@ bar"""
["string", "\"foo /* comment */ bar\""],
["string", "'foo // bar'"],
["string", "'''foo\r\n/* comment */\r\nbar'''"],
["string", "\"\"\"foo\r\n// comment\r\nbar\"\"\""]
["string", "'''hell\\'''o'''"],
["string", "\"\"\"foo\r\n// comment\r\nbar\"\"\""],
["string", "\"\"\"hell\\\"\"\"o\"\"\""]
]

----------------------------------------------------

Checks for single quoted, triple single quoted, double quoted,
triple double quoted, slashy and dollar slashy strings.
triple double quoted, slashy and dollar slashy strings.

0 comments on commit fb61833

Please sign in to comment.