-
Notifications
You must be signed in to change notification settings - Fork 3
/
sh.snippets
262 lines (216 loc) · 4.82 KB
/
sh.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
global !p
from snippets import *
endglobal
snippet ! "shebang" b
#!/bin/bash
endsnippet
global !p
import px
for full_name, name in px.libs().items():
exec("import " + full_name)
endglobal
global !p "# NOQA"
import px.snippets
endglobal
snippet pr "print debug" bw
`!p
if " " in t[1]:
suffix = "\\n' >"
prefix = ''
else:
suffix = ' >'
prefix = t[1] + ": %q\\n' "
prefix = "{}:{}: {}".format(
os.path.basename(px.buffer.get().name),
str(px.cursor.get()[0]),
prefix
)
`printf 'XXXXXX `!p snip.rv=prefix`$1`!p snip.rv=suffix`&2
endsnippet
post_jump "px.snippets.expand(snip)"
snippet pd "Description" b
pr$1 >>${2:/tmp/debug}
endsnippet
snippet ix "if string empty" b
if [ -n "$$1" ]; then
${2:${VISUAL}}
fi
endsnippet
snippet w "" w
${1}x
endsnippet
snippet a "alias" b
alias $1='$2'
endsnippet
pre_expand "snip.context = replace_line_with_indent(snip)"
snippet i "if [" b
`!p
try:
condition
except:
condition = snip.context.strip()[1:]
brackets = 0
if t[1].startswith('-') or \
t[1].startswith('"') or \
t[1].startswith('!') or \
t[1].startswith('$('):
brackets = 2
`if`!p
snip.rv=" " + "["*brackets + " " if brackets > 0 else " "
`${1:`!p snip.rv = condition`}`!p
snip.rv=" " + "]"*brackets if brackets > 0 else ""
`; then
${2:${VISUAL}}
fi$0
endsnippet
snippet o "if [" b
if $1; then
${2:${VISUAL}}
fi
endsnippet
snippet "^(\s*)(:[:\w-]+)$" "function" rw
`!p snip.rv=match.group(1)``!p
snip.rv=match.group(2)`() {
`!p snip.rv=match.group(1)` ${0:${VISUAL}}
`!p snip.rv=match.group(1)`}
endsnippet
context "snip.visual_text"
snippet : "function visual auto" bA
:$1() {
${0:${VISUAL}}
}
endsnippet
global !p
def is_in_function(snip):
return px.whitespaces.match_higher_indent(
snip.buffer,
snip.cursor,
r'^([\w:_-])+\(\)\s*\{'
)
def is_local_def(snip):
prev_line = px.buffer.get_prev_nonempty_line()
if re.match(r'.*{$', prev_line):
return True
if re.match(r'\s+local \w+="?\$(\d+)"?', prev_line):
return True
else:
return False
def replace_line_with_indent(snip):
line = snip.buffer[snip.cursor[0]]
snip.buffer[snip.cursor[0]] = px.whitespaces.get_indentation(line)[1]
snip.cursor.preserve()
return line
endglobal
context "is_in_function(snip) and is_local_def(snip)"
pre_expand "snip.context = replace_line_with_indent(snip)"
snippet l "local" wbeA
`!p
try:
once
except:
once = True
curr_line = snip.context.strip()[1:]
var = ""
print(curr_line)
if curr_line != "":
matches = re.match(r'\s*(\w+)=(.*)', curr_line)
if matches:
var = matches.group(1)
arg = matches.group(2)
else:
arg="$({})".format(curr_line)
else:
prev_line = px.buffer.get_prev_nonempty_line()
matches = re.match(r'\s+local \w+="?\$(\d+)"?', prev_line)
if matches:
arg = '$' + str(int(matches.group(1))+1)
else:
arg = '$1'
`local ${1:`!p snip.rv=var`}=${2:`!p snip.rv=arg`}
endsnippet
snippet l "local" wb
local $0
endsnippet
priority -1
context "snip.visual_text and not snip.visual_text.strip().startswith('set -x')"
snippet "" "wrap in set -x" re
set -x
${VISUAL}
set +x
endsnippet
context "snip.visual_text.strip().startswith('set -x')"
snippet "" "wrap in set -x" re
${VISUAL/\n\s+set \+x|set -x\n\s+//g}
endsnippet
snippet c "" w
case "$1" in
$2)
$3
;;$4
esac
endsnippet
snippet " ;;" "" wrm
;;
$1)
$2
;;
endsnippet
snippet w "while" b
while ${1:[ $2 ]}; do
$3
done
endsnippet
snippet r "return" b
return $1
endsnippet
snippet f "for" b
for $1 in "$2"; do
$3
done
endsnippet
snippet sa "source absolute path" b
_base_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source $_base_dir/$1
endsnippet
snippet # "shdoc" b
# @description $1
endsnippet
snippet "# a" "shdoc arg" b
# @arg $$1
endsnippet
snippet "# e" "shdoc exitcode" b
# @exitcode $1
endsnippet
snippet "# s" "shdoc see" b
# @see $1
endsnippet
snippet "# x" "shdoc example" b
# @example
# $1
endsnippet
snippet "# n" "shdoc noargs" b
# @noargs
endsnippet
snippet "# o" "shdoc stdout" b
# @stdout $1
endsnippet
snippet is "import:source" b
import:source "$1"
endsnippet
snippet "import:source \"r" "import:source reconquest" rA
import:source "github.com/reconquest/$1
endsnippet
snippet "\$(\d+)-" "${N:-} var default" Air
\${`!p snip.rv = match.group(1)`:-$1}
endsnippet
context "re.match(r'\$(\d+)', get_selected_placeholder(snip) or '')"
snippet - "${N:-} var default auto visual" Aie
${`!p snip.rv = snip.context.group(1)`:-$1}
endsnippet
snippet "\$(\d+)\+" "${N:+} var alternate" Air
\${`!p snip.rv = match.group(1)`:+$1}
endsnippet
context "re.match(r'\$(\d+)', get_selected_placeholder(snip) or '')"
snippet + "${N:+} var alternate auto visual" Aie
${`!p snip.rv = snip.context.group(1)`:+$1}
endsnippet