From 2bffc46ecb557ee7c0cb63092bc9357aca598edf Mon Sep 17 00:00:00 2001 From: Vadim Khohlov Date: Thu, 2 Oct 2014 11:04:56 +0300 Subject: [PATCH 1/2] Python: added snippets for classmethod and staticmethod --- UltiSnips/python.snippets | 43 +++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/UltiSnips/python.snippets b/UltiSnips/python.snippets index 3387533..b63cf37 100644 --- a/UltiSnips/python.snippets +++ b/UltiSnips/python.snippets @@ -157,6 +157,20 @@ def write_slots_args(args, snip): args = ['"_%s"' % arg for arg in args] snip += '__slots__ = (%s,)' % ', '.join(args) + +def write_function_code(t, snip): + snip.rv = "" + snip >> 1 + + args = get_args(t[2]) + if args: + write_docstring_args(args, snip) + + style = get_style(snip) + snip += format_return(style) + snip.rv += '\n' + snip.mkline('', indent='') + snip += triple_quotes(snip) + endglobal ######################################## @@ -395,22 +409,33 @@ def __coerce__(self, other): ${25:pass} endsnippet + snippet def "function with docstrings" b def ${1:function}(`!p if snip.indent: snip.rv = 'self' + (", " if len(t[2]) else "")`${2:arg1}): `!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p -snip.rv = "" -snip >> 1 +write_function_code(t, snip) ` + ${0:pass} +endsnippet -args = get_args(t[2]) -if args: - write_docstring_args(args, snip) -style = get_style(snip) -snip += format_return(style) -snip.rv += '\n' + snip.mkline('', indent='') -snip += triple_quotes(snip) ` +snippet defc "class method with docstrings" b +@classmethod +def ${1:function}(`!p +if snip.indent: + snip.rv = 'cls' + (", " if len(t[2]) else "")`${2:arg1}): + `!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p +write_function_code(t, snip) ` + ${0:pass} +endsnippet + + +snippet defs "static method with docstrings" b +@staticmethod +def ${1:function}(${2:arg1}): + `!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p +write_function_code(t, snip) ` ${0:pass} endsnippet From f298510733dce805963af81ec4e8d3e80d8de9bb Mon Sep 17 00:00:00 2001 From: Vadim Khohlov Date: Sun, 5 Oct 2014 00:25:28 +0300 Subject: [PATCH 2/2] Added docstring; removed empty line --- UltiSnips/python.snippets | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/UltiSnips/python.snippets b/UltiSnips/python.snippets index b63cf37..41a3645 100644 --- a/UltiSnips/python.snippets +++ b/UltiSnips/python.snippets @@ -158,7 +158,13 @@ def write_slots_args(args, snip): snip += '__slots__ = (%s,)' % ', '.join(args) -def write_function_code(t, snip): +def write_function_docstring(t, snip): + """ + Writes a function docstring with the current style. + + :param t: The values of the placeholders + :param snip: UltiSnips.TextObjects.SnippetUtil object instance + """ snip.rv = "" snip >> 1 @@ -409,13 +415,12 @@ def __coerce__(self, other): ${25:pass} endsnippet - snippet def "function with docstrings" b def ${1:function}(`!p if snip.indent: snip.rv = 'self' + (", " if len(t[2]) else "")`${2:arg1}): `!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p -write_function_code(t, snip) ` +write_function_docstring(t, snip) ` ${0:pass} endsnippet @@ -426,7 +431,7 @@ def ${1:function}(`!p if snip.indent: snip.rv = 'cls' + (", " if len(t[2]) else "")`${2:arg1}): `!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p -write_function_code(t, snip) ` +write_function_docstring(t, snip) ` ${0:pass} endsnippet @@ -435,7 +440,7 @@ snippet defs "static method with docstrings" b @staticmethod def ${1:function}(${2:arg1}): `!p snip.rv = triple_quotes(snip)`${4:TODO: Docstring for $1.}`!p -write_function_code(t, snip) ` +write_function_docstring(t, snip) ` ${0:pass} endsnippet