andyroo
andyroo

Reputation: 157

How to color function call in Vim syntax highlighting

I'm new to Vim and curious how to highlight a function call after the function has been defined. As an example, in the SublimeText version, totalForArray is green when it is defined, as well as when it is called on line 12. This is what my Vim looks like imgur.com/q2WMQ4d, and I'm wondering how to make totalForArray highlighted when it's called.

Upvotes: 5

Views: 5973

Answers (3)

user2745184
user2745184

Reputation:

An improvement on Vitor's regex matching. This will highlight nested function calls while respecting highlighting for keywords like while, if, for, etc... and also allows for whitespace between function name and parenthesis e.g. myFunction (int argc) { ... }

syn match dFunction "\zs\(\k\w*\)*\s*\ze("
hi link dFunction Function

Upvotes: 5

Vitor
Vitor

Reputation: 1976

As a simpler alternative to what was proposed by @Ingo, you can also define a syntax to match any keyword that is directly followed by a parentheses:

syn match jsFunction "\<\k\+\ze("
hi link jsFunction Function

Searching in github I was also able to find the vim-javascript plugin, which appears to have various extensions to the default Javascript syntax included in Vim. In particular, it contains the following syntax definition:

syntax match jsFuncCall /\k\+\%(\s*(\)\@=/

This will implement the same syntax highlight I described before, but by using this plugin you might also benefit from other improvements that are included in it.

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172510

Vim's syntax parsing usually only colors the function definition, because that one is easy to locate with a regular expression. For function calls, it would have to maintain a list of detected functions.

There are plugins that extend syntax highlighting with such a list, usually taken from the tags database. For example, the easytags.vim plugin performs automatic tags updates and can highlight those via the :HighlightTags command.

Upvotes: 2

Related Questions