From 8f7cdb9c694862d0663ab2e7ad0b67f2f5e97d76 Mon Sep 17 00:00:00 2001 From: "rygwdn@gmail.com" <> Date: Sun, 16 May 2010 02:08:46 -0300 Subject: [PATCH] added new 'smart def' and 'smart class' for python using new indent info --- UltiSnips/python.snippets | 53 ++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/UltiSnips/python.snippets b/UltiSnips/python.snippets index 4fdec32..31e2f51 100644 --- a/UltiSnips/python.snippets +++ b/UltiSnips/python.snippets @@ -20,19 +20,54 @@ endsnippet ########## snippet class "smart class" b class ${1:MyClass}(${2:object}): - """${3:Docstring for $1}""" + """ ${3:Docstring for $1} """ - def __init__( self${4/([^,])?(.*)/(?1:, )/}${4:arg} ): - """ - TODO: Fill me in -${4/.+/(?0:\n)/}${4/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=[^,]*)?(,\s*|$)/(?2: $2 - describe me here\n)/g} """ -${2/object$|(.+)/(?1: $0.__init__\(self\)\n\n)/}${4/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=[^,]*)?(,\s*|$)/(?2: self._$2 = $2\n)/g} + def __init__(self$4): + """ ${5:TODO: Fill me in} +`!p res = "" +ts = '\t' +if vim.eval("&expandtab") == 1: + ts = " " * int(vim.eval("&ts")) + +indent = initial_indent + ts * 2 +args = [arg.split('=')[0].strip() for arg in t[4].split(',') if arg] +args = [arg for arg in args if arg and arg != "self"] + +if args: + res += '\n' + +for i, arg in enumerate(args): + res += indent + res += ":%s: description\n" % arg + +res += indent + '"""\n' +if t[2] != "object": + res += indent + t[2] + ".__init__(self)\n\n" + +for i, arg in enumerate(args): + res += indent + res += "self._%s = %s\n" % (arg, arg) +` + $0 endsnippet snippet def "smart def" b -def ${1:function}(${2/([^,])?(.*)/(?1:)/}${2:arg}): - """${3:Docstring for $1} -${2/.+/(?0:\n)/}${2/(\A\s*,\s*\Z)|,?\s*([A-Za-z_][A-Za-z0-9_]*)\s*(=[^,]*)?(,\s*|$)/ :param $2: description\n/g} +def ${1:function}(${2:self}): + """ ${3:TODO: Docstring for $1} +`!p res = "" +ts = '\t' +if vim.eval("&expandtab") == 1: + ts = " " * int(vim.eval("&ts")) + +args = [arg.split('=')[0].strip() for arg in t[2].split(',') if arg.strip()] +args = [arg for arg in args if arg and arg != "self"] + +if args: + res += '\n' +indent = initial_indent + ts +for arg in args: + res += indent + ":%s: description\n" % arg +` :returns: description """ ${0:pass}