Thanks. The recommendations worked. Adding L:(…) around all the injections selector does the trick. I don’t know why it didn’t work for me then, but probably because of the second problem. Simplifying the other selectors and not trying to follow the ERB syntax makes everything a little bit easier to understand (and correctly syntax highlight both languages in the same file).

Thanks a lot for the help!

El lunes, 30 de mayo de 2016 a las 14:36, textmate-request@lists.macromates.com escribió:
On 16 May 2016, at 17:29, Daniel Rodr?guez Troiti?o wrote:

A simple version of my grammar looks like this:
[?]
'source.swift.gyb - (meta.embedded.block.gyb)' = {

The injection scope selector should use L:(?) so your injected rules 
go before Swift?s (as you also write yourself).

{ begin = '(^|\s*)(?=%\{(?![^\}]*\}%))';

This seems to be the problem, this rule doesn?t actually match 
anything as it?s a ?begin of line? assertion and then a ?look 
ahead? assertion.

So while the rule ?matches? when you have `%{` at the beginning of a 
line, no characters are consumed, so we descend into the rule?s 
patterns, but here we also have the Swift rules, and the L:(?) only 
affects the priority of the root rules injected (not their children), so 
the Swift rule will consume the `%` character because it was not 
consumed by the parent rule?s begin pattern.

A minimal version of your grammar that works would be this:

{ injections = {
'L:(source.swift.gyb - meta.embedded.block.gyb)' = {
patterns = (
{ name = 'meta.embedded.block.gyb';
begin = '%\{';
end = '\}%';
contentName = 'source.python';
patterns = ( { include = 'source.python'; } );
},
);
};
};
patterns = ( { include = 'source.swift'; } );
}

I understand you wanted to scope the leading/trailing whitespace and 
then re-use the actual matching of %{ and %}, but I don?t think this 
is possible, though I think it will be simpler to add optional matches 
like this:

begin = '(^\s*)?%\{';
end = '\}%(\s*$)?';

Then name these captures for 
`punctuation.whitespace.embedded.[leading|trailing].gyb`.