1+ from dragonfly import (MappingRule , Choice , Dictation , Grammar , Repeat , StartApp , Function )
2+
3+ from castervoice .lib import control
4+ from castervoice .lib import settings
5+ from castervoice .lib .actions import Key , Text
6+ from castervoice .lib .context import AppContext
7+ from castervoice .lib .dfplus .additions import IntegerRefST
8+ from castervoice .lib .merge .state .short import R
9+ from castervoice .lib .ctrl .mgr .rule_details import RuleDetails
10+
11+
12+
13+ import os
14+ import subprocess
15+ import json
16+
17+ def create_arguments (command ,format ,** kwargs ):
18+ p = {x :kwargs [x ] for x in kwargs .keys () if x not in ['_node' ,'_rule' ,'_grammar' ]}
19+ p ["format" ] = format
20+ p ["command" ] = command
21+ return {"arg" :p }
22+
23+
24+ def send_sublime (c ,data ):
25+ x = json .dumps (data ).replace ('"' ,'\\ "' )
26+ y = "subl --command \" " + c + " " + x + "\" "
27+ subprocess .call (y , shell = True )
28+ # subprocess.call("subl --command \"argument {\\\"arg\\\":[1,\\\"None\\\", \\\"argument\\\", 1]}\"",shell = True)
29+ subprocess .call ("subl" , shell = True )
30+
31+ def noob_send (command ,format ,** kwargs ):
32+ data = create_arguments (command ,format ,** kwargs )
33+ send_sublime ("python_voice_coding_plugin" , data )
34+
35+ def lazy_value (c ,f ,** kwargs ):
36+ return R (Function (noob_send , command = c , format = f ,** kwargs ))
37+
38+
39+ class SublimeRulePlugin (MappingRule ):
40+ mapping = {
41+ # alternative rule
42+ "[smart] alternative <alternative_index>" :
43+ lazy_value ("alternative" ,1 ),
44+ "smart <color> [alternative]" :
45+ lazy_value ("alternative" ,2 ),
46+
47+ # paste back rule
48+ "[smart] paste back [<paste_back_index>]" :
49+ lazy_value ("paste_back" ,1 ),
50+ "[smart] paste <color> back" :
51+ lazy_value ("paste_back" ,2 ),
52+
53+ # argument rule
54+ "[smart] [<adjective>] argument <argument_index>" :
55+ lazy_value ("argument" ,1 ),
56+ "[smart] <vertical_direction> [<ndir>] [<adjective>] argument <argument_index>" :
57+ lazy_value ("argument" ,2 ),
58+ "[smart] [<adjective>] <level> [<level_index>] argument <argument_index>" :
59+ lazy_value ("argument" ,3 ),
60+ "[smart] <level> [<level_index>] <adjective> argument <argument_index>" :
61+ lazy_value ("argument" ,4 ),
62+
63+ # big roi rule
64+ "smart <big_roi> [<big_roi_sub_index>]" :
65+ lazy_value ("big_roi" ,1 ),
66+ "[smart] <adjective> <big_roi> [<big_roi_sub_index>]" :
67+ lazy_value ("big_roi" ,2 ),
68+ "[smart] <vertical_abstract_only_direction> [<ndir>] <big_roi> [<big_roi_sub_index>]" :
69+ lazy_value ("big_roi" ,3 ),
70+ "[smart] <vertical_abstract_only_direction> [<ndir>] <block> [<adjective>] <big_roi> [<big_roi_sub_index>]" :
71+ lazy_value ("big_roi" ,4 ),
72+
73+
74+ # insert rule
75+ "(smart insert|insert item) <item_index>" :
76+ lazy_value ("insert_item" ,1 ),
77+
78+ # collect rule
79+ "[smart] collect <collectable>" :
80+ lazy_value ("collect_indexable" ,1 ),
81+ "[smart] variable <collect_index>" :
82+ lazy_value ("collect_variable" ,2 ),
83+ "[smart] parameter <collect_index>" :
84+ lazy_value ("collect_parameter" ,2 ),
85+ "[smart] module <collect_index>" :
86+ lazy_value ("collect_module" ,2 ),
87+ "[smart] imported (value|object) <collect_index>" :
88+ lazy_value ("collect_imported_value" ,2 ),
89+
90+
91+
92+ # banana example
93+ # "banana [<adjective>] <big_roi> [<big_roi_sub_index>]":
94+ # lazy_value("big_roi",4,vertical_abstract_only_direction = "above",
95+ # ndir = 1,block = "function"),
96+
97+ }
98+ extras = [
99+ IntegerRefST ("argument_index" , 1 , 10 ),
100+ IntegerRefST ("alternative_index" , 1 , 10 ),
101+ IntegerRefST ("ndir" ,1 ,20 ),
102+ IntegerRefST ("level_index" ,1 ,10 ),
103+ IntegerRefST ("big_roi_sub_index" ,0 ,10 ),
104+ IntegerRefST ("paste_back_index" ,0 ,10 ),
105+ IntegerRefST ("collect_index" ,1 ,30 ),
106+ IntegerRefST ("item_index" ,1 ,30 ) ,
107+ Choice ("adjective" ,{
108+ "first" : "first" ,
109+ "second" : "second" ,
110+ "third" : "third" ,
111+ "fourth" : "fourth" ,
112+ "fifth" : "fifth" ,
113+ "sixth" : "sixth" ,
114+ "seventh" : "seventh" ,
115+ "eighth" : "eighth" ,
116+ "ninth" :"ninth" ,
117+ "last" :"last" ,
118+ "second last" : "second last" ,
119+ "third last" : "third last" ,
120+ "fourth last" : "fourth last" ,
121+ }
122+ ) ,
123+ Choice ("vertical_direction" ,{
124+ "up" :"up" ,
125+ "down" :"down" ,
126+ "above" :"above" ,
127+ "below" :"below" ,
128+ }
129+ ),
130+ Choice ("vertical_abstract_only_direction" ,{
131+ "above" :"above" ,
132+ "below" :"below" ,
133+ }
134+ ),
135+ Choice ("color" ,{
136+ "red" :1 ,
137+ "blue" :2 ,
138+ "green" :3 ,
139+ "yellow" :4 ,
140+ "orange" :5 ,
141+ }
142+ ),
143+ Choice ("level" ,{
144+ "inside" :"inside" ,
145+ }
146+ ),
147+ Choice ("big_roi" ,{
148+ "if condition" : "if condition" ,
149+ "else if condition" : "else if condition" ,
150+ "while condition" : "while condition" ,
151+ "if expression condition" : "if expression condition" ,
152+ "if expression body" : "if expression body" ,
153+ "if expression" :"if expression" ,
154+ "comprehension condition" : "comprehension condition" ,
155+ "return value" : "return value" ,
156+ "pass" :"pass" ,
157+ "break" : "break" ,
158+ "continue" : "continue" ,
159+ "assertion message" : "assertion message" ,
160+ "assertion condition" : "assertion condition" ,
161+ "(assignment right| right)" : "assignment right" ,
162+ "(assignment left| left)" : "assignment left" ,
163+ "assignment full" : "assignment full" ,
164+ "import statement" :"import statement" ,
165+ #"(import|imported) (value|item|object|element)":"import value",
166+ #"module" : "module",
167+ "(expression statement|expression)" : "expression statement" ,
168+ "iterator" : "iterator" ,
169+ "iterable" : "iterable" ,
170+
171+ }
172+ ),
173+ Choice ("block" ,{
174+ "(function|functions)" :"function" ,
175+ }
176+ ),
177+ Choice ("collectable" ,{
178+ "(variable|variables)" :"variable" ,
179+ "( parameter | parameters)" :"parameter" ,
180+ "(module|modules)" :"module" ,
181+ "(import|imported) (value|item|object|element)" :"import value" ,
182+ "function ( name |names)" :"function name" ,
183+ }
184+ ),
185+
186+
187+ ]
188+ defaults = {
189+ "adjective" :"None" ,
190+ "ndir" :1 ,
191+ "level_index" :1 ,
192+ "big_roi_sub_index" :0 ,
193+ "paste_back_index" :0 ,
194+
195+ }
196+
197+
198+ #---------------------------------------------------------------------------
199+
200+
201+ def get_rule ():
202+ return SublimeRulePlugin , RuleDetails (name = "python voice coding plugin" , executable = "sublime_text" , title = "Sublime Text" )
0 commit comments