Sublime Forum

Syntax hightlighting for String interpolation in Ruby

#1

A Ruby .rb file with the following:

  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:

<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!

0 Likes

#2

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

<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>
0 Likes

#3

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).

0 Likes