3

I have the following code and I want to define a syntax highlight rule that will show all lines as commented out starting at the line with ## till the indentation ends just before the last print line.

The indentation works like that in python.

function test a b
    arg Int a b
    ## if a>b
        show "Bigger"
        another command
    print "More stuff"

I think I should use something like the following, but how do I define the end condition based on indentation ? The start condition should somehow remember its indentation and the end condition should use that information. But how ?

syntax region myComment start=/\v##/ skip=/\v\\./ end=???

The comment highligthing should also stop when encountering a blank line.

function test a b
    arg Int a b
    ## if a>b
       show "Bigger"

function more a b
    arg Int a b

1 Answer 1

1

You can try following:

syntax region Comment start=/^\z(\s*\)##/ end=/^\z1\ze\S/

See :h /\z(\).

Basically it saves whitespace indentation in \z1 and use it in end of region just before first non-whitespace. Also see :h /\ze.

enter image description here

For Edited part of question

Make it also end on empty line

syntax region Comment start=/^\z(\s*\)##/ end=/\(^\z1\ze\S\)\|\(^\s*$\)/

by adding empty line regex to the end.

enter image description here

Bear in mind that it probably wouldn't work if you mix tabs and spaces.

4
  • Thanks. It does solve my original question, but not my entire problem. When the commented block is followed by a blank line instead of another command, the highlighting breaks for the rest of the file. I will edit my question to show this case as well. Commented Jan 29, 2020 at 18:08
  • @PaulSchutte check updates
    – Maxim Kim
    Commented Jan 29, 2020 at 18:36
  • I'm sure it would work with tabs+space, because \s matches both. But in general best to avoid them, sure
    – D. Ben Knoble
    Commented Jan 29, 2020 at 21:34
  • But the length would be different for \z part
    – Maxim Kim
    Commented Jan 30, 2020 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.