[TxMt] Re: Dynamic includes in language grammars

Elia Schito elia at schito.me
Thu May 10 22:27:40 UTC 2012


Can you send me the updated grammar/bundle when finished? :)


Here's the answer:
(from: http://blog.macromates.com/2011/format-strings/)

The Case for Replacements

Another form of Format Strings allows you to change the case of a variable
reference. If you combine that with the fact that scope nameelements in
TextMate 2's Language Grammars are now Format Strings, this allows for nice
dynamic scoping tricks.

For example, say we rework Ruby's "here-document" (heredoc) syntax rule to
look something like this (simplified) example:


begin = '<<-?(\w+)';
name  = 'string.unquoted.${1:/downcase}.ruby';
…

That would mean that a heredoc definition like this:


<<-CSS
body {
  margin: 10px auto;
}
CSS

would be scoped as meta.here-doc.css.ruby. This allows the injection of the
CSS Grammar and automations into the scope meta.here-doc.css. That would
mean that you get the proper syntax highlighting of these heredocs and you
could trigger CSS Bundle commands while editing them.

As I write this, the ruby grammar still has a dozen rules with a dozen
different tokens that include a dozen different languages, so the above is
a planned simplification that would allow us to avoid having to upgrade the
ruby grammar for new potential here-doc tokens.

The supported case transformations are /upcase, /downcase, and/capitalize.
There's also a special /asciify transformation that will transliterate
things like "æ" and "ø" into "ae" and "o" respectively as well as remove
any remaining non-ASCII characters. The latter is particularly useful when
deriving an identifier or label, that needs to be accepted by a language
parser, from a heading or comment, which is free-form prose. These
transformations can be combined.








Elia

—
☁ @elia <http://twitter.com/elia> (twitter) ✎ elia at schito.me (gtalk)
☎ (+39) 348/9051393  perlelia at gmail.com (FaceTime)



On Wed, May 9, 2012 at 12:08 AM, Tyson Tate <tyson at tysontate.com> wrote:

> I'm updating my Markdown bundle to better handle GitHub-style fenced code
> blocks like so:
>
>        ```ruby
>        foo = { bar: true }
>        ```
>
> The grammar is simple enough for one language:
>
>        ruby_block = {
>                begin = '^```ruby.*$';
>                end = '^```.*$';
>                patterns = ( { include = 'source.ruby'; } );
>        };
>
> However, I'd like to support any language. I tried the following, but it
> doesn't appear to work (and I'm not sure why):
>
>        code_block = {
>                begin = '^```(\w+).*$';
>                end = '^```.*$';
>                patterns = ( { include = 'source.\1'; } );
>        };
>
> Is something similar possible / supported?
>
> Thanks!
>
> -Tyson
>
> _______________________________________________
> textmate mailing list
> textmate at lists.macromates.com
> http://lists.macromates.com/listinfo/textmate
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macromates.com/textmate/attachments/20120511/9f082735/attachment.html>


More information about the textmate mailing list