How to add syntax highlighting to sublime text 2

2019-02-12 04:28发布

问题:

Given the string "text text #{interpolation}" Sublime Text 2 highlights the whole string with one color. I would like to highlight the interpolated text so it is easy to pick out. When I press ctrl-shift-alt-p in the interpolated section Sublime tells me the namespace: source.ruby string.quoted.double.ruby source.ruby.embedded.source

I am wondering where I would define a rule to highlight this(I think in the tmLanguage file), what format that rule would take, and how to go about assigning a color to it.

回答1:

If you dig into the included Dawn.tmTheme file (one of the only included themes that does this highlighting properly) you'll find these highlighting rules for String embedded-source, for some reason some of the themes leave this out completely:

<dict>
    <key>name</key>
    <string>String embedded-source</string>
    <key>scope</key>
    <string>string source</string>
    <key>settings</key>
    <dict>
        <key>background</key>
        <string>#6F8BBA26</string>
        <key>fontStyle</key>
        <string></string>
        <key>foreground</key>
        <string>#080808</string>
    </dict>
</dict>


回答2:

This is better as it actually highlights the code within the interpolation as normal code, rather than all the same colour.

<dict>
   <key>name</key>
      <string>Embedded Ruby Punctuation</string>
   <key>scope</key>
      <string>string punctuation.section.embedded.ruby</string>
   <key>settings</key>
   <dict>
      <key>foreground</key>
      <string>#F92672</string>
   </dict>
</dict>
<dict>
   <key>name</key>
      <string>Embedded Ruby Source</string>
   <key>scope</key>
      <string>string source.ruby.embedded.source</string>
   <key>settings</key>
   <dict>
      <key>foreground</key>
      <string>#FFFBF7</string>
   </dict>
</dict>