Skip to content

Commit

Permalink
Elixir: Added missing keyword and other improvements (#2773)
Browse files Browse the repository at this point in the history
  • Loading branch information
gordalina authored Mar 2, 2021
1 parent 88fa72c commit e6c0d29
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 42 deletions.
37 changes: 17 additions & 20 deletions components/prism-elixir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Prism.languages.elixir = {
'comment': /#.*/m,
'doc': {
pattern: /@(?:doc|moduledoc)\s+(?:("""|''')[\s\S]*?\1|("|')(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2)/,
inside: {
'attribute': /^@\w+/,
'string': /['"][\s\S]+/
}
},
'comment': {
pattern: /#.*/m,
greedy: true
},
// ~r"""foo""" (multi-line), ~r'''foo''' (multi-line), ~r/foo/, ~r|foo|, ~r"foo", ~r'foo', ~r(foo), ~r[foo], ~r{foo}, ~r<foo>
'regex': {
pattern: /~[rR](?:("""|''')(?:\\[\s\S]|(?!\1)[^\\])+\1|([\/|"'])(?:\\.|(?!\2)[^\\\r\n])+\2|\((?:\\.|[^\\)\r\n])+\)|\[(?:\\.|[^\\\]\r\n])+\]|\{(?:\\.|[^\\}\r\n])+\}|<(?:\\.|[^\\>\r\n])+>)[uismxfr]*/,
Expand Down Expand Up @@ -36,14 +46,12 @@ Prism.languages.elixir = {
lookbehind: true,
alias: 'symbol'
},
'module': {
pattern: /\b[A-Z]\w*\b/,
alias: 'class-name'
},
// Look-ahead prevents bad highlighting of the :: operator
'attr-name': /\w+\??:(?!:)/,
'capture': {
// Look-behind prevents bad highlighting of the && operator
pattern: /(^|[^&])&(?:[^&\s\d()][^\s()]*|(?=\())/,
lookbehind: true,
alias: 'function'
},
'argument': {
// Look-behind prevents bad highlighting of the && operator
pattern: /(^|[^&])&\d+/,
Expand All @@ -54,8 +62,9 @@ Prism.languages.elixir = {
pattern: /@\w+/,
alias: 'variable'
},
'function': /\b[_a-zA-Z]\w*[?!]?(?:(?=\s*(?:\.\s*)?\()|(?=\/\d+))/,
'number': /\b(?:0[box][a-f\d_]+|\d[\d_]*)(?:\.[\d_]+)?(?:e[+-]?[\d_]+)?\b/i,
'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/,
'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/,
'boolean': /\b(?:true|false|nil)\b/,
'operator': [
/\bin\b|&&?|\|[|>]?|\\\\|::|\.\.\.?|\+\+?|-[->]?|<[-=>]|>=|!==?|\B!|=(?:==?|[>~])?|[*\/^]/,
Expand All @@ -73,18 +82,6 @@ Prism.languages.elixir = {
'punctuation': /<<|>>|[.,%\[\]{}()]/
};

Prism.languages.insertBefore('elixir', 'keyword', {
'module': {
pattern: /\b(defmodule\s)[A-Z][\w.\\]+/,
lookbehind: true,
alias: 'class-name'
},
'function': {
pattern: /\b(defp?\s)[\w.\\]+/,
lookbehind: true
}
});

Prism.languages.elixir.string.forEach(function(o) {
o.inside = {
'interpolation': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-elixir.min.js

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

6 changes: 2 additions & 4 deletions tests/languages/elixir/attribute_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ foobar

[
["attribute", "@vsn"], ["number", "2"],
["attribute", "@moduledoc"], ["string", [
"\"\"\"\r\nfoobar\r\n\"\"\""
]],
["doc", [ ["attribute", "@moduledoc" ], [ "string", "\"\"\"\r\nfoobar\r\n\"\"\"" ] ] ],
["attribute", "@tag"], ["atom", ":external"]
]

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

Checks for module attributes.
Checks for module attributes.
42 changes: 35 additions & 7 deletions tests/languages/elixir/capture_feature.test
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
&Math.zero?(0)
fun = &Math.zero?/1
(&is_function/1).(fun)
fun = &(&1 + 1)
&List.flatten(&1, &2)

fun = &Math.zero?/invalid

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

[
"fun ", ["operator", "="],
["capture", "&Math.zero?/1"],
["operator", "&"],
["module", "Math"],
["punctuation", "."],
["function", "zero?" ],
["punctuation", "("],
["capture", "&is_function/1"],
["number", "0"],
["punctuation", ")"],
"\r\nfun ", ["operator", "="],
["operator", "&"],
["module", "Math"],
["punctuation", "."],
["function", "zero?" ],
["operator", "/"],
["number", "1"],
["punctuation", "("],
["operator", "&"],
["function", "is_function"],
["operator", "/"],
["number", "1"],
["punctuation", ")"],
["punctuation", "."],
["punctuation", "("], "fun", ["punctuation", ")"],
"\r\nfun ", ["operator", "="],
["capture", "&"],
["operator", "&"],
["punctuation", "("], ["argument", "&1"],
["operator", "+"], ["number", "1"], ["punctuation", ")"],
["capture", "&List.flatten"],
["operator", "&"],
["module", "List"],
["punctuation", "."], ["function", "flatten"],
["punctuation", "("], ["argument", "&1"],
["punctuation", ","], ["argument", "&2"],
["punctuation", ")"]
["punctuation", ")"],
"\r\n\r\nfun ",
[ "operator", "=" ],
[ "operator", "&" ],
[ "module", "Math" ],
[ "punctuation", "." ],
"zero?",
[ "operator", "/" ],
"invalid"
]

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

Checks for function capturing and arguments.
Checks for function capturing and arguments.
56 changes: 56 additions & 0 deletions tests/languages/elixir/doc_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@doc "single"
@doc 'single'
@doc """triple"""
@doc '''triple'''
@doc '''
multiline
'''
@doc """
multiline
"""
@doc since: "1.3.0"
@doc deprecated: "phased out"

@moduledoc "single"
@moduledoc 'single'
@moduledoc """triple"""
@moduledoc '''triple'''
@moduledoc '''
multiline
'''
@moduledoc """
multiline
"""
@moduledoc since: "1.3.0"
@moduledoc deprecated: "phased out"

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

[
[ "doc", [ [ "attribute", "@doc" ], [ "string", "\"single\"" ] ] ],
[ "doc", [ [ "attribute", "@doc" ], [ "string", "'single'" ] ] ],
[ "doc", [ [ "attribute", "@doc" ], [ "string", "\"\"\"triple\"\"\"" ] ] ],
[ "doc", [ [ "attribute", "@doc" ], [ "string", "'''triple'''" ] ] ],
[ "doc", [ [ "attribute", "@doc" ], [ "string", "'''\nmultiline\n'''" ] ] ],
[ "doc", [ [ "attribute", "@doc" ], [ "string", "\"\"\"\nmultiline\n\"\"\"" ] ] ],
[ "attribute", "@doc" ],
[ "attr-name", "since:" ],
[ "string", [ "\"1.3.0\"" ] ],
[ "attribute", "@doc" ],
[ "attr-name", "deprecated:" ],
[ "string", [ "\"phased out\"" ] ],
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "\"single\"" ] ] ],
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "'single'" ] ] ],
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "\"\"\"triple\"\"\"" ] ] ],
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "'''triple'''" ] ] ],
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "'''\nmultiline\n'''" ] ] ],
[ "doc", [ [ "attribute", "@moduledoc" ], [ "string", "\"\"\"\nmultiline\n\"\"\"" ] ] ],
[ "attribute", "@moduledoc" ],
[ "attr-name", "since:" ],
[ "string", [ "\"1.3.0\"" ] ],
[ "attribute", "@moduledoc" ],
[ "attr-name", "deprecated:" ],
[ "string", [ "\"phased out\"" ] ]
]

----------------------------------------------------
6 changes: 3 additions & 3 deletions tests/languages/elixir/issue1392.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ String.upcase(@fixed)
----------------------------------------------------

[
"String",
["module", "String"],
["punctuation", "."],
"upcase",
["function", "upcase"],
["punctuation", "("],
["attribute", "@fixed"],
["punctuation", ")"]
]

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

Ensure module attributes don't consume punctuation.
Ensure module attributes don't consume punctuation.
13 changes: 8 additions & 5 deletions tests/languages/elixir/issue775.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
----------------------------------------------------

[
["attribute", "@doc"],
["string", [
"\"\"\"\r\n## Parameters\r\n\"\"\""
]]
[
"doc",
[
[ "attribute", "@doc" ],
[ "string", "\"\"\"\r\n## Parameters\r\n\"\"\"" ]
]
]
]

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

Ensures that markdown headers are not highlighted as comments inside strings.
See #775 for details.
See #775 for details.
4 changes: 2 additions & 2 deletions tests/languages/elixir/keyword_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defdelegate
defstruct do else
end fn for if
import not or
require rescue try
raise require rescue try
unless use when

----------------------------------------------------
Expand All @@ -26,7 +26,7 @@ unless use when
["keyword", "defstruct"], ["keyword", "do"], ["keyword", "else"],
["keyword", "end"], ["keyword", "fn"], ["keyword", "for"], ["keyword", "if"],
["keyword", "import"], ["keyword", "not"], ["keyword", "or"],
["keyword", "require"], ["keyword", "rescue"], ["keyword", "try"],
["keyword", "raise"], ["keyword", "require"], ["keyword", "rescue"], ["keyword", "try"],
["keyword", "unless"], ["keyword", "use"], ["keyword", "when"]
]

Expand Down

0 comments on commit e6c0d29

Please sign in to comment.