Is it possible to change the case of variable values within Snipmate snippets?
For example:
snippet dc
def create
@${1} = $1.new
end
Should output:
def create
@product = Product.new
end
I tried to use backticks to call a custom function:
snippet dc
def create
@${1} = `ToUpperCase('$1')`.new
end
And defined this function in Vim:
function! ToUpperCase(str)
let result = substitute(a:str, '\(\w\)', '\u\1', '')
return result
endfunction
This doesn't work as it seems that Snipmate expands its $n variables after it has executed the backticks.
Disclaimer: I am the main author of UltiSnips.
For your interest and rollin-the-drum purposes, I present two snippet definitions for UltiSnips which has been mentioned here before. Both do what the OP wants. The first one uses transformations (TextMate syntax):
snippet dc "create" b
def create
@$1 = ${1/.*/\u$0/}.new
end
endsnippet
The second uses python code interpolation. For my tastes this is easier to read but it is a little more verbose.
snippet dc "create" b
def create
@$1 = `!p snip.rv = t[1].title()`.new
end
endsnippet
Since version 1.3 UltiSnips comes with a script that can convert snipMate snippets, switching should therefore be easy.
The current release of snipMate is not capable of performing transformations on the mirrored text. Look up :help snipMate-disadvantages
where it says:
Regex cannot be performed on variables, such as "${1/.*/\U&}"
If you really want this feature, you might want to try one of the other snippet plugins out there. UltiSnips uses the same syntax for defining snippets, and claims to have all of the same features as TextMate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With