Skip to content

Commit

Permalink
fix(elixir) Support function names/symbols with a slash (#2406)
Browse files Browse the repository at this point in the history
- Do not mistake code like `import Kernel, except: [ /: 2 ]` as the beginning 
  of a regex, which causes highlighting to break.
- `/:`, `+:`, are still not considered symbols

This solves the problem by making the pattern `/: \d` an exception case that is
never treated as a regex.  For most cases, most of the time this should
be a win.

Resolves #730.
  • Loading branch information
joshgoebel committed Feb 17, 2020
1 parent 4bc39be commit 3f2b813
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -16,6 +16,7 @@ Core Changes:

Language Improvements:

- fix(elixir) Support function names with a slash (#2406) [Josh Goebel][]
- fix(javascript) comma is allowed in a "value container" (#2403) [Josh Goebel][]
- enh(apache) add `deny` and `allow` keywords [Josh Goebel][]
- enh(apache) highlight numeric attributes values [Josh Goebel][]
Expand Down
21 changes: 15 additions & 6 deletions src/languages/elixir.js
Expand Up @@ -19,7 +19,11 @@ export default function(hljs) {
lexemes: ELIXIR_IDENT_RE,
keywords: ELIXIR_KEYWORDS
};

var NUMBER = {
className: 'number',
begin: '(\\b0o[0-7_]+)|(\\b0b[01_]+)|(\\b0x[0-9a-fA-F_]+)|(-?\\b[1-9][0-9_]*(.[0-9_]+([eE][-+]?[0-9]+)?)?)',
relevance: 0
};
var SIGIL_DELIMITERS = '[/|([{<"\']'
var LOWERCASE_SIGIL = {
className: 'string',
Expand Down Expand Up @@ -128,11 +132,7 @@ export default function(hljs) {
begin: ELIXIR_IDENT_RE + ':(?!:)',
relevance: 0
},
{
className: 'number',
begin: '(\\b0o[0-7_]+)|(\\b0b[01_]+)|(\\b0x[0-9a-fA-F_]+)|(-?\\b[1-9][0-9_]*(.[0-9_]+([eE][-+]?[0-9]+)?)?)',
relevance: 0
},
NUMBER,
{
className: 'variable',
begin: '(\\$\\W)|((\\$|\\@\\@?)(\\w+))'
Expand All @@ -144,6 +144,15 @@ export default function(hljs) {
begin: '(' + hljs.RE_STARTERS_RE + ')\\s*',
contains: [
hljs.HASH_COMMENT_MODE,
{
// to prevent false regex triggers for the division function:
// /:
begin: /\/: (?=\d+\s*[,\]])/,
relevance: 0,
contains: [
NUMBER
]
},
{
className: 'regexp',
illegal: '\\n',
Expand Down
6 changes: 6 additions & 0 deletions test/markup/elixir/function-not-regex.expect.txt
@@ -0,0 +1,6 @@
<span class="hljs-keyword">import</span> Kernel, <span class="hljs-symbol">except:</span> [
<span class="hljs-symbol">spawn:</span> <span class="hljs-number">1</span>,
+: <span class="hljs-number">2</span>,
/: <span class="hljs-number">2</span>,
<span class="hljs-symbol">Unless:</span> <span class="hljs-number">2</span>
]
6 changes: 6 additions & 0 deletions test/markup/elixir/function-not-regex.txt
@@ -0,0 +1,6 @@
import Kernel, except: [
spawn: 1,
+: 2,
/: 2,
Unless: 2
]

0 comments on commit 3f2b813

Please sign in to comment.