I'm confused how nested patterns work in tmLanguage.json When I have the following code:
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"name": "myexample",
"scopeName": "source.myexample",
"patterns": [
{
"begin": "^(foo):",
"end": "$",
"beginCaptures": {
"1" : {"name": "myfoo"}
},
"patterns": [
{
"match": "^(bar)",
"captures": {
"1" : {"name": "mybar"}
}
}
]
}
]
}
I'd expect it to match this text:
foo:bar:bar
and capture word foo and first bar but not the second bar
However, it only matches foo and nothing else.
If I remove ^ from ^(bar) it matches both bar, but if I replace it with : it matches only second bar
So how do I make sure it captures only first occurrence of bar in the line, but only if it follows after foo: or if it's at the beginning of the entire line?:
foo:bar:bar -> should match
bar:bar -> should match
blah:bar:bar -> should not match
The foo: is optional it may or may not be present and I'd like to avoid duplicating the pattern with something like ^(?!foo:)bar
As I understand from TextMate grammar manual that nested patterns will use text that matched between begin and end of it's parent patterns, which confuses me why using ^ doesn't work or : captures only second occurrence...
^always matches at the beginning of a line. Using:won't help either, because your first colon is already consumed by^(foo):. Depending on your exact use case, you might instead check for a:following the relevantbar:or check that it is not at line end:
To match a line reading
bar:bar, you might have to put all rules on the top level, using"match"throughout.In that case, the above part might be written as: