-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_go.py
302 lines (224 loc) · 9.03 KB
/
_go.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from dragonfly import (CompoundRule, MappingRule, Grammar, Function, Choice, Dictation, Text, Key, IntegerRef)
from macro_utilities import (execute_with_dictation, replace_in_text)
from vim.rules.letter import (camel_case, proper)
class GoEnabler(CompoundRule):
spec = "enable Go"
def _process_recognition(self, node, extras):
if go_bootstrap is not None:
go_bootstrap.disable()
go_grammar.enable()
print("Go grammar enabled!")
class GoDisabler(CompoundRule):
spec = "disable Go"
def _process_recognition(self, node, extras):
go_grammar.disable()
go_bootstrap.enable()
print("Go grammar disabled")
visibility_attribute_choice_map = {
"public": "public",
"private": "private",
}
def visibility_attribute_choice(name="visibility_attribute"):
return Choice(name, visibility_attribute_choice_map)
def output_function(name, visibility_attribute=None, is_method=False):
method_parameter_output = "_" if name == "" else "$"
method_output = "(%s) " % method_parameter_output if is_method else ""
parameter_output = "_" if name == "" or is_method else "$"
execute_with_dictation(
name,
lambda n: replace_in_text(
"func %s%s(%s) _ {" % (method_output, format_name(
n, visibility_attribute), parameter_output)
),
lambda n: replace_in_text("func %s$(_) _ {" % method_output)
)
def output_type(name, construct, visibility_attribute=None):
execute_with_dictation(
name,
lambda n: Text(
"type %s %s {" % (format_name(n, visibility_attribute), construct)
),
lambda n: replace_in_text("type $ %s {" % construct)
)
comparison_choice_map = {
"equal": "==",
"not equal": "!=",
"less or equal": "<=",
"greater or equal": ">=",
"less": "<",
"greater": ">",
}
def comparison_choice(name="comparison"):
return Choice(name, comparison_choice_map)
def output_if(name, statement_type):
execute_with_dictation(
name,
on_dictation=lambda v: Text("%s %s {" % (statement_type, format_variable_name(name))),
on_other=lambda v: replace_in_text("%s $ {" % statement_type),
)
def output_none_check(name, construct, is_not=False):
comparison_output = "!= nil" if is_not else "== nil"
execute_with_dictation(
name,
lambda n: Text("%s %s %s {\n" % (construct, format_variable_name(n), comparison_output)),
lambda n: replace_in_text("%s $ %s {" % (construct, comparison_output))
)
def output_if_comparison(name, construct, comparison=None):
if comparison is not None:
execute_with_dictation(
name,
on_dictation=lambda v: replace_in_text(
"%s %s %s $ {" % (construct, format_variable_name(v), comparison)
),
on_other=lambda v: replace_in_text("%s $ %s _ {" % (construct, comparison))
)
def output_error_check(name):
execute_with_dictation(
name,
lambda n: Text("if %s != nil {\n" % format_variable_name(n)),
lambda n: Text("if err != nil {\n")
)
type_name_choice_map = {
"string": "string",
"integer": "int",
"boolean": "bool",
"float": "float",
}
def type_name_choice(name="type_name"):
return Choice(name, type_name_choice_map)
def output_make_slice(type_name=None):
type_output = type_name if type_name is not None else "$"
size_output = "$" if type_name is not None else "_"
replace_in_text("make([]%s, %s)" % (type_output, size_output)).execute()
def format_variable_name(name):
return camel_case(str(name).replace("-", ""))
def format_name(name, visibility_attribute):
if visibility_attribute is None or visibility_attribute == "private":
return camel_case(str(name).replace("-", ""))
elif visibility_attribute == "public":
return proper(str(name).replace("-", ""))
def output_make_map(type_name=None, type_name2=None):
if type_name is not None and type_name2 is not None:
Text("make(map[%s]%s)" % (type_name, type_name2)).execute()
else:
replace_in_text("make(map[$]_)").execute()
def output_make_channel(type_name=None):
if type_name is not None:
Text("make(chan %s)" % type_name).execute()
else:
replace_in_text("make(chan $)").execute()
def output_variable_declaration(name, type_name=None):
if type_name is not None:
execute_with_dictation(
name,
lambda n: Text("var %s %s" % (format_variable_name(n), type_name)),
lambda n: replace_in_text("var $ %s" % type_name)
)
else:
execute_with_dictation(
name,
lambda n: Text("var %s " % format_variable_name(n)),
lambda n: replace_in_text("var $ _")
)
def output_variable_initialization(name):
execute_with_dictation(
name,
lambda n: Text("%s := " % format_variable_name(n)),
lambda n: replace_in_text("$ := _")
)
def output_assignment(name):
execute_with_dictation(
name,
lambda n: Text("%s = " % format_variable_name(n)),
lambda n: Text(" = ")
)
def output_for_loop():
replace_in_text("for $ {").execute()
def output_ranged_for_loop(binding_name, name):
if name != "":
if binding_name != "":
Text(
"for _, %s := range %s {\n" % (format_variable_name(binding_name), format_variable_name(name))
).execute()
else:
replace_in_text("for _, $ := range %s {" % format_variable_name(name)).execute()
else:
if binding_name != "":
replace_in_text("for _, %s := range $ {" % format_variable_name(binding_name)).execute()
else:
replace_in_text("for _, _ := range $ {").execute()
def output_switch(name):
execute_with_dictation(
name,
lambda n: Text("switch %s {\n" % format_variable_name(n)),
lambda n: replace_in_text("switch $ {")
)
def output_case():
replace_in_text("case $:").execute()
def output_anonymous_function(name):
execute_with_dictation(
name,
lambda n: replace_in_text("func (%s $) _ {" % format_variable_name(n)),
lambda n: replace_in_text("func ($) _ {")
)
def output_index_brackets(index, name):
execute_with_dictation(
name,
lambda n: Text("%s[%d]" % (format_variable_name(n), index)),
lambda n: Text("[%d]" % index)
)
class GoUtilities(MappingRule):
mapping = {
# control flow
"if [<name>] is <comparison>": Function(output_if_comparison, construct="if"),
"if [<name>] is none": Function(output_none_check, construct="if", is_not=False),
"if [<name>] is not none": Function(output_none_check, construct="if", is_not=True),
"if [<name>]": Function(output_if, statement_type="if"),
"else if [<name>]": Function(output_if, statement_type="else if"),
"else if [<name>] is <comparison>": Function(output_if_comparison, construct="else if"),
"else": Text("else {") + Key("enter"),
"error check [on <name>]": Function(output_error_check),
"switch [on <name>]": Function(output_switch),
"case": Function(output_case),
"select": Text("select {\n"),
"for loop": Function(output_for_loop),
"for [<binding_name>] in [<name>]": Function(output_ranged_for_loop),
# definitions
"[<visibility_attribute>] function [<name>]": Function(output_function),
"[<visibility_attribute>] method [<name>]": Function(output_function, is_method=True),
"[<visibility_attribute>] struct [<name>]": Function(output_type, construct="struct"),
"[<visibility_attribute>] interface [<name>]": Function(output_type, construct="interface"),
# variables
"variable [<name>] [is <type_name>]": Function(output_variable_declaration),
"[<name>] is assigned": Function(output_variable_initialization),
"[<name>] equals": Function(output_assignment),
# containers
"make [<type_name>] slice": Function(output_make_slice),
"make map [from <type_name> to <type_name2>]": Function(output_make_map),
"make [<type_name>] channel": Function(output_make_channel),
# miscellaneous utilities
"anonymous function [taking <name>]": Function(output_anonymous_function),
"indexing <index> [into <name>]": Function(output_index_brackets),
}
extras = [
Dictation("name", default=""),
Dictation("binding_name", default=""),
visibility_attribute_choice("visibility_attribute"),
type_name_choice("type_name"),
type_name_choice("type_name2"),
comparison_choice("comparison"),
IntegerRef("index", min=0, max=9999999999),
]
go_bootstrap = Grammar("go bootstrap")
go_bootstrap.add_rule(GoEnabler())
go_bootstrap.load()
go_grammar = Grammar("go grammar")
go_grammar.add_rule(GoUtilities())
go_grammar.add_rule(GoDisabler())
go_grammar.load()
go_grammar.disable()
def unload():
global go_grammar
if go_grammar:
go_grammar.unload()
go_grammar = None