3

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

2 Answers 2

4

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

8
  • I wonder, why there's no such plugin already? :) Thanks for the excellent answer. Sep 10, 2012 at 19:28
  • @SergioTulentsev Now there is.
    – Daniel Beck
    Sep 10, 2012 at 20:04
  • thanks man, you're awesome! Although you eliminated this one opportunity to learn, I won't complain :) Sep 11, 2012 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, 2012 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, 2012 at 18:30
4

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.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .