Skip to content

Add syntax highlight to function calls #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion spec/syntax/function_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe 'function syntax' do
it 'doesnt treat underscored functions like unsued variables' do
expect(<<~EOF).to include_elixir_syntax('elixirId', '__ensure_defimpl__')
expect(<<~EOF).to include_elixir_syntax('elixirFunctionCall', '__ensure_defimpl__')
defp derive(protocol, for, struct, opts, env) do
# ... code ...
__ensure_defimpl__(protocol, for, env)
Expand All @@ -17,4 +17,102 @@
__ensure_defimpl__(protocol, for, env)
EOF
end

it 'detects higher order function calls' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
func.()
EOF
end

it 'detects function calls with parenthesis' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
func()
EOF
end

it 'detects function calls with bangs' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func!')
func!()
EOF
end

it 'detects function calls with question marks' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func?')
func?()
EOF
end

it 'detects function calls appended by module with parenthesis' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
Mod.func()
EOF
end

it 'detects function calls appended by atom with parenthesis' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
:mod.func()
EOF
end

it 'detects function calls appended by module without parenthesis' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
Mod.func
EOF
end

it 'detects function calls appended by atom without parenthesis' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
:mod.func
EOF
end

it 'detects function calls without parenthesis that contain paramenters' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
func 1
EOF
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
func [1]
EOF
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
func :atom
EOF
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
func "string"
EOF
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
func 'a'
EOF
end

it 'does not highlight function calls without parenthesis that does not contain paramenters' do
expect(<<~'EOF').not_to include_elixir_syntax('elixirFunctionCall', 'func')
func
Copy link
Collaborator

@jbodah jbodah Aug 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd and I don't know what the solution here is. This causes only some function calls to be highlighted by default:

image

The first two hd instances come back as elixirId rather than elixirFunctionCall

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to confess I completely forgot we could use atoms as module names. Just pushed a commit to fix that.

Also, while at it I realised |> func should also be highlighted since if we are piping it is not ambiguous that we are calling a function.

EOF
end

it 'does not detect calls to function with invalid names' do
expect(<<~'EOF').not_to include_elixir_syntax('elixirFunctionCall', '2fast2func')
2fast2func()
EOF
end

it 'ignores spacing between module and function names' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
Module . func
EOF
end

it 'detects piped functions with parenthesis' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
one_func()
|> func()
EOF
end

it 'detects piped functions without parenthesis' do
expect(<<~'EOF').to include_elixir_syntax('elixirFunctionCall', 'func')
one_func()
|> func
EOF
end
end
5 changes: 5 additions & 0 deletions syntax/elixir.vim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ syn keyword elixirBoolean true false nil
syn match elixirVariable '@[a-z]\w*'
syn match elixirVariable '&\d\+'

syn match elixirFunctionCall '\<[a-z_]\w*[!?]\?\(\s*\.\?\s*(\|\s\+[a-zA-Z0-9@:\'\"\[]\)\@='
syn match elixirFunctionCall '\(:\w\+\s*\.\s*\|[A-Z]\w*\s*\.\s*\)\@<=[a-z_]\w*[!?]\?'
syn match elixirFunctionCall '\(>\s+\)\<[a-z_]\w*[!?]\?'

syn keyword elixirPseudoVariable __FILE__ __DIR__ __MODULE__ __ENV__ __CALLER__

syn match elixirNumber '\<-\?\d\(_\?\d\)*\(\.[^[:space:][:digit:]]\@!\(_\?\d\)*\)\?\([eE][-+]\?\d\(_\?\d\)*\)\?\>'
Expand Down Expand Up @@ -197,6 +201,7 @@ hi def link elixirStructDefine Define
hi def link elixirExUnitMacro Define
hi def link elixirModuleDeclaration Type
hi def link elixirPrivateFunctionDeclaration elixirFunctionDeclaration
hi def link elixirFunctionCall Function
hi def link elixirFunctionDeclaration Function
hi def link elixirPrivateMacroDeclaration elixirMacroDeclaration
hi def link elixirMacroDeclaration Macro
Expand Down