Sublime Forum

Detect end of syntaxt context

#1

Hello,

I am defining a syntax for a text file where a line is the child of the previous line with less indentation, like this:

  • a
    • b, child of a (because a has less indentation)
      • c, child of b (because b has less indentation)
      • d, child of b (not child of c, because c does not has less indentation)
    • e, child of a (because a is the previous line with less indentation)

I would like to color the line “todo” and its children, meaning the following lines with a greater indentation.
In the example below, we would color the lines “todo” and “child of todo”

  • hello
    • todo
      • child of todo
        • child of todo
      • child of todo
    • NOT child of todo

It is the first time I make a syntax definition and I thought I should use the push/pop context, so I made this test below for the todo line that begin with 3 spaces (I am sorry, I added - characters everywhere, because the preview was removing all my indentation! And the preview removes the 3 spaces in match: ‘^ - todo’, between ^ and todo)

  • contexts:
    • main:
      • match: ‘^ - todo’
        • scope: todo
        • push: todo
    • todo:
      • meta_scope: todo
      • match: ^ {0,3}[^ ]
        • pop: true

But I would like it to work with any number of spaces.
For example, if the todo line begin with 9 spaces, the todo context would end as soon as there is a line that begin with less than 9 spaces.
The number of spaces could be a parameter used in the match that pops the todo context.
I guess that languages like Python solved the same problem, to detect the end of a class or a function.
Do you have any idea to do it?
I used a push/pop context, but I would be happy to use any other solution!
Thank you!

PS: Besides, with my solution, in the example, the begin of line “NOT child of todo” is in the scope, and it should not.

0 Likes

#2

in your match pattern which pushes the context onto the stack, you can capture the indentation in a capture group and refer to it from the pop pattern
https://www.sublimetext.com/docs/syntax.html#php-heredocs

0 Likes

#3

Thank you @kingkeith!
I read the doc you sent me.
I understood I can assign different scopes to the different groups in parenthesis.
So now I have this:

  • contexts:
    • main:
      • match: ‘^( *)(- todo)’
        • captures:
          • 1: todo_scope
          • 2: todo_scope
        • push: todo_context
    • todo_context:
      • meta_scope: todo_scope
      • match: ^ {0,3}[^ ]
        • pop: true

But I did not find how to use these captured groups in the pushed context, in the " {0,3}" of the pop of todo_context.
Could you give me a little more information?

0 Likes