Skip to content

Commit 088cfc4

Browse files
committed
add elixirFunctionCall
Implementation from #506 cc: Thanks @w-sanches
1 parent 85afa5e commit 088cfc4

File tree

4 files changed

+119
-5
lines changed

4 files changed

+119
-5
lines changed

spec/syntax/alias_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
str = "Enum.empty?(...)"
88
expect(str).to include_elixir_syntax('elixirAlias', 'Enum')
99
expect(str).to include_elixir_syntax('elixirOperator', '\.')
10-
expect(str).to include_elixir_syntax('elixirId', 'empty?')
10+
expect(str).to include_elixir_syntax('elixirFunctionCall', 'empty?')
1111
end
1212

1313
it 'colorize the module alias even if it starts with `!`' do
@@ -43,6 +43,6 @@
4343
expect(str).to include_elixir_syntax('elixirAlias', '\.\(Baz\)\@=')
4444
expect(str).to include_elixir_syntax('elixirAlias', 'Baz')
4545
expect(str).to include_elixir_syntax('elixirOperator', '\.\(fun\)\@=')
46-
expect(str).to include_elixir_syntax('elixirId', 'fun')
46+
expect(str).to include_elixir_syntax('elixirFunctionCall', 'fun')
4747
end
4848
end

spec/syntax/function_spec.rb

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
describe 'function syntax' do
77
it 'doesnt treat underscored functions like unsued variables' do
8-
expect(<<~EOF).to include_elixir_syntax('elixirId', '__ensure_defimpl__')
8+
expect(<<~EOF).to include_elixir_syntax('elixirFunctionCall', '__ensure_defimpl__')
99
defp derive(protocol, for, struct, opts, env) do
1010
# ... code ...
1111
__ensure_defimpl__(protocol, for, env)
@@ -17,4 +17,114 @@
1717
__ensure_defimpl__(protocol, for, env)
1818
EOF
1919
end
20+
21+
it 'matches top-level macros as elixirKeyword' do
22+
expect(<<~EOF).to include_elixir_syntax('elixirKeyword', 'quote')
23+
quote do
24+
# ... code ...
25+
end
26+
EOF
27+
28+
expect(<<~EOF).to include_elixir_syntax('elixirKeyword', 'quote')
29+
quote(do: '')
30+
EOF
31+
end
32+
33+
it 'detects higher order function calls' do
34+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
35+
func.()
36+
EOF
37+
end
38+
39+
it 'detects function calls with parenthesis' do
40+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
41+
func()
42+
EOF
43+
end
44+
45+
it 'detects function calls with bangs' do
46+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func!')
47+
func!()
48+
EOF
49+
end
50+
51+
it 'detects function calls with question marks' do
52+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func?')
53+
func?()
54+
EOF
55+
end
56+
57+
it 'detects function calls appended by module with parenthesis' do
58+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
59+
Mod.func()
60+
EOF
61+
end
62+
63+
it 'detects function calls appended by atom with parenthesis' do
64+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
65+
:mod.func()
66+
EOF
67+
end
68+
69+
it 'detects function calls appended by module without parenthesis' do
70+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
71+
Mod.func
72+
EOF
73+
end
74+
75+
it 'detects function calls appended by atom without parenthesis' do
76+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
77+
:mod.func
78+
EOF
79+
end
80+
81+
it 'detects function calls without parenthesis that contain paramenters' do
82+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
83+
func 1
84+
EOF
85+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
86+
func [1]
87+
EOF
88+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
89+
func :atom
90+
EOF
91+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
92+
func "string"
93+
EOF
94+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
95+
func 'a'
96+
EOF
97+
end
98+
99+
it 'does not highlight function calls without parenthesis that does not contain paramenters' do
100+
expect(<<~'EOF').not_to include_elixir_syntax('elixirFunctionCall', 'func')
101+
func
102+
EOF
103+
end
104+
105+
it 'does not detect calls to function with invalid names' do
106+
expect(<<~'EOF').not_to include_elixir_syntax('elixirFunctionCall', '2fast2func')
107+
2fast2func()
108+
EOF
109+
end
110+
111+
it 'ignores spacing between module and function names' do
112+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
113+
Module . func
114+
EOF
115+
end
116+
117+
it 'detects piped functions with parenthesis' do
118+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
119+
one_func()
120+
|> func()
121+
EOF
122+
end
123+
124+
it 'detects piped functions without parenthesis' do
125+
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
126+
one_func()
127+
|> func
128+
EOF
129+
end
20130
end

spec/syntax/module_function_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
describe 'Module function syntax' do
66
it 'for used as module function' do
7-
expect(<<~EOF).to include_elixir_syntax('elixirId', 'for')
7+
expect(<<~EOF).to include_elixir_syntax('elixirFunctionCall', 'for')
88
OverridesDefault.for
99
EOF
1010
end
1111

1212
it 'case used as module function' do
13-
expect(<<~EOF).to include_elixir_syntax('elixirId', 'case')
13+
expect(<<~EOF).to include_elixir_syntax('elixirFunctionCall', 'case')
1414
OverridesDefault.case
1515
EOF
1616
end

syntax/elixir.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ syn keyword elixirTodo FIXME NOTE TODO OPTIMIZE XXX HACK contained
1818

1919
syn match elixirId '\<[_a-zA-Z]\w*[!?]\?\>' contains=elixirUnusedVariable
2020

21+
syn match elixirFunctionCall '\<[a-z_]\w*[!?]\?\(\s*\.\?\s*(\|\s\+[a-zA-Z0-9@:\'\"\[]\)\@='
22+
syn match elixirFunctionCall '\(:\w\+\s*\.\s*\|[A-Z]\w*\s*\.\s*\)\@<=[a-z_]\w*[!?]\?'
23+
syn match elixirFunctionCall '\(>\s+\)\<[a-z_]\w*[!?]\?'
24+
2125
syn match elixirKeyword '\(\.\)\@<!\<\(for\|case\|when\|with\|cond\|if\|unless\|try\|receive\|after\|raise\|rescue\|catch\|else\|quote\|unquote\|super\|unquote_splicing\)\>:\@!'
2226

2327
syn keyword elixirInclude import require alias use

0 commit comments

Comments
 (0)