Bast
Bast

Reputation: 13

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:

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?

Upvotes: 0

Views: 1304

Answers (2)

Bast
Bast

Reputation: 13

Thank you for your answer! It does work. Thank you for the tip.

The only drawback is that it's a bit slow when the file has to load complete lines.

I complete your code to fit my needs:

syn match spiceCKT /^\s*.SUBCKT/

syn match spiceCell /\v(^\s*.SUBCKT\s+)@<=\S*\s+/

syn match spicePins /\v(^\s*.SUBCKT\s+or_gate\s+)@<=(\s*\S*)*/

The lines with @<= are making the screen a bit laggy when I scroll down. I'm still trying to work on it.

Upvotes: 0

Kent
Kent

Reputation: 195059

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

Upvotes: 2

Related Questions