-
Notifications
You must be signed in to change notification settings - Fork 3
/
python.snippets
171 lines (137 loc) · 3.99 KB
/
python.snippets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
global !p
import px
for full_name, name in px.libs().items():
exec("import " + full_name)
from px.whitespaces import *
endglobal
global !p
def ensure_newlines(snip):
px.langs.python.ensure_newlines(snip.buffer, snip.snippet_start)
px.langs.python.ensure_newlines_after(snip.buffer, snip.snippet_end)
snip.cursor.preserve()
def expand_method_in_right_place(snip):
if isinstance(snip.context, dict):
print(snip.context)
return
if snip.context and snip.context[0].strip().startswith('class '):
indent_amount = 1
else:
indent_amount = 0
if snip.visual_content:
line, _ = get_higher_indent(snip.buffer, snip.cursor)
indent, _ = get_indentation(line)
new_cursor = match_exact_indent(snip.buffer, snip.cursor, indent, 'def ')
if new_cursor:
snip.cursor.set(*new_cursor)
insert_lines_before(snip.buffer, snip.cursor, ['\n'])
snip.context = {
'call_position': snip.cursor,
'call_indent': get_indentation(snip.buffer[snip.cursor[0]]),
'snip.context_match': snip.context,
'visual': snip.visual_content
}
snip.cursor.set(*px.whitespaces.ensure_indent(snip.buffer, snip.cursor, indent_amount))
def insert_extracted_method_call(snip):
if snip.tabstop != 0:
return
if snip.context['visual']:
call_position = snip.context['call_position']
snip.buffer[call_position[0]] = snip.context['call_indent'][1]
vim.current.window.snip.cursor = snip.cursor.to_vim_cursor(*call_position)
snip.expand_anon('a($2, $1)')
return from_vim_snip.cursor(vim.current.window.snip.cursor)
else:
return snip.cursor
def after_def(snip):
prev_line = snip.buffer[snip.line-1].strip()
return prev_line.startswith("def ") or prev_line.startswith("class ")
endglobal
snippet i "if"
if $1:
${0:${VISUAL}}
endsnippet
snippet r "return"
return
endsnippet
priority 10
pre_expand "expand_method_in_right_place(snip)"
post_expand "ensure_newlines(snip)"
snippet d "def"
def $1($2):
$3
endsnippet
snippet f "for"
for $1 in $2:
${0:${VISUAL}}
endsnippet
snippet p "print" b
print("`!p snip.rv=t[1]`", ${1:${VISUAL}})
endsnippet
snippet pf "print to file" b
with open('/tmp/log','ab') as f: f.writelines([repr($1), "\n"])
endsnippet
snippet "(\s+)if n" "if not" br
`!p snip.rv = match.group(1)`if not
endsnippet
global !p
def filter_previous_buffer(snip, pattern):
for line in snip.buffer[0:snip.line]:
if not re.match(pattern, line):
return False
return True
endglobal
snippet "^i" "import" "filter_previous_buffer(snip, '^(#|$|import |from )')" reA
import $0
endsnippet
snippet "^import (\w+)" "from ... import" br
from `!p snip.rv = match.group(1)` import
endsnippet
snippet e "self.assertEquals" "match_higher_indent(snip.buffer, snip.cursor, 'def test')" be
self.assertEqual(
$1,
$2
)
endsnippet
pre_expand "expand_method_in_right_place(snip)"
post_expand "ensure_newlines(snip)"
post_jump "insert_extracted_method_call(snip)"
snippet d "def" "match_higher_indent(snip.buffer, snip.cursor, 'class ')" be
def $1(self$2):
${3:${VISUAL}}
endsnippet
snippet "^cu" "class unit test" br
import unittest
class $1TestCase(unittest.TestCase):
$2
if __name__ == '__main__':
unittest.main()
endsnippet
post_expand "ensure_newlines(snip.snippet_start, snip.snippet_end)"
snippet c "class" b
class $1:
$2
endsnippet
snippet c "class constructor" "match_higher_indent(snip.buffer, snip.cursor, '^class')" be
def __init__(self${1/.+/, /}$1):
$2
endsnippet
snippet o "docstring" "after_def(snip)" be
"""
$1
"""
endsnippet
snippet re.ma "re.match" "not px.syntax.is_string(px.cursor.to_vim(snip.cursor))" wAe
re.match(r'$1', $2)
endsnippet
snippet tx "" w
self$0
endsnippet
priority 100
snippet d "def test" "match_higher_indent(snip.buffer, snip.cursor, 'class \w+\(unittest')" be
def test_$1(self):
$2
endsnippet
# @TODO
# - snippet for adding import statement for package
# - autoexpand class and method definitions
# - snippet for constructor