Added snipmate's Filename() helper to UltiSnips.vim

This commit is contained in:
Phillip Berndt 2011-07-13 14:19:17 +02:00
parent 2060e06e7c
commit 281781fcee
2 changed files with 10 additions and 16 deletions

View File

@ -12,6 +12,15 @@ if exists('did_UltiSnips_vim') || &cp || version < 700 || !has("python")
finish finish
endif endif
" Snipmate compatibilty: Filename function, taken from
" snipMate.vim {{{
fun! Filename(...)
let filename = expand('%:t:r')
if filename == '' | return a:0 == 2 ? a:2 : '' | endif
return !a:0 || a:1 == '' ? filename : substitute(a:1, '$1', filename, 'g')
endf
" }}}
" Global Variables {{{ " Global Variables {{{
" The trigger used to expand a snippet. " The trigger used to expand a snippet.

View File

@ -8,21 +8,9 @@ import sys
import re import re
import os import os
def global_code_as_required(content):
" Return some global code for the resulting snippet. Only python support here.. "
if "`!p snip.rv = Filename" in content:
return "import os\n" + \
"""def Filename(fn, *args):
filename = os.path.splitext(fn)[0]
if filename == "":
return args[1] if len(args) == 2 else ""
return filename if len(args) == 0 or args[0] == "" else args[1].replace("$1", filename)
"""
def convert_snippet_contents(content): def convert_snippet_contents(content):
" If the snippet contains snipmate style substitutions, convert them to ultisnips style " " If the snippet contains snipmate style substitutions, convert them to ultisnips style "
content = re.sub("`\s*Filename\(", "`!p snip.rv = Filename(fn, ", content) content = re.sub("`([^`]+`)", "`!v \g<1>", content)
content = re.sub("`(?!!)([^`]+`)", "`!v \g<1>", content)
return content return content
def convert_snippet_file(source): def convert_snippet_file(source):
@ -92,7 +80,4 @@ if __name__ == '__main__':
target = open(target, "w") target = open(target, "w")
snippets = convert_snippets(source) snippets = convert_snippets(source)
global_code = global_code_as_required(snippets)
if global_code:
print >> target, "global !p\n%s\nendglobal\n" % global_code
print >> target, snippets print >> target, snippets