22
33from PythonVoiceCodingPlugin .queries import *
44from PythonVoiceCodingPlugin .application .build_cache import BuildCache
5- from PythonVoiceCodingPlugin .application .state_update import retrieve_state
5+ from PythonVoiceCodingPlugin .application .state_update import clear_state , retrieve_state , retrieve_text , get_location_text , update_changes
66from PythonVoiceCodingPlugin .interface .common .actions import *
77
88
@@ -14,7 +14,7 @@ class Application():
1414
1515 def __init__ (self ,vid ):
1616 self .history = []
17- self .state = {"result" : None ,"origin" : None ,"alternatives" : [],"change_count" :- 1 }
17+ self .state = {"result" : None ,"origin" : None ,"initial_origin" : None , " alternatives" : [],"change_count" :- 1 }
1818 self .ui_controller = None
1919 self .vid = vid
2020
@@ -26,7 +26,11 @@ def get_application(vid):
2626 Application .create_application_for_view (vid )
2727 return Application .active_applications [vid ]
2828
29-
29+ def update_text (self ,code ):
30+ retrieve_text (self .state ,code )
31+
32+ def update_locations (self ,locations_text ):
33+ update_changes (self .state ,locations_text )
3034
3135
3236 def respond_to_query (self ,interface ,query_description ,secondary = False ):
@@ -38,25 +42,31 @@ def respond_to_query(self,interface,query_description,secondary=False):
3842 code = view_information ["code" ]
3943 change_count = view_information ["change_count" ]
4044 latest_build = Application .build_cache .get_build (self .vid ,change_count )
41- retrieve_state (self .state ,view_information ,code )
45+ print ("state\n \n " ,self .state ,"\n \n " )
46+ try :
47+ if not secondary :
48+ print ("and during inside here " ,query_description )
49+ need_update = retrieve_state (self .state ,view_information ,code )
50+ print ("\n needed update: " ,need_update ,"\n " )
51+ print (" after update this state " ,self .state )
52+ except :
53+ clear_state (self .state )
54+ if False :
55+ raise
4256
4357 # get the corresponding query and execute it
4458 s = get_query (query_description )(code ,latest_build )
4559 secondary_query_description = get_secondary_query (query_description )
4660 if secondary_query_description :
47- backup = [deepcopy (self .state ),deepcopy (self .global_state )]
61+ self . backup = [deepcopy (self .state ),deepcopy (self .global_state )]
4862
4963 try :
5064 s (view_information ,query_description ,extra )
5165 except Exception as e :
52- print ("\n \n finally\n \n " )
53- if not s .exceptions_raised :
54- print (e )
55-
56- print ("\n \n finally\n \n " )
57- interface .clear_actions ()
58- interface .push_action (PopUpErrorAction (str (e )))
59- return False
66+ if not s .exceptions_raised :
67+ print ("inside exceptions raised" ,e )
68+ raise
69+ return False
6070
6171
6272 # check if there are exceptions
@@ -65,35 +75,36 @@ def respond_to_query(self,interface,query_description,secondary=False):
6575 interface .push_action (PopUpErrorAction (str (s .exceptions_raised )))
6676 return False
6777
78+
6879 # register build for later use
6980 b = s .get_the_latest_build ()
7081 if b :
7182 Application .build_cache .register_build (self .vid ,change_count ,b )
7283
84+ if secondary :
85+ self .state ,self .global_state = self .backup
86+
87+
7388 if isinstance (s ,SelectionQuery ):
7489 result = s .result
7590 alternatives = s .alternatives
76- # self.state["result"] = None
77- # self.state["alternatives"] = []
91+ self .state ["origin" ] = view_information ["selection" ]
92+ self .state ["initial_origin" ] = view_information ["selection" ]
93+ names = ["result" ,"origin" , "alternatives" ,"initial_origin" ]
94+ for name in names :
95+ interface .push_action (ClearHighlightAction (name ))
7896 if result :
7997 self .state ["result" ] = result
80- self .state ["alternatives" ] = []
81- self .state ["alternatives_text" ] = []
82- if not isinstance (result ,list ):
83- self .state ["result_text" ] = code [result [0 ]:result [1 ]]
84- else :
85- self .state ["result_text" ] = [code [x [0 ]:x [1 ]] for x in result if x ]
98+ self .state ["alternatives" ] = alternatives
99+ # self.state["alternatives_text"] = get_location_text(alternatives,code)
100+ self .update_text (code )
86101 interface .push_action (SelectionAction (result ))
87102 self .history .append (("selection" ,view_information ["change_count" ],view_information ["selection" ],result ))
88- interface .push_action (ClearHighlightAction ("alternatives" ))
89- if alternatives :
90- self .state ["alternatives" ] = alternatives
91- if not isinstance (alternatives [0 ],list ):
92- self .state ["alternatives_text" ] = [code [x [0 ]:x [1 ]] for x in alternatives ]
93- else :
94- self .state ["alternatives_text" ] = [[code [x [0 ]:x [1 ]] for x in y ] for y in alternatives if y ]
95- interface .push_action (DisplayRegionsAction ("alternatives" ,alternatives ,"Alternatives:\n " ))
96- interface .push_action (HighlightCleverAction (alternatives ,"alternatives" ,result ))
103+
104+
105+ # if alternatives:
106+ # interface.push_action(DisplayRegionsAction("alternatives",alternatives,"Alternatives:\n"))
107+
97108
98109 elif isinstance (s ,InsertionQuery ):
99110 output = s .writing_locations_text
@@ -102,6 +113,8 @@ def respond_to_query(self,interface,query_description,secondary=False):
102113 for location , text in output :
103114 interface .push_action (ReplaceAction (location ,text ))
104115 self .history .append (("insert" ))
116+ self .update_locations (output )
117+
105118 if selections :
106119 interface .push_action (SelectionAction (selections ))
107120
@@ -113,6 +126,7 @@ def respond_to_query(self,interface,query_description,secondary=False):
113126 if result :
114127 for location in writing_positions :
115128 interface .push_action (ReplaceAction (location ,result ))
129+ self .update_locations ([(x ,result ) for x in writing_positions ])
116130 if items :
117131 print (s .label ,"labeling\n " )
118132 interface .push_action (DisplayNiceAction (s .label ,items ,True ))
@@ -122,20 +136,29 @@ def respond_to_query(self,interface,query_description,secondary=False):
122136 if selections :
123137 interface .push_action (SelectionAction (selections ))
124138
139+ interface .push_action (HighlightCleverAction (self .state ["alternatives" ],"alternatives" ,self .state ["result" ],colorize = True ))
140+ for name in ["result" ,"origin" , "initial_origin" ]:
141+ interface .push_action (HighlightCleverAction (self .state [name ],name ))
142+
143+
125144 if secondary_query_description :
126145 interface .push_action (ClearHighlightAction ("alternatives" ))
127146 secondary_success = self .respond_to_query (interface ,secondary_query_description ,secondary = True )
128147 if not secondary_success :
129- self .state ,self .global_state = backup
148+ self .state ,self .global_state = self . backup
130149 return False
131150 return True
132151
133152
134153
135154 def respond_to_event (self ,interface ,event_description ):
136155 event = event_description ["event" ]
156+ view_information = interface .get_view_information ()
137157 if event == "update_change_count" :
158+ if self .state ["change_count" ] != event_description ["change_count" ]:
159+ retrieve_text (self .state ,view_information ["code" ])
138160 self .state ["change_count" ] = event_description ["change_count" ]
161+
139162
140163
141164
0 commit comments