Vim的函数计算从文件名的模块Ruby类的定义? [英] Vim function to calculate ruby class definition with modules from filename?

查看:201
本文介绍了Vim的函数计算从文件名的模块Ruby类的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始使用vim脚本到我的工作流程的一些更复杂的零件自动化。我在寻找一个具体的例子先进让我开始。你会如何​​VIM大师的执行下面的?

I'd like to start using vimscript to automate some more complicated parts of my workflow. I'm looking for a concrete advanced example to get me started. How would you vim guru's implement the following?

我在位于的lib /富/酒吧/ buz.rb 文件空缓冲区。我想用我的文件上下文+绑定到插入以下到缓冲区:

I'm in an empty buffer for a file located at lib/foo/bar/buz.rb. I want to use my file context + a binding to insert the following into the buffer:

module Foo
module Bar

  class Buz
  end

end
end

什么是vim的功能是什么样子?

What's the vim function look like?

奖励: Vim的功能更新类的定义,以反映当前位置

我觉得这个例子会照顾我的vim的脚本到一个新的水平。谢谢!

I think this example will take my vim scripting to the next level. Thanks!

推荐答案

这是什么,最终我来到了。

This is ultimately what I came up with.

" Generates a ruby class definition based on the current file's path
function! GenerateRubyClassDefinition()
  " parse file path
  let l:path = expand("%:.:r")
  let l:path = substitute(l:path, "lib/", "", "")
  let l:parts = split(l:path, "/")

  " extract parts
  let l:class_name = l:parts[-1]
  let l:module_names = l:parts[0:-2]

  " generate
  let l:output = ""

  " generate - module headers
  for m in l:module_names
    let l:output .= "module " . g:Abolish.mixedcase(m) . "\n"
  endfor

  " generate - class
  let l:output .= "\n"
  let l:output .= "  class " . g:Abolish.mixedcase(class_name) . "\n"
  let l:output .= "  end\n"
  let l:output .= "\n"

  " generate - module footers
  for m in l:module_names
    let l:output .= "end\n"
  endfor

  echo l:output
endfunction

以上段假定您已经安装tpope的VIM-取消插件。这有助于与snakecase文件路径转化为混合词对象名称。

The above snippet assumes you have tpope's vim-abolish plugin installed. This helps with the transformation of snakecase file paths into mixedcase object names.

采购上面,并呼吁:GenerateRubyClassDefinition()从打开的文件缓冲区将呼应目标输出

Sourcing the above and calling :GenerateRubyClassDefinition() from an open file buffer will echo the target output.

这篇关于Vim的函数计算从文件名的模块Ruby类的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆