Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I initially attempted to implement this with (regex) conditional expressions after seeing an apparent backreference in an end pattern referencing a capture group from a begin pattern. I soon learned that TextMate performs a string replace on the end pattern first (replacing things that look like backreferences with the captured values from the begin patterns) before the end pattern is actually used as a regex. TextMate does not however do any such magic that would support conditional expressions in end patterns referencing capture groups from begin patterns. Infininight recommended some regex magic that works within the existing framework of the TextMate grammar though, and this magic enables us to make each existing heredoc rule work for both regular and indented heredocs, instead of duplicating each rule and maintaining one set of rules for regular heredocs and another set of rules for indented heredocs. So what is the magic? Given an end pattern of (^((?!\1)\s+)?HTML$), \1 is replaced by TextMate before being used as a regex with the contents of capture group 1 from the begin pattern (as mentioned above). Imagine that the begin pattern is <<(~)HTML, and so \1 indicates whether or not the heredoc being matched is an indented heredoc. In the event the heredoc is indented, we have (^((?!~)\s+)?HTML$). The negative look-ahead for ~ will always match a correctly- formatted terminator, thereby enabling the presence of \s+ preceding the terminator. The ? following the whole ((?!~)\s+) means that the preceding whitespace isn't _required_ when an indented heredoc is used. In the event the heredoc is not indented, we have (^((?!)\s+)?HTML$). The negative look-ahead with no contents never matches, and thus the \s+ will never be part of a match, meaning whitespace will never be allowed for non-intended heredocs. It's magic.
- Loading branch information