I am trying to come up with a Vim syntax for python function call, e.g.:
foo(notarg==0, arg2, kwarg='a', kwarg2='b')
dict(a=1, b=2, c=3)
where we want to highlight
- (i) function name
foo
,dict
- (ii) keyword parameters, e.g.
kwarg
,kwarg2
I know there is a great answer on a similar question. To repeat:
syn region FCall matchgroup=FName start='[[:alpha:]_]\i*\s*(' end=')' contains=FCall,FCallKeyword
syn match FCallKeyword /\i*\ze\s*=[^=]/ contained
Or, alternatively, in my own vim syntax file for python, I could manage to achieve a similar behavior:
syn match pythonCall /\<\h\i*\ze\s*(/ contains=pythonBuiltin,pythonBuiltinFunc,pythonBuiltinType
\ nextgroup=pythonCallRegion skipwhite keepend
syn region pythonCallRegion contained matchgroup=pythonParamsDelim start=/(/ end=/)/ keepend extend
\ contains=pythonCallComma,pythonCall,@pythonCallArgument,pythonCallArgKeyword
syn match pythonCallArgKeyword contained /\h\i*\ze\s*==\@!/
However, the problem is that when a previously existing syntax group (e.g. pythonBuiltinType
) matches function name, the entire highlight for keyword arguments would not work:
For example:
dict(a=1, b=2)
where we have the following highlighting rule somewhere (e.g. python-mode)
syn keyword pythonBuiltinType dict ....
Given this, dict
matches pythonBuiltinType
, but not matches pythonCall
. This happens for the both ways I just described. A screenshot:
What did I wrong here? Vim syntax is quite difficult, so your help would be greatly appreciated.
syn list
andsyn clear
trick, in order to make itcontained
. This is pretty smart.contained
, which may give a cleaner solution. Maybe I should try updating all primitive syntax rules. Do you think, however, is there any way to make it work without touching them but playing with additory syntax (e.g.pythonCall
)?:h :syn-keyword
), and because the keywords inpythonBuiltinType
are not nested: they match at the top level. If they were contained, you could probably override what you want.:h text-prop-intro
:Instead of defining patterns to match the text, the highlighting is set by a script, possibly using the output of an external parser.
You need a recent version of Vim to read this help file, and I don't think it's ready to replace syntax highlighting yet, because it's very recent and still under development. Maybe keep an eye on it, as it could help fix your issue later.