Home Download Buy Blog Forum Support

Syntax hightlighting for String interpolation in Ruby

Syntax hightlighting for String interpolation in Ruby

Postby kmandrup on Wed Sep 28, 2011 3:05 pm

A Ruby .rb file with the following:

Code: Select all
  puts "Strategy Default for #{base}"


displays puts in white and the whole string in yellow (default theme). If I use fx "#{123}" the numbers are displayed/syntax highlighted in purple.
How can I get it to work with alphanumeric chars inside the interpolation?

I looked at the Falcon mode:

Code: Select all
<string>string.quoted.double.falcon</string>
<key>patterns</key>
<array>
   <dict>
      <key>match</key>
      <string>\\.</string>
      <key>name</key>
      <string>constant.character.escape.falcon</string>
   </dict>
   <dict>
      <key>match</key>
      <string>\$\(.*?\)</string>
      <key>name</key>
      <string>string.interpolated.falcon</string>
   </dict>
   <dict>
      <key>match</key>
      <string>\$[a-zA-Z_0-9]+</string>
      <key>name</key>
      <string>string.interpolated.falcon</string>
   </dict>
</array>


It should be similar for Ruby, except using #{...} as delimiters. I tried looking at the Ruy.tmlLanguage so far haven't figured out how to do it here... Anyone? Thanks!
kmandrup
 
Posts: 4
Joined: Wed Sep 28, 2011 2:31 pm

Re: Syntax hightlighting for String interpolation in Ruby

Postby kmandrup on Wed Sep 28, 2011 3:15 pm

Looks like the change to allow for vars inside the interpolations should be somewhere in here, but a bit cryptic to me!

Code: Select all
<key>interpolated_ruby</key>
<dict>
   <key>patterns</key>
   <array>
      <dict>
         <key>captures</key>
         <dict>
            <key>0</key>
            <dict>
               <key>name</key>
               <string>punctuation.section.embedded.ruby</string>
            </dict>
            <key>1</key>
            <dict>
               <key>name</key>
               <string>source.ruby.embedded.source.empty</string>
            </dict>
         </dict>
         <key>match</key>
         <string>#\{(\})</string>
         <key>name</key>
         <string>source.ruby.embedded.source</string>
      </dict>
      <dict>
         <key>begin</key>
         <string>#\{</string>
         <key>captures</key>
         <dict>
            <key>0</key>
            <dict>
               <key>name</key>
               <string>punctuation.section.embedded.ruby</string>
            </dict>
         </dict>
         <key>end</key>
         <string>\}</string>
         <key>name</key>
         <string>source.ruby.embedded.source</string>
         <key>patterns</key>
         <array>
            <dict>
               <key>include</key>
               <string>#nest_curly_and_self</string>
            </dict>
            <dict>
               <key>include</key>
               <string>$self</string>
            </dict>
         </array>
      </dict>
      <dict>
         <key>captures</key>
         <dict>
            <key>1</key>
            <dict>
               <key>name</key>
               <string>punctuation.definition.variable.ruby</string>
            </dict>
         </dict>
         <key>match</key>
         <string>(#@)[a-zA-Z_]\w*</string>
         <key>name</key>
         <string>variable.other.readwrite.instance.ruby</string>
      </dict>
      <dict>
         <key>captures</key>
         <dict>
            <key>1</key>
            <dict>
               <key>name</key>
               <string>punctuation.definition.variable.ruby</string>
            </dict>
         </dict>
         <key>match</key>
         <string>(#@@)[a-zA-Z_]\w*</string>
         <key>name</key>
         <string>variable.other.readwrite.class.ruby</string>
      </dict>
      <dict>
         <key>captures</key>
         <dict>
            <key>1</key>
            <dict>
               <key>name</key>
               <string>punctuation.definition.variable.ruby</string>
            </dict>
         </dict>
         <key>match</key>
         <string>(#\$)[a-zA-Z_]\w*</string>
         <key>name</key>
         <string>variable.other.readwrite.global.ruby</string>
      </dict>
   </array>
</dict>
kmandrup
 
Posts: 4
Joined: Wed Sep 28, 2011 2:31 pm

Re: Syntax hightlighting for String interpolation in Ruby

Postby ChadW on Wed Sep 28, 2011 5:25 pm

You should probably take a gander at the info docs for some background and the full TextMate syntax docs for details .

Specifically, you'll need to use the patterns-include sub-matching system. I haven't actually used this much myself, and know of it only through the docs, so I won't throw down code specifics myself (I've been making too many small errors in my own experiments; wouldn't want to lead you astray).
ChadW
 
Posts: 21
Joined: Fri Sep 23, 2011 7:45 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 36 guests