Sublime Forum

tmLanguage regex failing

#1

This has been driving me crazy. I have been playing around with trying to adopt goto functionality for Sylus learnboost.github.com/stylus/

Given this sample code:

[code]header
padding 12px 0px
background #eee
border-bottom 1px solid #ccc
linearGradient(#f8f8f8, #e6e6e6)

nav
    font-size 15px
    width 400px
    position absolute
    max-height 40px
    right 0px
    bottom 5px
    text-align right

nav .portrait
        position relative
        top 9px
        margin-right 5px
        left 0px

        img
            border 1px solid #ccc
            width 30px
            height 30px
            transition border-color 0.25s

            &:hover
                border-color #676767[/code]

I am trying to match the selector groups (header, nav, nav .portrait, img, &:hover) but not match any of the other lines. I am aware I could match every line and then process submatches on that to determine if it is in fact a valid selector group, but if possible I would like to avoid that and just match the selector groups in one swoop.

I am pretty good at regex and I got it working perfectly on rubular.com/r/oF8b6TzArG, but when I try to do it in a tmLanguage file it does not seem to work at all. Perhaps it is some limitation with backreferences that could be a variable number of spaces?

Here is what I am working with

<dict> <key>match</key> <string>^( *?)(&amp;\:\.\#a-zA-Z](.*?))(?=\n\1 +)</string> <key>captures</key> <dict> <key>2</key> <dict> <key>name</key> <string>meta.selector.stylus</string> </dict> </dict> </dict>

I am also aware if we assume one selector per line without spaces I could change it to ^( ?)(&:.#a-zA-Z](\S?))(?=\n) and that will work in Sublime.

Any help would be greatly appreciated.

Also don’t worry about the whole tabs vs. spaces thing. After I get it working I can easily adapt it to work with both.

0 Likes

#2

To do multi-line regexs, you need to use the begin and end keys. See here

0 Likes

#3

Thanks for the reply. I tried using begin and end too, but didn’t have any luck. I will try again.

0 Likes

#4

Yeah, no luck. For reference here is the code I tried

<dict> <key>begin</key> <string>^( *)</string> <key>end</key> <string>\n\1 +</string> <key>match</key> <string>&amp;:.#a-zA-Z](.*)</string> <key>name</key> <string>meta.selector.stylus</string> </dict>

Also what I am trying to match is a single line so it shouldn’t require using begin and end. Positive lookaheads for \n still seem to work when using single line matching.

0 Likes

#5

As you can see per language grammar:

0 Likes

#6

Yes, and the pattern I am using is trying to match just a single line. Matching against a single line still lets you do a positive look ahead onto the next line. I don’t think the problem has to do with single line vs. multi line because it still fails when using the begin and end tags.

It seems to me that the problem is that the backreference fails to match a variable number of spaces correctly to detect indentation.

0 Likes