'标记”对于VIM" />

"执行并更新'#=>'标记”对于VIM

发布于 2024-10-13 02:32:30 字数 665 浏览 7 评论 0 原文

VIM中是否有可能重复“执行并更新'# =>'标记”用于 ruby​​ 代码的 TextMate 功能。

我想要这样的东西:

x = 2
class A
  def a
    42
  end
end

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

输入一些命令...并获取

x = 2
class A
  def a
    42
  end
end

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

这是来自 Ciarán Walsh 的博客

另一个绝对值得了解的工具 是“执行并更新'# =>' 标记”命令(默认在 ⌃⇧⌘E 上)。 要使用它,请添加一些注释标记 (#⇥ 上有一个片段 为您插入这些)到末尾 您想查看的线路 结果然后触发 命令。 TextMate 将运行您的代码 并报告标记结果 行内联在评论中。这 该功能非常适合发布代码 在线,因为它显示了来源和 结果在一起。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总以为 2024-10-20 02:32:30

使用 rcodetools 中的 xmpfilter

Use xmpfilter from rcodetools.

情话已封尘 2024-10-20 02:32:30

看起来将其编写为 Vim 函数应该不会太难。试试这个:

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

然后 :调用 ExecuteAndUpdate()

一个限制是它用标记对每一行进行两次评估。因此带有标记的线条不应该有副作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文