Sublime Forum

Feature ideas for .sublime-syntax


Working recently with syntax files, a few times have I thought: “Gosh, having this would be so useful!”.

I am going to share these ideas, maybe a few will prove easy to implement. I think they may help make syntax files way more concise, and we have a new version of Sublime Text coming after all :slight_smile:

1. Local variables
Sometimes same definition, like a complicated match with multiple captures, has to be copied over and over again due to a minor change - for example keywords to be matched. Adding local variables to include/set/push could help to reuse the code:

  - match: '^\[Foo\]$'
    scope: meta.section.tag
    push: section
      - scope:
      - keywords: Bar|Baz

  - meta_scope: {{scope}}
  - match: '^(({{keywords}})|(\w*))\s*(=)'
      1: meta.directive
      2: keyword.other
      3: invalid.illegal
      4: keyword.operator.assignment

Maybe this could even be extended to allow anonymous contexts:

      - context:
        - include: ...
        - match: ...

  - include: {{context}}

2. Conditionals
Sometimes we may only want to match something in certain scopes, and it seems the current scope is always determined. It would be helpful to be able to check if selector matches:

- match: 'foo'
  if_scope: abc.def - xyz

3. Uncoditional push/pop/set
Sometimes we want to push or pop scope at given place unconditionally. Instead of commonly used:

- match: ''
  pop: true

Why not allowing just:

- pop: true

4. Referencing variables from other syntax
Just as it is with including contexts from other syntax files:

- include: scope:from.other.file#context

It could improve reusability to allow this also for variables:

- match: '(?=\b{{scope:from.other.file#variable}})'
  pop: true

Some other character could be used instead of # to make the difference apparent, for example @.

What do you think about these? Could they be implemented without much trouble?
Maybe there is some common way to do any of these already that I am just not aware of?