Use argparse in the snipmate conversion script, renamed that script to convert_snipmate_snippets, removed the snipmate compatibility code from UltiSnips.vim and placed it into it's own file, fixed a bug where the user's value of g:snips_author was overwritten

This commit is contained in:
Phillip Berndt 2011-07-14 12:24:14 +02:00
parent d08b58cf39
commit 8276ec9b94
3 changed files with 42 additions and 19 deletions

View File

@ -12,16 +12,6 @@ if exists('did_UltiSnips_vim') || &cp || version < 700 || !has("python")
finish
endif
" Snipmate compatibilty: Filename function, taken from
" snipMate.vim {{{
let g:snips_author = "John Doe"
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 {{{
" The trigger used to expand a snippet.

View File

@ -0,0 +1,28 @@
" File: snipMate_compatibility.vim
" Author: Phillip Berndt <phillip.berndt@gmail.com>
" Description: Snipmate compatibility helper functions for UltiSnips
"
" Snipmate defines a function named Filename and a variable called
" g:snips_author for use in snippet subtitutions. See
" https://github.com/msanders/snipmate.vim/blob/master/doc/snipMate.txt
" for details.
"
if exists('did_UltiSnips_snipmate_compatibility')
finish
endif
let did_UltiSnips_snipmate_compatibility = 1
" Define g:snips_author; some snipmate snippets use this
if ! exists('g:snips_author')
let g:snips_author = "John Doe"
endif
" 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
" }}}

View File

@ -7,6 +7,7 @@
import sys
import re
import os
import argparse
def convert_snippet_contents(content):
" If the snippet contains snipmate style substitutions, convert them to ultisnips style "
@ -68,16 +69,20 @@ def convert_snippets(source):
return convert_snippet_file(source)
if __name__ == '__main__':
if len(sys.argv) not in (3, 2):
print >> sys.stderr, "Syntax: get_snipmate_snippets.py <source directory or file> [target file]"
print >> sys.stderr
# Parse command line
argsp = argparse.ArgumentParser(description="Convert snipmate compatible snippets to UltiSnips' file format",
epilog="example:\n %s drupal/ drupal.snippets\n will convert all drupal specific snippets from snipmate into one file drupal.snippets" % sys.argv[0],
formatter_class=argparse.RawDescriptionHelpFormatter)
argsp.add_argument("source", help="Source directory for one filetype or a snippets file")
argsp.add_argument("target", help="File to write the resulting snippets into. If omitted, the snippets will be written to stdout.", nargs="?", default="-")
args = argsp.parse_args()
source = args.source
try:
target = sys.stdout if args.target == "-" else open(args.target, "w")
except IOError:
print >> sys.stderr, "Error: Failed to open output file %s for writing" % args.target
sys.exit(1)
if len(sys.argv) == 2:
source = sys.argv[1]
target = sys.stdout
else:
source, target = sys.argv[1:]
target = open(target, "w")
snippets = convert_snippets(source)
print >> target, snippets