Fixes and improvements to snipmate conversion util by Steve McKinney

This commit is contained in:
Holger Rapp 2012-11-24 10:32:00 +01:00
parent b56e8991c7
commit 54d917ddd4
2 changed files with 25 additions and 12 deletions

View File

@ -1260,6 +1260,7 @@ Contributors listed in chronological order:
stardiviner - NagatoPain
skeept - Jorge Rodrigues
buztard
stephenmckinney - Steve McKinney
7.2 Snippets *UltiSnips-contrisnippets*

View File

@ -22,9 +22,9 @@ def convert_snippet_file(source):
# Ignore empty lines
if line.strip() == "":
continue
# The rest of the handlig is stateful
# The rest of the handling is stateful
if state == 0:
# Find snippet start
# Find snippet start. Keep comments.
if line[:8] == "snippet ":
snippet_info = re.match("(\S+)\s*(.*)", line[8:])
if not snippet_info:
@ -33,6 +33,9 @@ def convert_snippet_file(source):
retval += 'snippet %s "%s"' % (snippet_info.group(1), snippet_info.group(2) if snippet_info.group(2) else snippet_info.group(1)) + "\n"
state = 1
snippet = ""
elif line[:1] == "#":
retval += line
state = 0
elif state == 1:
# First line of snippet: Get indentation
whitespace = re.search("^\s+", line)
@ -45,10 +48,12 @@ def convert_snippet_file(source):
snippet += line[len(whitespace):]
state = 2
elif state == 2:
# In snippet: Check if indentation level is the same. If not, snippet has ended
if line[:len(whitespace)] != whitespace:
# In snippet: If indentation level is the same, add to snippet. Else end snippet.
if line[:len(whitespace)] == whitespace:
snippet += line[len(whitespace):]
else:
retval += convert_snippet_contents(snippet) + "endsnippet\n\n"
# Copy-paste the section from state=0 so that we don't skip every other snippet
#Copy-paste the section from state=0 so that we don't skip every other snippet
if line[:8] == "snippet ":
snippet_info = re.match("(\S+)\s*(.*)", line[8:])
if not snippet_info:
@ -57,8 +62,9 @@ def convert_snippet_file(source):
retval += 'snippet %s "%s"' % (snippet_info.group(1), snippet_info.group(2) if snippet_info.group(2) else snippet_info.group(1)) + "\n"
state = 1
snippet = ""
else:
snippet += line[len(whitespace):]
elif line[:1] == "#":
retval += line
state = 0
if state == 2:
retval += convert_snippet_contents(snippet) + "endsnippet\n\n"
return retval
@ -85,12 +91,18 @@ if __name__ == '__main__':
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
source_file_name = args.source
tmp_file_name = ''.join([args.target,'.tmp'])
try:
target = sys.stdout if args.target == "-" else open(args.target, "w")
tmp = sys.stdout if args.target == "-" else open(tmp_file_name, "w")
except IOError:
print >> sys.stderr, "Error: Failed to open output file %s for writing" % args.target
print >> sys.stderr, "Error: Failed to open output file %s for writing" % tmp_file_name
sys.exit(1)
snippets = convert_snippets(source)
print >> target, snippets
snippets = convert_snippets(source_file_name)
print >> tmp, snippets
if args.target != "-":
if os.access(args.target, os.F_OK):
os.remove(args.target)
os.rename(tmp_file_name, args.target)