-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_cpp.py
272 lines (210 loc) · 9.64 KB
/
custom_cpp.py
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
263
264
265
266
267
268
269
270
271
272
from dragonfly import (MappingRule, Choice, Dictation, Grammar, Function,RunCommand,FocusWindow)
from castervoice.lib import control
from castervoice.lib import settings
from castervoice.lib.actions import Key, Text
from castervoice.lib.context import AppContext
from castervoice.lib.merge.additions import IntegerRefST
from castervoice.lib.merge.state.short import R
from castervoice.lib.ctrl.mgr.rule_details import RuleDetails
# import subprocess
import json
variants = []
last_inserted=None
def send_sublime(c,data):
RunCommand(["subl", "-b","--command",c + " " + json.dumps(data)],synchronous = True).execute()
def insert_snippet(snippet):
global last_inserted
last_inserted = snippet
send_sublime("insert_snippet",{"contents": snippet})
def insert_snippet_with_variants(snippet_variants,n = 1, **kwargs):
global variants
if isinstance(snippet_variants,list):
insert_snippet(snippet_variants[n-1])
variants = snippet_variants
else:
variants = snippet_variants
insert_snippet(snippet_variants(n))
def insert_variant(n):
insert_snippet_with_variants(variants,n)
def display_variants():
items = [{"caption":json.dumps(x),"command":"insert_snippet","args":{"contents":x}} for x in variants]
send_sublime("quick_panel",{"items":items})
def string_range(lower,upper):
for i in range(lower,upper):
yield str(i)
def dollar_range(lower,upper):
for i in range(lower,upper):
yield "$" + str(i)
def dollar(x):
return "$" + str(x)
snippets = {
"just kidding":[
"${1:x}=$0;"
],
"auto assign":[
"auto $1 = $0;",
"auto [$1] = $0;",
"auto& $1 = $0;",
"auto& [$1] = $0;",
"auto $1 = *$0;",
],
"attribute assign":
lambda n: "".join(["auto& $"+x+" = $1.$"+x+";\n" for x in string_range(2,n + 2)]),
"reverse attribute assign":
lambda n: "".join(["$1.$"+x+" = $"+x+";\n" for x in string_range(2,n + 2)]),
"assign true":"$1 = true;",
"assign false":"$1 = false;",
"get": lambda n:"std::get<{0}>($0)".format(n),
"output line":lambda n:"std::cout<< " + ' << " " << '.join(["$" + str(x) for x in range(1,n + 1)]) + " << std::endl;",
"output stream":lambda n:"os << " + ' << " " << '.join(["$" + str(x) for x in range(1,n + 1)]) + " << std::endl;",
"input line":lambda n:"std::cin >> " + ' >> '.join(["$" + str(x) for x in range(1,n + 1)]) + ';',
"input stream":lambda n:"is >> " + ' >> '.join(["$" + str(x) for x in range(1,n + 1)]) + ';',
"error line":lambda n:"std::cerr<< " + ' << " " << '.join(["$" + str(x) for x in range(1,n + 1)]) + " << std::endl;",
"error named":lambda n:"std::cerr<< " + ' << " " << '.join(['"${0}" << " " << ${0}'.format(x) for x in range(1,n + 1)]) + " << std::endl;",
"output attributes":lambda n:"${0:std::cout}<< " + ' << " " << '.join(["$1.$" + str(x) for x in range(2,n + 2)]) + " << std::endl;",
"input attributes":lambda n:"${0:std::cin} >> " + ' >> '.join(["$1.$" + str(x) for x in range(2,n + 2)]) + ";",
"overload output":[
'std::ostream& operator << (std::ostream& os,const $1& $2){\n\t$0\n\treturn os;\n}\n'
],
"overload input":[
'std::istream& operator >> (std::istream& is,const $1& $2){\n\t$0\n\treturn is;\n}\n'
],
"output container":[
'std::copy($1.begin(),$1.end(),std::ostream_iterator<${2:int}>(${4:std::cout}," "));\n',
'for(auto& ${1:x}:${2:v}){\n'
'\tstd::copy($1.begin(),$1.end(),std::ostream_iterator<${3:int}>(${4:std::cout}," "));\n'
'\tstd::cout<< std::endl;\n'
'}\n',
],
"input container":[
'std::copy_n(std::istream_iterator<${3:int}>(${4:std::cin}), $2, std::back_inserter(${1:v}));',
],
"input old": lambda n:'scanf("' + "%d"*n + '",' + ','.join(["&"+x for x in dollar_range(1,n+1)])+');',
"single lambda":[
'[](auto a){return $0;}',
'[=](auto a){return $0;}',
'[&](auto a){return $0;}',
'[](auto ${1:a}){\n\t$0\n}',
'[=](auto ${1:a}){\n\t$0\n}',
'[&](auto ${1:a}){\n\t$0\n}',
],
"single lambda assign":"auto $1 = [&](auto ${2:a}){\n\t$0\n};",
"double lambda":[
'[](auto a,auto b){return $0;}',
'[=](auto a,auto b){return $0;}',
'[&](auto a,auto b){return $0;}',
'[](auto ${1:a},auto ${2:b}){\n\t$0\n}',
'[=](auto ${1:a},auto ${2:b}){\n\t$0\n}',
'[&](auto ${1:a},auto ${2:b}){\n\t$0\n}',
],
"double lambda assign":"auto $1 = [&](auto ${2:a},auto ${3:b}){\n\t$0\n};",
"evaluation comparison":[
"$1(${2:a})<$1(${3:b})",
"$1(${2:a})>$1(${3:b})",
"$1(${2:a})==$1(${3:b})",
"$1(${2:a})!=$1(${3:b})",
],
"attribute comparison":[
"${2:a}.$1 < ${3:b}.$1",
"${2:a}.$1 > ${3:b}.$1",
"${2:a}.$1 == ${3:b}.$1",
"${2:a}.$1 != ${3:b}.$1",
],
"look up comparison":[
"$1[${2:a}] < $1[${3:b}]",
"$1[${2:a}] > $1[${3:b}]",
"$1[${2:a}] == $1[${3:b}]",
"$1[${2:a}] != $1[${3:b}]",
],
"declare visited":"std::vector<bool> ${2:visited}(${1:n},false);",
"declare zero":lambda n:"int "+",".join([x+" = 0" for x in dollar_range(1,n+1)]) + ";",
"declare begin":"auto $1 = $2.begin();",
"declare end":"auto $1 = $2.end();",
"declare size":"auto $1 = $2.size();",
"classic loop":[
'for(int ${2:i} = 0; $2 < ${1:n}; $2++){\n\t$0\n}\n',
'for(int ${3:i} = 0; $3 < ${1:n}; $3++){\n\tfor(int ${3:j} = 0; $3 < ${2:m}; $3++){\n\t\t$0\n\t}\n}\n',
],
"range loop":[
"for(auto ${1:x} : ${2:v}){\n\t$0\n}\n",
"for(auto ${1:x} : ${2:v}){\n\tfor(auto ${3:y}:$1){\n\t\t$0\n\t}\n}\n",
],
"reference loop":[
"for(auto& ${1:x} : ${2:v}){\n\t$0\n}\n",
"for(auto& ${1:x} : ${2:v}){\n\tfor(auto& ${3:y}:$1){\n\t\t$0\n\t}\n}\n",
],
"double reference loop":[
"for(auto&& ${1:x} : ${2:v}){\n\t$0\n}\n",
"for(auto&& ${1:x} : ${2:v}){\n\tfor(auto& ${3:y}:$1){\n\t\t$0\n\t}\n}\n",
],
"zip loop":[
"for(auto $1 = $2.begin(); $1 < $2.end(); $1++){\n\t$0\n}\n",
"for(auto $1 = $2.begin(), $3 = $4.begin(); $1 < $2.end() && $3 < $4.end(); $1++,$3++){\n\t$0\n}\n",
],
"loop range":"for(auto&& $1 : range($2)){\n\t$0\n}\n",
"loop enumerate":"for(auto&& $1 : enumerate($2)){\n\t$0\n}\n",
"loop zip":"for(auto&& $1 : zip($2)){\n\t$0\n}\n",
"loop I map":"for(auto&& $1 : imap($2)){\n\t$0\n}\n",
"loop filter":"for(auto&& $1 : filter($2)){\n\t$0\n}\n",
"loop filter false":"for(auto&& $1 : filterfalse($2)){\n\t$0\n}\n",
"loop unique everseen":"for(auto&& $1 : unique_everseen($2)){\n\t$0\n}\n",
"loop takewhile":"for(auto&& $1 : takewhile($2)){\n\t$0\n}\n",
"loop dropwhile":"for(auto&& $1 : dropwhile($2)){\n\t$0\n}\n",
"loop cycle":"for(auto&& $1 : cycle($2)){\n\t$0\n}\n",
"loop repeat":"for(auto&& $1 : repeat($2)){\n\t$0\n}\n",
"loop count":"for(auto&& $1 : count($2)){\n\t$0\n}\n",
"loop groupby":"for(auto&& $1 : groupby($2)){\n\t$0\n}\n",
"loop starmap":"for(auto&& $1 : starmap($2)){\n\t$0\n}\n",
"loop accumulate":"for(auto&& $1 : accumulate($2)){\n\t$0\n}\n",
"loop compress":"for(auto&& $1 : compress($2)){\n\t$0\n}\n",
"loop sorted":"for(auto&& $1 : sorted($2)){\n\t$0\n}\n",
"loop sorted":"for(auto&& $1 : sorted($2)){\n\t$0\n}\n",
"loop chain":"for(auto&& $1 : chain($2)){\n\t$0\n}\n",
"loop chain from iterable":"for(auto&& $1 : chain.from_iterable($2)){\n\t$0\n}\n",
"loop reversed":"for(auto&& $1 : reversed($2)){\n\t$0\n}\n",
"loop slice":"for(auto&& $1 : slice($2)){\n\t$0\n}\n",
"loop sliding window":"for(auto&& $1 : sliding_window($2)){\n\t$0\n}\n",
"loop chunked":"for(auto&& $1 : chunked($2)){\n\t$0\n}\n",
"loop batched":"for(auto&& $1 : batched($2)){\n\t$0\n}\n",
"loop product":"for(auto&& $1 : product($2)){\n\t$0\n}\n",
"loop combinations":"for(auto&& $1 : combinations($2)){\n\t$0\n}\n",
"loop combinations_with_replacement":"for(auto&& $1 : combinations_with_replacement($2)){\n\t$0\n}\n",
"loop permutations":"for(auto&& $1 : permutations($2)){\n\t$0\n}\n",
"loop powerset":"for(auto&& $1 : powerset($2)){\n\t$0\n}\n",
"function template":[
"template<class T>\nauto $1($3${2:const T& t}){\n\t$0\n}\n",
"template<class T,class U>\nauto $1($4${2:const T& t},${3:const U& u}){\n\t$0\n}\n",
],
"function main":[
"int main(){\n\t$0\n}\n",
"int main(int argc, char** argv){\n\t$0\n}\n",
],
"condition end":"$0 == ${1:v}.end()",
"condition not end":"$0 != ${1:v}.end()",
}
class CppSnippetExample(MappingRule):
pronunciation = "sublime snippet"
mapping = {
"<snippet>":
R(Function(insert_snippet)),
"<snippet_variants> [<n>]":
R(Function(insert_snippet_with_variants)),
"variant <n>":
R(Key("c-z") + Function(insert_variant)),
"display variants":
R(Key("c-z") + Function(display_variants)),
"small test":
R(Function(send_sublime,c = "insert_snippe",data = {"contents":"${1:$PARAK}=2","PARAK":"y"})) +
R(Text("hello")),
"small jerry":
R(Function(send_sublime,c = "prev_field",data = {})),
}
extras = [
IntegerRefST("n",1,10),
Choice("snippet",{k:v for k,v in snippets.items() if isinstance(v,str)}),
Choice("snippet_variants",{k:v for k,v in snippets.items() if not isinstance(v,str)}),
]
defaults = {}
#---------------------------------------------------------------------------
def get_rule():
return CppSnippetExample, RuleDetails(name="new snippet", executable=["sublime_text"])