I'm trying to get the arguments (crudely I just want to highlight anything in the parenthesis) for python functions to highlight. I have a working regex that highlights the right things:
syn match pythonArguments "\vdef.*\(\zs.+\ze\)"
hi def link pythonArguments Special
Now, I got a similar thing to work in the same document (function calls) with this:
syn match pythonFunctionCall "\v\w+\ze\("
hi def link pythonFunctionCall Function
But, for some reason the arguments regex won't turn into a syntax group. And I tried running them as a command rather than in my ~/.vim/after/syntax/python.vim document to no avail. When my cursor is over expressions which are highlighted by the search /\vdef.*\(\zs.+\ze\)
, there is no syntax group. I've tried everything I can think of.
So, for anyone interested, it seems that vim (8.1 on MacOS) has buggy syntax groups. I ended up getting it to work with:
syn match pythonFunctionDeclaration "\v\(.+\):" contains=ALL transparent
syn match pythonArguments "\v\(\zs.+\ze\)" contained contains=ALL
I don't know why the other things that I tried didn't work, but this will highlight python function arguments (and it will leave alone already existing syntax groups).
EDIT: I learned more. So, the problem with regex for syntax groups is that overlap between different patterns is ignored. For example:
/\vdef|def something
will match the 'def' part of 'def something' because vim will skip over the 'something' part because it already matched with the first pattern. I don't know if that made any sense, checkout this.
Back to making the highlighting better, the problem with the syntax groups above is that some if statements and for loops will also match. However, this work:
syn clear pythonFunction
syn match pythonFunction "\h\w*"
nextgroup=pythonFunctionDeclaration display contained
syn match pythonFunctionDeclaration "\v\(.+\):" contains=pythonArguments transparent contained
syn match pythonArguments "\v\(\zs.+\ze\)" contained contains=ALLBUT,pythonFunction
hi def link pythonArguments Special
This will make our special syntax group only happen after functions.
TLDR; add the second (the longer) block of code to you ~/.vim/after/syntax/python.vim file to make python function arguments highlighted the same as the Special syntax group is highlighted.