beginCaptures not being applied to grammar


#1

I am trying to make a grammar that highlights arrow functions as follows:

scopeName: 'source.js'
name: 'js-func-highlight'
fileTypes: [
  'js'
]
patterns: [
  {
    include: 'source.js'
  }
  {
    match: 'test'
    name: 'block'
  }
  {
    begin: '=> {'
    beginCaptures:
      0:
        name: 'block'
      1:
        name: 'block'
      2:
        name: 'block'
      3:
        name: 'block'
    end: '}'
    endCaptures:
      0:
        name: 'block'
      1:
        name: 'block'
      2:
        name: 'block'
    name: 'block'
    patterns: [
      {
        include: '$self'
      }
    ]
  }
]

When I remove include: 'source.js' it works perfectly. However, when I do not remove it, it does not highlight the arrow functions but does highlight the word test. I am sure I am doing something wrong, but I am very new to atom grammars so I am not sure what it is.

Thanks.


#2

Arrow functions should already be scopd by language-js. I can’t confirm right now, because I haven’t tested this, but I’m confident that wil be your problem.

Incidentally, the order of the patterns array matters. Rules will be tested from top to bottom, so that’s why anything in source.js will override the ones you put below it.


#3

Thank you for the help! Is there any way to get around that?


#4
scopeName: 'source.example'
injectionSelector: "L:source.js, L:meta.function"
patterns: [
  {
    match: 'test'
    name: 'block'
  }
  {
    begin: '=> {'
    beginCaptures:
      0: name: 'block'
      1: name: 'block'
      2: name: 'block'
      3: name: 'block'
    end: '}'
    endCaptures:
      0: name: 'block'
      1: name: 'block'
      2: name: 'block'
    name: 'block'
  }
  { include: '$self' }
]

This appears to do what you want. For more on what I did, you can read this post. For more on all the possible things you can do, there is my other post.

Whether or not what you’re doing is a good idea remains in question though :slight_smile:


#5

Thank you so much - this is super helpful. I agree, it is not necessarily a very good idea - I am doing it as a project for my teacher.