Search code examples
regexsublimetexttextmateatom-editor

Grammar regex in Sublime/Textmate/Atom


In Puppet there are two forms of require:

Relationship

require foo

Metaparameter

require => 'foo'

I want to change the Texmate/Sublime Text/Atom grammar so it can highlight them correctly.

Right now the code looks like this (Atom package, but they all share the same Textmate style regex):

{
    'begin': '(?i)\\b(import|include|require)\\b\\s*'
    'beginCaptures':
      '1':
        'name': 'keyword.control.import.include.puppet'
    'end': '(?=\\s|$)'
    'name': 'meta.include.puppet'
}

However, this means that require => still gets highlighted:

Example incorrect highlighting

When the grammar is correct, require => should be the orange color.

How can I change the regex to accommodate this requirement? Does it require a separate rule?


Solution

  • You can add a negative lookahead:

    (?!.*=>)\b(import|include|require)\b\s*
    

    That will assert that the line doesn't contains => see Live Demo