Skip to content

Commit

Permalink
Elixir: Added defdelagate keyword and highlighting for function/mod…
Browse files Browse the repository at this point in the history
…ule names (#2709)
  • Loading branch information
matthewlehner authored Jan 10, 2021
1 parent 7296270 commit 59f725d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
14 changes: 13 additions & 1 deletion components/prism-elixir.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Prism.languages.elixir = {
alias: 'variable'
},
'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)?|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|require|rescue|try|unless|use|when)\b/,
'boolean': /\b(?:true|false|nil)\b/,
'operator': [
/\bin\b|&&?|\|[|>]?|\\\\|::|\.\.\.?|\+\+?|-[->]?|<[-=>]|>=|!==?|\B!|=(?:==?|[>~])?|[*\/^]/,
Expand All @@ -73,6 +73,18 @@ 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.

10 changes: 7 additions & 3 deletions tests/languages/elixir/keyword_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ catch cond def
defcallback
defexception
defimpl defmodule
defp defprotocol
defp
defprotocol
defdelegate
defstruct do else
end fn for if
import not or
Expand All @@ -18,7 +20,9 @@ unless use when
["keyword", "defcallback"],
["keyword", "defexception"],
["keyword", "defimpl"], ["keyword", "defmodule"],
["keyword", "defp"], ["keyword", "defprotocol"],
["keyword", "defp"],
["keyword", "defprotocol"],
["keyword", "defdelegate"],
["keyword", "defstruct"], ["keyword", "do"], ["keyword", "else"],
["keyword", "end"], ["keyword", "fn"], ["keyword", "for"], ["keyword", "if"],
["keyword", "import"], ["keyword", "not"], ["keyword", "or"],
Expand All @@ -28,4 +32,4 @@ unless use when

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

Checks for all keywords.
Checks for all keywords.
29 changes: 29 additions & 0 deletions tests/languages/elixir/module_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Math do
def sum(a, b) do
a + b
end
end

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

[
["keyword", "defmodule"], ["module", "Math"], ["keyword", "do"],

["keyword", "def"],
["function", "sum"],
["punctuation", "("],
"a",
["punctuation", ","],
" b",
["punctuation", ")"],
["keyword", "do"],

"\n a ", ["operator", "+"], " b\n ",

["keyword", "end"],
["keyword", "end"]
]

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

Checks for all keywords.

0 comments on commit 59f725d

Please sign in to comment.