Sublime Forum

Enhancing Ruby Syntax highlighting: assistance needed

#1

I’m trying to enhance syntax highlighting for ruby files by adding the ability to recognize heredoc strings with embedded ERB. It already works for HTML, SQL, and a few other languages:

untitled_%E2%80%94_User

Digging into the Ruby syntax file, I found the sections related to heredoc language embedding and made a new one for erb:

  heredocs:
    # heredoc with embedded HTML and indented terminator
    - match: '(<<[-~]"?((?:[_\w]+_|)HTML)\b"?)'
      scope: punctuation.definition.string.begin.ruby
      push: [heredoc-html, trailing-heredoc-start]
    # heredoc with embedded ERB and indented terminator
    - match: '(<<[-~]"?((?:[_\w]+_|)ERB)\b"?)'
      scope: punctuation.definition.string.begin.ruby
      push: [heredoc-erb, trailing-heredoc-start]

and

  heredoc-html:
    - meta_scope: string.unquoted.embedded.html.ruby
    - meta_content_scope: text.html.embedded.ruby
    - match: ^\s*\2$
      scope: punctuation.definition.string.end.ruby
      pop: true
    - include: scope:text.html.basic
    - include: interpolated-ruby
    - include: escaped-char
  
  heredoc-erb:
    - meta_scope: string.unquoted.embedded.html.ruby
    - meta_content_scope: text.html.ruby.embedded.ruby
    - match: ^\s*\2$
      scope: punctuation.definition.string.end.ruby
      pop: true
    - include: scope:text.html.ruby
    - include: interpolated-ruby
    - include: escaped-char

Full syntax file here

With this modification, the ERB embedding works… sort of:

Any idea how I could get sublime to better recognize the end delimiter? I’m guessing it must be something about the language I’m embedding since HTML works fine.

As a side note, it’s interesting that the included CSS embedding has the same issue:

0 Likes

#2

In case it helps, here is what I get for scope stacks when my cursor is on the ending delimiter:

HTML (working):

source.ruby.custom
string.unquoted.embedded.html.ruby
punctuation.definition.string.end.ruby

ERB (not working):

source.ruby.custom
string.unquoted.embedded.html.ruby
text.html.ruby.embedded.ruby
text.html.basic

CSS (not working):

source.ruby.custom
string.unquoted.embedded.css.ruby
text.css.embedded.ruby
meta.rule-set.css.css3
meta.selector-list.css.css3
meta.selector.css.css3
variable.other.tag.css.cs3

0 Likes

#3

CSS looks fine to me.
18%20PM

class Foo < Bar
    <<~CSS
        body {background-color: green;}
    CSS

    def baz
        # Seems fine, no?
    end
end

It looks a little bit like you could be popping out of your ERB prematurely on the first one. I can’t tell exactly, because strings in Ruby are going to be highlighted the same as strings in ERB.

Perhaps you can work out which scope you’re in with the Cmd+Alt+P scope info popup?

Edit: saw your second post. Looks perhaps like ERB immediately pushes text.html.basic and you’re not able to pop out of both.

0 Likes

#4

Really appreciate the assist. You’re right that the ERB syntax pushes text.html.basic. It just adds a couple match patters on top of that, so I just copied those into the heredoc-erb context directly and had it include text.html.basic, and that got it working.

  heredoc-erb:
    - meta_scope: string.unquoted.embedded.html.ruby
    - meta_content_scope: text.html.ruby.embedded.ruby
    - match: ^\s*\2$
      scope: punctuation.definition.string.end.ruby
      pop: true
    - include: scope:text.html.basic
    - include: interpolated-ruby
    - include: escaped-char
    - match: "<%+#"
      scope: punctuation.definition.comment.erb
      push:
        - meta_scope: comment.block.erb
        - match: "%>"
          pop: true
    - match: "<%+(?!>)[-=]?"
      scope: punctuation.section.embedded.ruby
      push:
        - meta_scope: source.ruby.rails.embedded.html
        - match: "-?%>"
          scope: punctuation.section.embedded.ruby
          pop: true
        - match: (#).*?(?=-?%>)
          scope: comment.line.number-sign.ruby
          captures:
            1: punctuation.definition.comment.ruby
        - include: "Ruby on Rails.sublime-syntax"
0 Likes