在Snipmate.vim片段中更改案例?

Jar*_*nen 6 vim snipmate

是否有可能在Snipmate片段中更改变量值的大小写?

例如:

snippet dc
  def create
    @${1} = $1.new
  end
Run Code Online (Sandbox Code Playgroud)

应输出:

def create
  @product = Product.new
end
Run Code Online (Sandbox Code Playgroud)

我试图使用反引号来调用自定义函数:

snippet dc
  def create
    @${1} = `ToUpperCase('$1')`.new
  end
Run Code Online (Sandbox Code Playgroud)

并在Vim中定义了这个函数:

function! ToUpperCase(str)
    let result = substitute(a:str, '\(\w\)', '\u\1', '')
    return result
endfunction
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为看起来Snipmate 执行反引号扩展了它的$ n变量.

Sir*_*Ver 10

免责声明:我是UltiSnips的主要作者.

为了您的兴趣和rollin-the-drum的目的,我提出了UltiSnips的两个片段定义,这些定义在此之前已经提到过.两者都做OP想要的.第一个使用转换(TextMate语法):

snippet dc "create" b
def create
   @$1 = ${1/.*/\u$0/}.new
end
endsnippet
Run Code Online (Sandbox Code Playgroud)

第二个使用python代码插值.对于我的口味,这更容易阅读,但它更冗长一点.

snippet dc "create" b
def create
   @$1 = `!p snip.rv = t[1].title()`.new
end
endsnippet
Run Code Online (Sandbox Code Playgroud)

由于版本1.3 UltiSnips附带了一个可以转换snipMate片段的脚本,因此切换应该很容易.


Jar*_*nen 2

我对 snipMate 进行了一些修改,以实现我正在寻找的功能。

将此代码放在 autoload/snipMate.vim 中 s:RemoveSnippet() 函数的末尾(第 #14 行之后):

let linecount = len(getline("1", "$"))
for linenum in range(1, linecount)
    let line = getline(linenum)
    let line = substitute(line, '\v\%uc\(([^)]+)\)', '\U\1\E', 'g')
    let line = substitute(line, '\v\%ucfirst\(([^)]+)\)', '\u\1', 'g')
    call setline(linenum, line)
endfor
Run Code Online (Sandbox Code Playgroud)

现在您可以像这样定义片段:

snippet dc
  def create
    @${1:product} = %ucfirst($1).new
    %uc($1) = "This is Ruby %uc(constant) example."
  end
Run Code Online (Sandbox Code Playgroud)

输出:

def create
  @product = Product.new
  PRODUCT = "This is Ruby CONSTANT example."
end
Run Code Online (Sandbox Code Playgroud)

请注意,替换不是实时完成的,而是在您“退出”代码片段之后完成的。