forked from tgrosinger/aenea-grammars
/
_programming.py
277 lines (231 loc) · 7.19 KB
/
_programming.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
273
274
275
276
277
# Created for aenea using libraries from the Dictation Toolbox
# https://github.com/dictation-toolbox/dragonfly-scripts
#
# Commands for writing in the Python and Go programming languages
#
# Author: Tony Grosinger
#
# Licensed under LGPL
import aenea
import aenea.configuration
from aenea.lax import Key, Function
from aenea import (
IntegerRef,
Text,
Dictation,
Choice
)
from _generic_edit import pressKeyMap
from format import format_snake_case, format_pascal_case, format_camel_case
import dragonfly
vim_context = aenea.ProxyPlatformContext('linux')
grammar = dragonfly.Grammar('python', context=vim_context)
mode = "gopher"
mode_map = {
"gopher": "gopher",
"python": "python",
"sql": "sql",
}
sql_map = {
"update": "UPDATE ",
"select": "SELECT ",
"from": "FROM ",
"count": "COUNT ",
"values": "VALUES ",
"as": "AS ",
"when": "WHEN ",
"in": "IN ",
"and": "AND ",
"all": "ALL ",
"similar to": "SIMILAR TO ",
"like": "LIKE ",
"set": "SET ",
}
def switch_mode(language):
global mode
mode = language
def create_class(text):
if mode == "python":
newText = 'class %s():\n ' % format_pascal_case(text)
Text("%(text)s").execute({"text": newText})
def create_private_function(text):
if mode == "python":
newText = 'def _%s(' % format_snake_case(text)
Text("%(text)s").execute({"text": newText})
elif mode == "gopher":
newText = 'func %s(' % format_camel_case(text)
Text("%(text)s").execute({"text": newText})
def create_class_function(text, text2):
if mode == "gopher":
newText = 'func (x *%s) %s(' % (format_pascal_case(text),
format_pascal_case(text2))
Text("%(text)s").execute({"text": newText})
def create_public_function(text):
if mode == "python":
newText = 'def %s(' % format_snake_case(text)
Text("%(text)s").execute({"text": newText})
elif mode == "gopher":
newText = 'func %s(' % format_pascal_case(text)
Text("%(text)s").execute({"text": newText})
def close_function():
if mode == "python":
Text("):\n").execute()
elif mode == "gopher":
Text(") {\n\n").execute()
Key("up, tab").execute()
def print_line():
if mode == "python":
Text("print()").execute()
Key("left").execute()
if mode == "gopher":
Text("fmt.Println()").execute()
Key("left").execute()
def format_string():
if mode == "gopher":
Text("fmt.Sprintf()").execute()
Key("left").execute()
def comment():
Key("escape, i").execute()
if mode == "python":
Text("# ").execute()
if mode == "gopher":
Text("// ").execute()
def sql_word(sqlKeyword):
if mode != "sql":
sqlKeyword = sqlKeyword.lower().strip()
Text("%(text)s").execute({"text": sqlKeyword})
def null():
if mode == "python":
Text("None").execute()
if mode == "gopher":
Text("nil").execute()
def true():
if mode == "python":
Text("True")
if mode == "gopher":
Text("true")
def false():
if mode == "python":
Text("False")
if mode == "gopher":
Text("false")
basics_mapping = aenea.configuration.make_grammar_commands('python', {
'var': Text("var "),
'new class [named] <text>': Function(create_class),
'new [public] (function|func) [named] <text>': Function(create_public_function),
'new private (function|func) [named] <text>': Function(create_private_function),
'new class function <text> [named] <text2>': Function(create_class_function),
'close (function|func)': Function(close_function),
'new array': Key("lbracket, enter, enter, up, tab"),
'print line': Function(print_line),
'format string': Function(format_string),
'(null|nil)': Function(null),
'true': Function(true),
'false': Function(false),
'comment': Function(comment),
'mode <language>': Function(switch_mode),
'<sqlKeyword>': Function(sql_word),
# Common words
'util': Text("util"),
'air': Text("err"),
'query': Text("query"),
'(Jason|json)': Text("json"),
'upper (Jason|json)': Text("JSON"),
'extrahop': Text("extrahop"), # Company name
'app': Text("app"),
# Datatypes
'type-int': Text("int"),
'type-int 64': Text("int64"),
'type-int 32': Text("int32"),
'type-int 16': Text("int16"),
'type-bool': Text("bool"),
'type-string': Text("string"),
# Python
"pie-from": Text("from"),
"pie-none": Text("None"),
"pie-true": Text("True"),
"pie-false": Text("False"),
"pie-class method": Text("@classmethod\n"),
"pie-def": Text("def "),
"pie-for": Text("for "),
"pie-dict": Text("dict("),
"pie-set": Text("set("),
"pie-sum": Text("sum("),
"pie-len": Text("len("),
"pie-list": Text("list("),
"pie-tuple": Text("tuple("),
"pie is instance": Text("isinstance("),
"pie init": Text("__init__("),
"[pie] self dot": Text("self."),
"pie iter items": Text(".iteritems("),
"pie string join": Key("apostrophe, right")+Text(".join("),
"dunder": Text("__"),
"dunder main": Text("__main__"),
"dunder init": Text("__init__(self"),
"dunder around": Key("underscore:4, left:2"),
# Go lang
'go-from': Text("from"),
'go-true': Text("true"),
'go-false': Text("false"),
'go-none': Text("nil"),
'go-nil': Text("nil"),
'go-def': Text("func "),
'go-slice': Text("make([])") + Key("left"),
'go-struct': Text("type struct {\n\n}") + Key("up:2, right:4"),
})
operators_mapping = {
'defined [as]': Text(':= '),
'assign [to]': Text('= '),
'compare (equal|to)': Text('== '),
'compare not equal': Text('!= '),
'compare greater': Text('> '),
'compare less': Text('< '),
'compare geck': Text('>= '),
'compare lack': Text('<= '),
'bit ore': Text('| '),
'bit and': Text('& '),
'bit ex or': Text('^ '),
'times': Text('* '),
'divided': Text('/ '),
'plus': Text('+ '),
'minus': Text('- '),
'plus equal': Text('+= '),
'minus equal': Text('-= '),
'times equal': Text('*= '),
'divided equal': Text('/= '),
'mod equal': Text('%%= '),
'pointer to': Text('*'),
}
data_types_mapping = {
'string': Text('string'),
'int': Text('int'),
'int 64': Text('int64'),
'enum': Text('enum'),
'int 32': Text('int32'),
'(boolean|bool)': Text('bool'),
'struct': Text('struct'),
}
class Basics(dragonfly.MappingRule):
mapping = basics_mapping
extras = [
Dictation('text'),
Dictation('text2'),
IntegerRef('n', 1, 999),
IntegerRef('n2', 1, 999),
Choice("pressKey", pressKeyMap),
Choice("language", mode_map),
Choice("sqlKeyword", sql_map),
]
class Operators(dragonfly.MappingRule):
mapping = operators_mapping
class DataTypes(dragonfly.MappingRule):
mapping = data_types_mapping
grammar.add_rule(Basics())
grammar.add_rule(Operators())
grammar.add_rule(DataTypes())
grammar.load()
def unload():
global grammar
if grammar:
grammar.unload()
grammar = None