Search code examples
vimvim-syntax-highlighting

Vim highlighting nested regions


I'm working with vim for several months now, and I still try to improve my experience with this great editor.

What I try to do is to create a syntax highlighting file for spice netlists (electronic stuff).

I'm trying to highlight the following pattern:

.SUBCKT or_gate A B OUT

(*or_gate* being the name of a cell
A, B and OUT bieng pins of this cell)

What I try to do is to highlight it with 3 different colors:

  • one for .SUBCKT
  • one for or_gate (the cell)
  • one for A, B and OUT (the pins)

So I'm looking at the lines starting with .SUBCKT, and I tried to find a way to match the different words of the line.

I tried a lot of different combination of syn match, or nested syn region but I just feel like I'm doing it the wrong way.

Here's an example of one of my attempts:

syn region spiceCKT start="\.SUBCKT" end="$" contains=spiceCell,spicePins

syn region spiceCell start="\.SUBCKT"rs=e end="$" contained

syn region spicePins start="\.SUBCKT\s\S*"rs=e end="$" contained

I tried to play with the pattern (\s\S*), to add/remove the rs=e parts, or even to define the start or end patterns adding \zs and \ze.

In the end I did not manage to make it work and I just feel like I'm making it way more complicated that it should be.

Anyone could help me figuring out what are my mistakes and how to handle such pattern highlighting?


Solution

  • You could try syn match with look-behind:

    syn match spiceCKT /^\s*\.SUBCKT/
    syn match spiceCell /\v(^\s*\.SUBCKT\s+)@<=or_gate/
    syn match spicePins /\v(^\s*\.SUBCKT\s+or_gate\s+)@<=A B OUT/
    

    I just did a small test. I don't have your syntax groups, I just tried with default groups, it looks like this:

    enter image description here