Sublime Forum

Indent-block syntax highlighting

#1

I’m attempting to write a sublime-syntax description for a language which uses indent-based blocks (like python or coffeescript). One feature of this language is a comment-block. Here is the logic:

  • It starts with a double hash (##)
  • If the first non-blank line after that is indented more than the line the ## was on, that line defines the indent-level for the rest of the block.
  • Every following, non-blank line that is indented >= the defined indent level is part of the block
  • If a line is found with less indentation, the comment block ends (that line is not in the comment)

The regex below is very close to capturing the logic above. The only thing it misses is the “indented MORE than the line the ## started on.” I’d be happy if everything in the file that matched this regex was highlighted as a comment. It isn’t quite right, but it works for all “real-world” cases. The regex:

/##(?: *\n)+( *)([^ \n][^\n]*)(\n( *\n)*\1[^\n]*)*/

Last, here is an example of what should and should-not be highlighted:

while true
  something ##

      in comment

        in comment

      in comment

    not.in.comment

  not.in.comment

not.in.comment

while true
  something ##
  not.in.comment

while true
  something ##
not.in.comment

I’ve been banging my head against this for a couple of hours now. Thanks in advance!

0 Likes

How to define complex syntax definitions?
#2

Regexes are matched one line at a time.

So you’re going to need to push a scope that pops out when the lines doesn’t starts with ‘\1’
Good luck :wink:

0 Likes

#3

I was trying that, but I wasn’t able to get it to work. Is there any doc on what \1 refers to if it isn’t referring to a pattern in the current regex? Clearly it refers to the result of some previous regex, but which one?

0 Likes

#4

in a match that pops, the \1 will refer to the first capture group from the match that pushed

check the PHP Heredocs section of https://www.sublimetext.com/docs/3/syntax.html

1 Like

#5

Also see the YAML.sublime-syntax, where I do exactly this. Note that I knowingly forwent matching only lines that are indented more than the line the comment block was started on, because it would make things increasingly more difficult due to the other syntax that can be matched on that line.

2 Likes