Sublime Forum

Can I include segments of a YAML file for syntax definitions?


I am creating a syntax definition file using YAML. There are several different contexts in which some things are colored differently or in which some things are/are not colored at all, but in most of them, many common matching rules apply (e.g., strings, comments, numbers, etc.). The problem is, every time I define a new context, I have to copy/paste the match: ... scope: ... logic several times to specify that these common things still apply.

So far I have been able to put the regular expressions for common things (strings, numbers, comments) in variables so I can at least single-source that part of it. But is there a way to just have one section of the file defined for the common match/scope rules and include that in other contexts to single-source the whole thing?



It sounds like you want to use the include directive; that will inline a copy of the rules from a provided context into the current one as if they were entered there directly. With that you could put your common matching rules in one context and then include it where needed:

%YAML 1.2
scope: source.example
    - match: 'first'
      scope: something.first

    - match: 'second'
      scope: something.second

    - include: common


Oh, that sounds perfect! Thanks so much, I will try that.



Good to know that exists, I was reading that there was no include type of facility in YAML but that may have been in a “pre-processor” sense.



YAML may or may not include an include type mechanism (I’m only familiar with it as it pertains to sublime-syntax files), but in this case the include is special to the sublime-syntax format.