Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This is my most favourite feature from ruby bundle in TextMate. Unfortunately, I can't find how to do the same in ST2. It still inserts the marker when you press TAB after the # symbol, so, I hope, the ability to run ruby code and update such markers is there, but is well hidden.

For non-users of TextMate: let's assume we have typed this code

a = 10
b = 30
a + b # => 

Then, upon pressing a shortcut (ctrl+cmd+shift+E by default), textmate executes this code and inserts value of corresponding lines after # => markers. In this instance, the result will look like this:

a = 10
b = 30
a + b # => 40

It's super handy for quickly trying out things and posting nice snippets to stackoverflow. Now I need ST2 to do this. :)

share|improve this question

2 Answers 2

up vote 4 down vote accepted

TextMate uses rcodetools to achieve this. From its bundle editor for the command Execute and Update ‘# =>’ Markers:

"${TM_RUBY}" -r "${TM_SUPPORT_PATH}/lib/ruby1.9/add_1.8_features.rb" -- "$TM_BUNDLE_SUPPORT/vendor/rcodetools/bin/xmpfilter"

xmpfilter comes with the rcodetools gem and can be run standalone.

$ sudo gem install rcodetools
[...]
$ export RUBYLIB=/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/lib/ruby/user-gems/1.8/gems/rcodetools-0.8.5.0/lib/
$ echo "1+5 # => " | ruby /System/Library/Frameworks/Ruby.framework/Versions/Current/usr/lib/ruby/user-gems/1.8/gems/rcodetools-0.8.5.0/bin/xmpfilter
1+5 # => 6

Now you just need to wrap these commands in a Sublime Text 2 plugin, passing the current document as input and replace it with output. You can find general information on how to create and integrate plugins with Sublime Text 2 in this answer.

Here's the plugin code custom to this problem, for the environment shown above:

import sublime, sublime_plugin, subprocess

class ExecuteAndUpdateRubyMarkers(sublime_plugin.TextCommand):
    def run(self, edit):
        r = sublime.Region(0, self.view.size())
        text = self.view.substr(r)

        s = subprocess.Popen(
            [
                '/usr/bin/env',
                'RUBYLIB=/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/lib/ruby/user-gems/1.8/gems/rcodetools-0.8.5.0/lib',
                'ruby',
                '/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/lib/ruby/user-gems/1.8/gems/rcodetools-0.8.5.0/bin/xmpfilter'
            ],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        out = s.communicate(text)
        if s.returncode != None and s.returncode != 0:
            sublime.message_dialog("There was an error: " + out[1])
            return

        viewlines = self.view.lines(r)
        outlines = out[0].split('\n')
        for i in range(0, len(viewlines)):
            self.view.replace(edit, viewlines[i], outlines[i])

Before:

Screenshot

After:

Screenshot

share|improve this answer
    
I wonder, why there's no such plugin already? :) Thanks for the excellent answer. –  Sergio Tulentsev Sep 10 '12 at 19:28
    
@SergioTulentsev Now there is. –  Daniel Beck Sep 10 '12 at 20:04
    
thanks man, you're awesome! Although you eliminated this one opportunity to learn, I won't complain :) –  Sergio Tulentsev Sep 11 '12 at 3:22
    
Daniel, I am having problems executing the plugin code you have posted. I am able to run the standalone example just fine. How are you executing the command from within ST2? Thanks –  AGS Nov 8 '12 at 18:23
    
@AGS Please be more specific about what the problem is, and make sure to read the linked answer explaining how to set up plugins to ST2. –  Daniel Beck Nov 8 '12 at 18:30

First of all, thanks to Daniel Beck for his solution as it works wonderfully and was just what I was looking for.

Edit 18 March 2013:

I've made this into a Sublime package called Ruby Markers. The package includes the code discussed in this topic as well as a few other things not discussed (like OS handling [I'm looking at you windows], unicode support, rvm and rbenv support). It is available via Package Manager or at https://github.com/mmims/sublime-text-2-ruby-markers.

share|improve this answer
    
Excellent, Michael. –  AGS Mar 27 '13 at 18:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.