Writing code as a string with syntax highlighting

I'm trying to write ruby ​​source code as a string (as part of the real Ruby code) in a text editor with syntax highlighting for Ruby code, but I want the Ruby syntax highlighting to work on the code within the line. By using double or single quotes, percentage notation, heredoc all make the text editor recognize that it is a string, so the entire piece of code is colored the same color as a string.

<<_
class Foo
    def bar
        # blah blah
    end
end
_

      

Is there a way to have Ruby syntax highlighting in the editor skip line boundaries so that its content is highlighted as Ruby code?


Edit

Stefan notices that Atom, Sublime Text, and RubyMine are great for doing this. But unfortunately it does not work in Ruby Emacs mode. Does anyone know of a modified ruby-mode.el

or some additional emacs library for this?

Edit

Or can anyone write some simple elisp code (just ignore the start and end of the heredoc when the id is the heredoc RUBY

if it's hard to implement highlighting for other languages)?

+3


source to share


2 answers


Atom, Sublime Text, and RubyMine at least support syntax highlighting based on the heredoc id . <<-RUBY

rendered as Ruby, <<-SQL

like SQL, etc.

Here's a screenshot from Atom:



Atom Editor Screenshot

+8


source


I know this question is two years old, but a late answer is always better than no answer.

As Dmitry hints at , it is indeed possible to do this in emacs mmm-mode. Here is part of my init-ruby-mode.el emacs config:

(require 'mmm-mode) ; install from melpa

(eval-after-load 'mmm-mode
'(progn
 (mmm-add-classes
  '((ruby-heredoc-js
     :submode js2-mode
     :front "<<-?JS_?.*\r?\n"
     :back "[ \t]*JS_?.*"
     :face mmm-code-submode-face)))
 (mmm-add-mode-ext-class 'ruby-mode "\\.rb$" 'ruby-heredoc-js)))

(eval-after-load 'mmm-mode
 '(progn
  (mmm-add-classes
  '((ruby-heredoc-shell
     :submode shell-script-mode
     :front "<<-?SH_?.*\r?\n"
     :back "[ \t]*SH_?.*"
     :face mmm-code-submode-face)))
 (mmm-add-mode-ext-class 'ruby-mode "\\.rb$" 'ruby-heredoc-shell)))

      

My emacs config is completely based on the purcell / emacs.d package . So I would recommend looking at LOTS for more good examples of how to make emacs work for you. I added a section for the nested ruby ​​code to work, this is what it looks like:



enter image description here

Also as a side note, above where it is stated require 'mmm-mode

- my config actually uses the purcell function require-package

, but not portable.

There is a bug where I need to save and return a buffer to get mmm-mode in order to recognize the newline heredoc, but I hardly use this feature so I shouldn't be solving it.

+1


source







All Articles