是否可以更改 Snipmate 片段中变量值的大小写?
例如:
snippet dc
def create
@${1} = $1.new
end
应该输出:
def create
@product = Product.new
end
我尝试使用反引号来调用自定义函数:
snippet dc
def create
@${1} = `ToUpperCase('$1')`.new
end
并在 Vim 中定义了这个函数:
function! ToUpperCase(str)
let result = substitute(a:str, '\(\w\)', '\u\1', '')
return result
endfunction
这不起作用,因为 Snipmate 在执行反引号后似乎会扩展其 $n 变量。
最佳答案
免责声明:我是 UltiSnips 的主要作者。
为了您的兴趣和敲鼓的目的,我提供了 UltiSnips 的两个片段定义之前已经在这里提到过。两者都做OP想要的。第一个使用转换(TextMate 语法):
snippet dc "create" b
def create
@$1 = ${1/.*/\u$0/}.new
end
endsnippet
第二种使用python代码插值。根据我的口味,这更容易阅读,但有点冗长。
snippet dc "create" b
def create
@$1 = `!p snip.rv = t[1].title()`.new
end
endsnippet
由于版本 1.3 UltiSnips 带有一个可以转换 snipMate 片段的脚本,因此切换应该很容易。
关于vim - 在 Snipmate.vim 片段中更改大小写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5064430/