Sublime Forum

Custom syntax highlighting question

#1

I’m writing a custom syntax highlighting file (for LS-DYNA input files), and I’m having problems with a particular issue. Keywords are denoted by a star (*), but if the keyword *COMMENT is provided, it should comment out all the input until the next keyword. I’ve been unable to replicate this functionality exactly - see my example here.

The current code which produces the example above:

%YAML 1.2
---
name: LSDYNA
file_extensions:
  - k
scope: source.lsdyna

contexts:
  main:
    - include: comment
    - include: keyword

  comment:
    - match: \$
      scope: punctuation.definiton.comment.lsdyna
      push:
        - meta_scope: comment.line.lsdyna
        - match: \n
          pop: true

  key_comment_1:
    - meta_scope: comment.block.lsdyna
    - match: (\*COMMENT )
      set: key_comment_2

  key_comment_2:
    - meta_scope: comment.block.lsdyna
    - match: \n
      set: key_comment_3

  key_comment_3:
    - meta_content_scope: comment.block.lsdyna
    - match: ^\*
      pop: true

  keyword:
    - include: key_comment_1
    - match: ^\*[A-Za-z0-9_]*
      scope: keyword.control.lsdyna
      pop: true

I’ve also tried using lookaheads as follows:

  key_comment_1:
    - match: (\*COMMENT)
      scope: punctuation.definiton.comment.lsdyna
      push:
        - meta_scope: comment.block.lsdyna
        - match: \n(?=\*)
          pop: true

But that doesn’t work either, it just causes everything after *COMMENT (including other keywords) to be commented out.

0 Likes

#2

Just use ^(?=\*) instead of \n(?=\*) (or instead of ^\* in the first example). AFAIK the matches are only in a single line and \n is at the end of the line.

3 Likes

#3

Ah that makes sense - works perfectly now. Thanks!

0 Likes