Permalink
Browse files

Merge https://github.com/SirVer/ultisnips

  • Loading branch information...
2 parents a236077 + 4c068bd commit 6de6f25ddccc1fed5f73ba8e7fda2e5498911a62 kiith-sa committed Dec 1, 2012
@@ -368,7 +368,12 @@ endsnippet
snippet rwprop "Read write property" b
def ${1:property}():
- ${2/.+/(?0:""")/}${2:The RW property $1}${2/.+/(?0:"""\n )/}def fget(self):
+ ${2/.+/(?0:""")/}${2:The RW property $1}`!p if t[2]:
+ snip.rv += '"""'
+ snip >> 1
+ snip += ""
+else:
+ snip.rv = ""`def fget(self):
return self._$1$0
def fset(self, value):
self._$1 = value
View
@@ -552,6 +552,10 @@ The options currently supported are: >
Vim settings and insert the tab characters as is. This option is useful
for snippets involved with tab delimited formats, for example.
+ s Remove whitespace immediately before the cursor at the end of a line
+ before jumping to the next tabstop. This is useful if there is a
+ tabstop with optional text at the end of a line.
+
The end line is the 'endsnippet' keyword on a line by itself. >
endsnippet
@@ -1260,6 +1264,7 @@ Contributors listed in chronological order:
stardiviner - NagatoPain
skeept - Jorge Rodrigues
buztard
+ stephenmckinney - Steve McKinney
7.2 Snippets *UltiSnips-contrisnippets*
@@ -288,6 +288,10 @@ def _re_match(self, trigger):
return match
return False
+ def has_option(self, opt):
+ """ Check if the named option is set """
+ return opt in self._opts
+
def matches(self, trigger):
# If user supplies both "w" and "i", it should perhaps be an
# error, but if permitted it seems that "w" should take precedence
@@ -412,12 +416,8 @@ def launch(self, text_before, visual_content, parent, start, end):
v.append(line_ind + line[tabs:])
v = '\n'.join(v)
- if parent is None:
- si = SnippetInstance(None, indent, v, start, end, visual_content = visual_content,
- last_re = self._last_re, globals = self._globals)
- else:
- si = SnippetInstance(parent, indent, v, start, end, visual_content,
- last_re = self._last_re, globals = self._globals)
+ si = SnippetInstance(self, parent, indent, v, start, end, visual_content,
+ last_re = self._last_re, globals = self._globals)
return si
@@ -743,6 +743,14 @@ def _jump(self, backwards = False):
if self._cs:
self._ctab = self._cs.select_next_tab(backwards)
if self._ctab:
+ before, after = _vim.buf.current_line_splitted
+ if self._cs.snippet.has_option("s"):
+ if after == "":
+ m = re.match(r'(.*?)\s+$', before)
+ if m:
+ lineno = _vim.buf.cursor.line
+ _vim.text_to_vim(Position(lineno,0), Position(
+ lineno,len(before)+len(after)), m.group(1))
_vim.select(self._ctab.start, self._ctab.end)
jumped = True
if self._ctab.no == 0:
@@ -15,12 +15,12 @@ class SnippetInstance(EditableTextObject):
also a TextObject because it has a start an end
"""
- def __init__(self, parent, indent, initial_text, start, end, visual_content, last_re, globals):
+ def __init__(self, snippet, parent, indent, initial_text, start, end, visual_content, last_re, globals):
if start is None:
start = Position(0,0)
if end is None:
end = Position(0,0)
-
+ self.snippet = snippet
self._cts = 0
self.locals = {"match" : last_re}
View
@@ -15,7 +15,7 @@ syn keyword snipTODO FIXME NOTE NOTES TODO XXX contained
syn match snipString '"[^"]*"'
syn match snipTabsOnly "^\t\+$"
-syn match snipKeyword "\(\<\(end\)\?\(snippet\|global\)\>\)\|extends" contained
+syn match snipKeyword "\(\<\(end\)\?\(snippet\|global\)\>\)\|extends\|clearsnippets" contained
" extends definitions
syn match snipExtends "^extends.*" contains=snipKeyword
@@ -36,6 +36,9 @@ syn match snipGlobalStart "^global.*" contained contains=snipKeyword,snipString
syn match snipGlobalEnd "^endglobal" contained contains=snipKeyword
syn region snipGlobal fold keepend start="^global" end="^endglobal" contains=snipGlobalStart,snipGlobalEnd,snipTabsOnly,snipCommand,snipVarExpansion,snipVar,@Python
+" snippet clearing
+syn match snipClear "^clearsnippets"
+
" highlighting rules
hi link snipComment Comment
@@ -58,4 +61,6 @@ hi link snipGlobalStart Statement
hi link snipGlobalEnd Statement
hi link snipGlobal Normal
+hi link snipClear Statement
+
let b:current_syntax = "snippet"
View
10 test.py
@@ -2605,6 +2605,16 @@ def _options_off(self):
keys = "test" + EX + JF + "sub junk {}"
wanted = "package c03;\nsub junk {}\n1;"
# End: Folding Interaction #}}}
+# Trailing whitespace {{{#
+class RemoveTrailingWhitespace(_VimTest):
+ snippets = ("test", """Hello\t ${1:default}\n$2""", "", "s")
+ wanted = """Hello\nGoodbye"""
+ keys = "test" + EX + BS + JF + "Goodbye"
+class LeaveTrailingWhitespace(_VimTest):
+ snippets = ("test", """Hello \t ${1:default}\n$2""")
+ wanted = """Hello \t \nGoodbye"""
+ keys = "test" + EX + BS + JF + "Goodbye"
+# End: Trailing whitespace }}}#
# Cursor Movement {{{#
class CursorMovement_Multiline_ECR(_VimTest):
@@ -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 @@ def convert_snippets(source):
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)

0 comments on commit 6de6f25

Please sign in to comment.