0

I am trying to write custom syntax highlighting for a domain specific markup language for a simulator I am working on. I can get everything but one part working. A simple example would look something like this:

(ObjectType
    stringAttribute: "String value"
    numberAttribute: 42
    listAttribute: {
        one: 1
        two: "two"
    }
    objectAttribute: 
        (AnotherObjectType
            //More attributes here
        )
)

where the ObjectType tells the simulation what class to load, and the attributes are set as properties of that object. The files can have deeply nested objects and lists, so I have them set up as fold regions as follows:

syn region object  start="(" end=")" fold transparent
syn region arglist start="{" end="}" fold transparent

However, I would like to highlight the object types, but haven't found a way to do so. I have gotten it to work in the with

syn match objectType display "(\s*\zs[A-Za-z0-9~!@#$%^&*\-_+<>?/]\+" contained

and adding contains=objectType to the end of the object rule. However, this breaks my other highlighting on things nested further down (e.g. strings, numbers, etc.). How would I get both the object type to be highlighted without messing everything else up?

1

1 Answer 1

0

That works for me:

syn region object  start="(" end=")" fold
syn region arglist start="{" end="}" fold

syn match objectType display "(\s*\zs[A-Za-z0-9~!@#$%^&*\-_+<>?/]\+" containedin=object
syn match string display +".\{-}\%([^\\]\%(\\\\\)*\\\)\@<!"+ containedin=object

It seems like the problem was the transparent, from the documentation:

If the "transparent" argument is given, this item will not be highlighted
itself, but will take the highlighting of the item it is contained in.  This
is useful for syntax items that don't need any highlighting but are used
only to skip over a part of the text.

The "contains=" argument is also inherited from the item it is contained in,
unless a "contains" argument is given for the transparent item itself.  To
avoid that unwanted items are contained, use "contains=NONE".  Example, which
highlights words in strings, but makes an exception for "vim": >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.