Skip to content

Commit e6c0d29

Browse files
authored
Elixir: Added missing keyword and other improvements (#2773)
1 parent 88fa72c commit e6c0d29

8 files changed

+124
-42
lines changed

components/prism-elixir.js

+17-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
Prism.languages.elixir = {
2-
'comment': /#.*/m,
2+
'doc': {
3+
pattern: /@(?:doc|moduledoc)\s+(?:("""|''')[\s\S]*?\1|("|')(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2)/,
4+
inside: {
5+
'attribute': /^@\w+/,
6+
'string': /['"][\s\S]+/
7+
}
8+
},
9+
'comment': {
10+
pattern: /#.*/m,
11+
greedy: true
12+
},
313
// ~r"""foo""" (multi-line), ~r'''foo''' (multi-line), ~r/foo/, ~r|foo|, ~r"foo", ~r'foo', ~r(foo), ~r[foo], ~r{foo}, ~r<foo>
414
'regex': {
515
pattern: /~[rR](?:("""|''')(?:\\[\s\S]|(?!\1)[^\\])+\1|([\/|"'])(?:\\.|(?!\2)[^\\\r\n])+\2|\((?:\\.|[^\\)\r\n])+\)|\[(?:\\.|[^\\\]\r\n])+\]|\{(?:\\.|[^\\}\r\n])+\}|<(?:\\.|[^\\>\r\n])+>)[uismxfr]*/,
@@ -36,14 +46,12 @@ Prism.languages.elixir = {
3646
lookbehind: true,
3747
alias: 'symbol'
3848
},
49+
'module': {
50+
pattern: /\b[A-Z]\w*\b/,
51+
alias: 'class-name'
52+
},
3953
// Look-ahead prevents bad highlighting of the :: operator
4054
'attr-name': /\w+\??:(?!:)/,
41-
'capture': {
42-
// Look-behind prevents bad highlighting of the && operator
43-
pattern: /(^|[^&])&(?:[^&\s\d()][^\s()]*|(?=\())/,
44-
lookbehind: true,
45-
alias: 'function'
46-
},
4755
'argument': {
4856
// Look-behind prevents bad highlighting of the && operator
4957
pattern: /(^|[^&])&\d+/,
@@ -54,8 +62,9 @@ Prism.languages.elixir = {
5462
pattern: /@\w+/,
5563
alias: 'variable'
5664
},
65+
'function': /\b[_a-zA-Z]\w*[?!]?(?:(?=\s*(?:\.\s*)?\()|(?=\/\d+))/,
5766
'number': /\b(?:0[box][a-f\d_]+|\d[\d_]*)(?:\.[\d_]+)?(?:e[+-]?[\d_]+)?\b/i,
58-
'keyword': /\b(?:after|alias|and|case|catch|cond|def(?:callback|exception|impl|module|p|protocol|struct|delegate)?|do|else|end|fn|for|if|import|not|or|require|rescue|try|unless|use|when)\b/,
67+
'keyword': /\b(?:after|alias|and|case|catch|cond|def(?:callback|exception|impl|module|p|protocol|struct|delegate)?|do|else|end|fn|for|if|import|not|or|raise|require|rescue|try|unless|use|when)\b/,
5968
'boolean': /\b(?:true|false|nil)\b/,
6069
'operator': [
6170
/\bin\b|&&?|\|[|>]?|\\\\|::|\.\.\.?|\+\+?|-[->]?|<[-=>]|>=|!==?|\B!|=(?:==?|[>~])?|[*\/^]/,
@@ -73,18 +82,6 @@ Prism.languages.elixir = {
7382
'punctuation': /<<|>>|[.,%\[\]{}()]/
7483
};
7584

76-
Prism.languages.insertBefore('elixir', 'keyword', {
77-
'module': {
78-
pattern: /\b(defmodule\s)[A-Z][\w.\\]+/,
79-
lookbehind: true,
80-
alias: 'class-name'
81-
},
82-
'function': {
83-
pattern: /\b(defp?\s)[\w.\\]+/,
84-
lookbehind: true
85-
}
86-
});
87-
8885
Prism.languages.elixir.string.forEach(function(o) {
8986
o.inside = {
9087
'interpolation': {

components/prism-elixir.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/elixir/attribute_feature.test

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ foobar
88

99
[
1010
["attribute", "@vsn"], ["number", "2"],
11-
["attribute", "@moduledoc"], ["string", [
12-
"\"\"\"\r\nfoobar\r\n\"\"\""
13-
]],
11+
["doc", [ ["attribute", "@moduledoc" ], [ "string", "\"\"\"\r\nfoobar\r\n\"\"\"" ] ] ],
1412
["attribute", "@tag"], ["atom", ":external"]
1513
]
1614

1715
----------------------------------------------------
1816

19-
Checks for module attributes.
17+
Checks for module attributes.
+35-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,56 @@
1+
&Math.zero?(0)
12
fun = &Math.zero?/1
23
(&is_function/1).(fun)
34
fun = &(&1 + 1)
45
&List.flatten(&1, &2)
56

7+
fun = &Math.zero?/invalid
8+
69
----------------------------------------------------
710

811
[
9-
"fun ", ["operator", "="],
10-
["capture", "&Math.zero?/1"],
12+
["operator", "&"],
13+
["module", "Math"],
14+
["punctuation", "."],
15+
["function", "zero?" ],
1116
["punctuation", "("],
12-
["capture", "&is_function/1"],
17+
["number", "0"],
18+
["punctuation", ")"],
19+
"\r\nfun ", ["operator", "="],
20+
["operator", "&"],
21+
["module", "Math"],
22+
["punctuation", "."],
23+
["function", "zero?" ],
24+
["operator", "/"],
25+
["number", "1"],
26+
["punctuation", "("],
27+
["operator", "&"],
28+
["function", "is_function"],
29+
["operator", "/"],
30+
["number", "1"],
1331
["punctuation", ")"],
1432
["punctuation", "."],
1533
["punctuation", "("], "fun", ["punctuation", ")"],
1634
"\r\nfun ", ["operator", "="],
17-
["capture", "&"],
35+
["operator", "&"],
1836
["punctuation", "("], ["argument", "&1"],
1937
["operator", "+"], ["number", "1"], ["punctuation", ")"],
20-
["capture", "&List.flatten"],
38+
["operator", "&"],
39+
["module", "List"],
40+
["punctuation", "."], ["function", "flatten"],
2141
["punctuation", "("], ["argument", "&1"],
2242
["punctuation", ","], ["argument", "&2"],
23-
["punctuation", ")"]
43+
["punctuation", ")"],
44+
"\r\n\r\nfun ",
45+
[ "operator", "=" ],
46+
[ "operator", "&" ],
47+
[ "module", "Math" ],
48+
[ "punctuation", "." ],
49+
"zero?",
50+
[ "operator", "/" ],
51+
"invalid"
2452
]
2553

2654
----------------------------------------------------
2755

28-
Checks for function capturing and arguments.
56+
Checks for function capturing and arguments.
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
@doc "single"
2+
@doc 'single'
3+
@doc """triple"""
4+
@doc '''triple'''
5+
@doc '''
6+
multiline
7+
'''
8+
@doc """
9+
multiline
10+
"""
11+
@doc since: "1.3.0"
12+
@doc deprecated: "phased out"
13+
14+
@moduledoc "single"
15+
@moduledoc 'single'
16+
@moduledoc """triple"""
17+
@moduledoc '''triple'''
18+
@moduledoc '''
19+
multiline
20+
'''
21+
@moduledoc """
22+
multiline
23+
"""
24+
@moduledoc since: "1.3.0"
25+
@moduledoc deprecated: "phased out"
26+
27+
----------------------------------------------------
28+
29+
[
30+
[ "doc", [ [ "attribute", "@doc" ], [ "string", "\"single\"" ] ] ],
31+
[ "doc", [ [ "attribute", "@doc" ], [ "string", "'single'" ] ] ],
32+
[ "doc", [ [ "attribute", "@doc" ], [ "string", "\"\"\"triple\"\"\"" ] ] ],
33+
[ "doc", [ [ "attribute", "@doc" ], [ "string", "'''triple'''" ] ] ],
34+
[ "doc", [ [ "attribute", "@doc" ], [ "string", "'''\nmultiline\n'''" ] ] ],
35+
[ "doc", [ [ "attribute", "@doc" ], [ "string", "\"\"\"\nmultiline\n\"\"\"" ] ] ],
36+
[ "attribute", "@doc" ],
37+
[ "attr-name", "since:" ],
38+
[ "string", [ "\"1.3.0\"" ] ],
39+
[ "attribute", "@doc" ],
40+
[ "attr-name", "deprecated:" ],
41+
[ "string", [ "\"phased out\"" ] ],
42+
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "\"single\"" ] ] ],
43+
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "'single'" ] ] ],
44+
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "\"\"\"triple\"\"\"" ] ] ],
45+
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "'''triple'''" ] ] ],
46+
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "'''\nmultiline\n'''" ] ] ],
47+
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "\"\"\"\nmultiline\n\"\"\"" ] ] ],
48+
[ "attribute", "@moduledoc" ],
49+
[ "attr-name", "since:" ],
50+
[ "string", [ "\"1.3.0\"" ] ],
51+
[ "attribute", "@moduledoc" ],
52+
[ "attr-name", "deprecated:" ],
53+
[ "string", [ "\"phased out\"" ] ]
54+
]
55+
56+
----------------------------------------------------

tests/languages/elixir/issue1392.test

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ String.upcase(@fixed)
33
----------------------------------------------------
44

55
[
6-
"String",
6+
["module", "String"],
77
["punctuation", "."],
8-
"upcase",
8+
["function", "upcase"],
99
["punctuation", "("],
1010
["attribute", "@fixed"],
1111
["punctuation", ")"]
1212
]
1313

1414
----------------------------------------------------
1515

16-
Ensure module attributes don't consume punctuation.
16+
Ensure module attributes don't consume punctuation.

tests/languages/elixir/issue775.test

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
----------------------------------------------------
66

77
[
8-
["attribute", "@doc"],
9-
["string", [
10-
"\"\"\"\r\n## Parameters\r\n\"\"\""
11-
]]
8+
[
9+
"doc",
10+
[
11+
[ "attribute", "@doc" ],
12+
[ "string", "\"\"\"\r\n## Parameters\r\n\"\"\"" ]
13+
]
14+
]
1215
]
1316

1417
----------------------------------------------------
1518

1619
Ensures that markdown headers are not highlighted as comments inside strings.
17-
See #775 for details.
20+
See #775 for details.

tests/languages/elixir/keyword_feature.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defdelegate
99
defstruct do else
1010
end fn for if
1111
import not or
12-
require rescue try
12+
raise require rescue try
1313
unless use when
1414

1515
----------------------------------------------------
@@ -26,7 +26,7 @@ unless use when
2626
["keyword", "defstruct"], ["keyword", "do"], ["keyword", "else"],
2727
["keyword", "end"], ["keyword", "fn"], ["keyword", "for"], ["keyword", "if"],
2828
["keyword", "import"], ["keyword", "not"], ["keyword", "or"],
29-
["keyword", "require"], ["keyword", "rescue"], ["keyword", "try"],
29+
["keyword", "raise"], ["keyword", "require"], ["keyword", "rescue"], ["keyword", "try"],
3030
["keyword", "unless"], ["keyword", "use"], ["keyword", "when"]
3131
]
3232

0 commit comments

Comments
 (0)