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.
3 Answers
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
1 Comment
.vimrc but they didn't work there, so I moved them to: .vim/after/syntax/go.vim (go.vim because I am using the Go language if you are using some other language use the appropriate filename like c.vim or cpp.vim)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.
5 Comments
\<\w\+( in the past with some success. May become slow on big files, but does the trick.\<\w\+(?syn match myFunction "\<\w\+\ze(". @IngoKarkat could you please review this suggestion? ThxAs 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.