"Execute and Update '# =>' Markers" for VIM

Is there a possibility in VIM to repeat "Execute and Update '# =>' Markers" TextMate feature for ruby code.

I'd like to have something like:

x = 2
class A
  def a
    42
  end
end

x # => 
A.new.a # =>

Enter some command... and get

x = 2
class A
  def a
    42
  end
end

x # => 2
A.new.a # => 42

Here is a description of this feature from Ciarán Walsh's Blog:

Another tool definitely worth knowing is the "Execute and Update '# =>' Markers" command (on ⌃⇧⌘E by default). To use it, add some comment markers (there is a snippet on #⇥ that will insert these for you) to the end of lines you would like to see the results of and then trigger the command. TextMate will run your code and report the result of the marked line inline in the comments. This feature is great for code posted online since it shows the source and the results together.


使用xmpfilter的xmpfilter。


Seems like it shouldn't be too hard to write this as a Vim function. Try this:

function! ExecuteAndUpdate()
    ruby << EOF
        marker = '# =>'
        buf = VIM::Buffer.current
        lines = File.readlines(buf.name)

        bnd = binding
        eval(lines.join("n"), bnd)

        lines.each_with_index do |line, i|
            if line.match(/#{marker}/)
                result = marker + ' ' + eval(line, bnd).inspect
                buf[i+1] = line.sub(/#{marker}.*/, result).chomp
            end
        end
EOF
endfunction

Then :call ExecuteAndUpdate() .

One limitation is that it evaluates each line with a marker twice. So lines with markers shouldn't have side-effects.

链接地址: http://www.djcxy.com/p/50094.html

上一篇: 帮助我了解PHP变量引用和范围

下一篇: “执行并更新”#=>'标记'用于VIM