5

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 3

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

I tried putting these in my .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)
2

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

I've used a \<\w\+( in the past with some success. May become slow on big files, but does the trick.
@Ingo-Karkat thanks, I downloaded the easytags plugin (along with vim-misc and exuberant ctags) and I can now jump to function definitions but unfortunately I don't think the syntax highlighting works on Javascript files, which is what I spend most of my time with
@Vitor Where do I put in \<\w\+(?
Try adding the following to your vimrc: syn match myFunction "\<\w\+\ze(". @IngoKarkat could you please review this suggestion? Thx
@Vitor: Yes, that would work (in most cases, unless obscured by another syntax group). It's different from my answer, though, because it'll highlight all functions, regardless of known or not.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.