Sublime Text syntax highlighting best practice

73 views Asked by At

I'm creating a .sublime-syntax file and have encountered an issue. I'm sure this is a common scenario with syntax files and there must be an elegant solution, but it's hard to follow existing definitions as they are often very complex.

To illustrate the issue:

// The below values are highlighted correctly
"string";   // source.uc string.quoted.double.uc
123456789;  // source.uc constant.numeric.uc

function myFunction() {  // source.uc meta.function.definition.uc
                         // source.uc meta.function.definition.uc meta.block.uc
    // Within "meta.block.uc", the below values are no longer highlighted:
    "string";
    123456789;
};

Screenshot of code in-situ:

Sublime Text screenshot

The "block" part of my syntax file looks like:

function-block:
  - match: \{
    scope: punctuation.section.block.begin.uc
    push:
      - meta_scope: meta.block.uc
      - match: \}
        scope: punctuation.section.block.end.uc
        pop: 1

Adding - include: strings beneath the meta_scope will ensure the string is highlighted correctly but I get the feeling there must be a better way of handling this rather than re-including contexts any time there's a meta scope on the stack.

Is there a way of avoiding re-including everything, or a better way entirely of structuring the syntax definition?

For reference, the full syntax definition for this example:

%YAML 1.2
---
name: UnrealScript

file_extensions:
  - uc

scope: source.uc

variables:
  valid_variable_name: '[a-zA-Z_][a-zA-Z_0-9]{0,}'
  valid_variable_type: '{{valid_variable_name}}'

contexts:
  main:
    - include: strings
    - include: numbers

    # Function declarations
    - match: '(?i)\bfunction(?=\s+)'
      scope: keyword.declaration.function.uc
      push: function

  function:
    - meta_scope: meta.function.definition.uc
    - include: function-return-type
    - include: function-name
    - include: function-block
    - match: ;
      scope: punctuation.terminator.uc
      pop: 1

  function-return-type:
    - match: '{{valid_variable_type}}(?=\s+{{valid_variable_name}})'
      scope: storage.type.uc

  function-name:
    - match: '{{valid_variable_name}}'
      scope: entity.name.function.uc

  function-block:
    - match: \{
      scope: punctuation.section.block.begin.uc
      push:
        - meta_scope: meta.block.uc
        - include: strings
        - match: \}
          scope: punctuation.section.block.end.uc
          pop: 1

  strings:
    - match: '"'
      scope: punctuation.definition.string.begin.uc
      push: double-quoted-string

  double-quoted-string:
    - meta_include_prototype: false
    - meta_scope: string.quoted.double.uc
    - match: '\\.'
      scope: constant.character.escape.uc
    - match: '"'
      scope: punctuation.definition.string.end.uc
      pop: 1

  numbers:
    - include: numbers-decimal
    - include: numbers-hex

  numbers-decimal:
    - match: '(?i)\b[0-9]+(\.[0-9]+)?f?\b'
      scope: constant.numeric.uc

  numbers-hex:
    - match: '(?i)\b0x[0-9a-f]+'
      scope: constant.numeric.hex.uc
0

There are 0 answers