Kiteration
Kiteration

Reputation: 171

Vim Syntax: Matching a regex in a region to after AND before a keyword

I am looking for a way to code a .vim syntax script to match variable names inside a region in a flexible way.

The syntax of the portion of the language I am trying to highlight is like so:

Function(arg1, arg2, ... argn);

where args are of the form infoo=outfoo.

Whitespace is ignored in this language. The code I have so far indicates a region parens

syn region parens start='(' end=')'

Is there a way to match regex expressions using the syn match 'regex' command to before and after the = character within the parens region only?

The problem is more complicated than simply matching all words in the parens region because I need to highlight infoo and outfoo, except when one of them is already something I have implemented syntax highlighting for. Said another way, infoo and outfoo must be different colors depending on whether they are declared as variable names or are hardcoded into the language.

I have followed the Vim wiki guide for most of my efforts, and I am finding it difficult to understand the Vim documentation. This is my first attempt at writing or editing Vim syntax highlighting

Upvotes: 0

Views: 1553

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172510

You're looking for syntax containment; via contains={group},..., you tell Vim to parse the arguments within the region (else, the region match would prevent other syntax matches in there). Then, you'll define syntax matches for the arguments with the contained parameter, so that Vim will only attempt to match those inside (the region).

Here's an example that parses the in and out parts separately; you can adapt that, and even define another, nested containment, too:

syn region parens start='(' end=')' contains=inArgument,outArgument
syn match inArgument "\w\+=\@=" contained
syn match outArgument "=\@<=\w\+" contained

Note: The syntax names should better adhere to the {languageName}{SyntaxElementName} style seen in other syntax scripts.

Upvotes: 3

Related Questions