Galnagdeno
Galnagdeno

Reputation: 11

Color highlight function calls in VIM

Does anyone know a way to color highlight function calls in Vim?

I know that some plugins could do something like that by keeping record of tags, but with what I've found online, I could not figure out how to make it work.

I've tried using easy tags (which, by the way, doesn't seem to be maintained anymore) and gutentags, but to be quite honest, I haven't come much close to make any of them to work.

On the other hand, I imagine that it would be quite simple to implement a script to highlight anything that lies between a dot and a left parentheses or a blank space and a left parentheses (as in .anyCodeAtAll(), anotherCode()), but I have no idea how to do it. It would be a incomplete solution of course, but it would be good enough for my purposes at the moment.

Does anyone know how to make that work?

Upvotes: 1

Views: 1685

Answers (2)

Owain Evans
Owain Evans

Reputation: 384

It might be that you have not installed your plug-in correctly.
Try following these steps (or retrace your steps) and see if it works / missed any steps out:

cd ~ Go to home directory.
vim .vimrc open .vimrc
Insert:

call plug#begin()  
Plug 'xolox/vim-easytags'  
call plug#end()

easytags#Options states easytags should work out of the box, but you could add option here in the
.vimrc file now or later,
e.g. put:
let g:easytags_syntax_keyword = 'always'
afer the call plug block. Anyway.
:wq write quit the .vimrc

source ~/.vimrc unsure if required as will do later
vim test.js open vim with test.whatever language you know.
:PlugInstall in vim
Here might take a bit of time.
Then :q out of that window.
:source ~/.vimrc this is needed

Then test out to see if you have syntax highlighting. I'm pretty sure this is along the right lines. Might be misspelled plugin name.

Upvotes: 0

padawin
padawin

Reputation: 4476

I have something like that in my configuration, but it's quite language specific. For example for Golang, I have a ~/.vim/after/go.vim which contains:

syntax match goCustomParen     "(" contains=cParen
syntax match goCustomFuncDef   "func\s\+\w\+\s*(" contains=goDeclaration,goCustomParen
" Exclude import as function name, for multi-line imports
syntax match goCustomFunc      "import\s\+(\|\(\w\+\s*\)(" contains=goCustomParen,goImport
syntax match goCustomScope     "\."
syntax match goCustomAttribute "\.\w\+" contains=goCustomScope
syntax match goCustomMethod    "\.\w\+\s*(" contains=goCustomScope,goCustomParen

highlight def link goCustomMethod Function
highlight def link goCustomAttribute Identifier

highlight goCustomFuncDef ctermfg=13
highlight goCustomFunc ctermfg=43
highlight goCustomAttribute ctermfg=247
highlight goCustomMethod ctermfg=33

And for Python, I have a ~/.vim/after/python.vim:

syntax match pyCustomParen     "(" contains=cParen
syntax match pyCustomFunc      "\w\+\s*(" contains=pyCustomParen
syntax match pyCustomScope     "\."
syntax match pyCustomAttribute "\.\w\+" contains=pyCustomScope
syntax match pyCustomMethod    "\.\w\+\s*(" contains=pyCustomScope,pyCustomParen

highlight def link pyCustomFunc  Function
highlight def link pyCustomMethod Function
highlight def link pyCustomAttribute Identifier

highlight pyCustomFunc ctermfg=43
highlight pyCustomAttribute ctermfg=247
highlight pyCustomMethod ctermfg=33

In each case, the first block defines what is a function, a method, an attribute, and so on, the second block link these custom definition to the generic classes "Function, Identifier..." and the 3rd block defines the colors.

The files need to be in the after directory to be executed after the colorscheme and highlights definitions.

Here's a side by side comparison of with and without these settings (look on the last 3 lines):

enter image description here

Unless someone has a better solution, you could adapt the above for the language you need it.

Upvotes: 1

Related Questions