Sublime Forum

[Solved] How to include one syntax after add my own scope?

#1

I want to define the # as commentary lines for octave, and after it just use the Sublime Text Default Matlab syntax.

I tried this, but does not work, the lines with # still ignored by my definition, and the Matlab syntax are used instead of mime.
%YAML 1.2

name: OCTAVE
file_extensions:
- octave
scope: source.octave
contexts:
main:
- match: (#).*$\n?
scope: comment.line.percentage.octave
pop: true
- match: .
push: Packages/Matlab/Matlab.sublime-syntax

The other solution which works but is ugly is to duplicate the Matlab syntax, and add the scope # as commentaries. But this is ugly. And I do not know if I can share my modifications to a Sublime Text Default Package as the Matlab syntax, just changing some lines after renaming everything to octave.

0 Likes

#2

As soon as Matlab syntax is pushed on the stack, it will never return to your main context. So your code might have worked for the comment at the beginning of the file but not for next lines. This might work instead:

  main:
    - match: ''
      push: Packages/Matlab/Matlab.sublime-syntax
      with_prototype:
      - match: #.*
        scope: comment.line.percentage.octave

And I don’t think this part is necessary: $\n?
Matches are always made against individual lines so just consuming all characters should match up to the end of the line. And newlines are probably stripped anyway.

1 Like

#3

Thank you, it is working. Note it just does not accept using # at the begging.
But using (#).* or ‘#.*’ is working fine.

I prefer keep the ‘\n’ to hope someday sublime will allow multi-line matches.
This is the new full code:
%YAML 1.2

name: OCTAVE
file_extensions:
- octave
scope: source.octave
contexts:
main:
- match: ‘’
push: Packages/Matlab/Matlab.sublime-syntax
with_prototype:
- match: ‘#.*\n’
scope: comment.line.percentage.octave
- match: (?<!.)\b(end(for|if|while|swith|function))\b
comment: Control keywords
scope: keyword.control.octave

0 Likes

#4

Even if it will, it would be a breaking change so would be an opt-in. So little pointless to handle those in current syntax IMO. But just a minor detail.

0 Likes

#5

the idea is to scope the trailing newline (if it is present) as a comment, so that autocompletion is not offered when typing inside a comment right before the end of the line

2 Likes

#6

I just found this out, I think it is easier to push another syntax using this:

name: OCTAVE
file_extensions:
  - octave
scope: source.octave
contexts:
  main:
    - match: #.*
      scope: comment.line.percentage.octave

    # Include/add the default Matlab Sublime Text syntax, on the top of this matching
    # my scopes defined above, before the default Matlab.sublime-syntax ones.
    - include: Packages/Matlab/Matlab.sublime-syntax

Update:

This solution is worse because it messed with the Octave Indentation Rules. So just use the original proposed by @rchl, just above.

0 Likes