Skip to content

Commit d5a382e

Browse files
committed
Massive Commit with many bugs For sure
1 parent 09c1cbb commit d5a382e

File tree

9 files changed

+192
-97
lines changed

9 files changed

+192
-97
lines changed

application/application.py

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from PythonVoiceCodingPlugin.queries import *
44
from 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
66
from 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

application/state_update.py

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_location_text(location,code):
5959
return code[location[0]:location[1]]
6060

6161
def convert_single_to_multiple(state):
62-
for k in ["result","origin","alternatives"]:
62+
for k in ["result","origin","initial_origin","alternatives"]:
6363
data = state[k]
6464
if data is None:
6565
data = []
@@ -73,17 +73,20 @@ def convert_single_to_multiple(state):
7373
state[k] = data
7474

7575
def convert_multiple_to_single(state):
76-
for k in ["result","origin","alternatives"]:
76+
print(" inside multiple single ",state)
77+
for k in ["result","origin","initial_origin","alternatives"]:
7778
data = state[k]
7879
if k not in ["alternatives"]:
7980
if data == []:
8081
data = None
8182
elif isinstance(data,list) and len(data)==1 and isinstance(data[0],list) and len(data[0])==1:
83+
print(" inside this case",k,data)
8284
data = data[0][0]
85+
print(" outside this case",k,data)
8386
else:
8487
raise Exception("when converting from multiple mode In single_mode "+k+" cannot be a nested list!")
8588
else:
86-
if isinstance(data,list) and all(isinstance(x,list) and len(x)==1 for x in data):
89+
if isinstance(data,list) and len(data)==1:
8790
data = make_flat(data)
8891
else:
8992
raise Exception(" when converting into single mode, each of the items in the alternatives must have length of one")
@@ -93,59 +96,78 @@ def convert_multiple_to_single(state):
9396
def clear_state(state):
9497
state["result"] = None
9598
state["origin"] = None
99+
state["initial_origin"] = None
96100
state["alternatives"] = []
97101
state["change_count"] = -1
98102

99103
def retrieve_primitive(state,sublime_data):
100104
output = deepcopy(state)
101105
output["alternatives"] = invert_guided(sublime_data["alternatives"],state["alternatives"])
102-
for k in ["result","origin"]:
106+
for k in ["result","origin","initial_origin"]:
103107
if not match_length(state[k],sublime_data[k]):
104108
raise Exception("state "+k+" does not match the sublime data")
105109
else:
106110
output[k] = sublime_data[k]
107-
return output
111+
for k in ["result","origin","initial_origin","alternatives"]:
112+
state[k] = output[k]
113+
return state
114+
115+
def retrieve_text(state,code):
116+
for k in ["result","origin","initial_origin","alternatives"]:
117+
state[k+"_text"] = get_location_text(state[k],code)
108118

109119

110120
def retrieve_state(state,view_information,code):
121+
'''
122+
normally I would like these to be implemented by means of modification handlers
123+
And an event listener transmitting every chains in the code. However it seems
124+
but the provided event listener does not provide me with the location that was changed.
125+
So in order to keep track over the location of important regions when the code changes,
126+
The current solution is to outsource everything to the sublime add_regions/get_regions
127+
functionality
128+
'''
129+
111130
if state["change_count"]>=view_information["change_count"]:
112-
return
131+
return False
113132
if state["change_count"]==-1:
114133
state["change_count"]=view_information["change_count"]
115-
return
116-
# retrieve data from their region tracker
134+
return False
117135

118136
try :
119137
convert_single_to_multiple(state)
120-
sublime_data = {x:get_regions_while_you_still_can(view_information,x) for x in ["result","origin","alternatives"]}
138+
sublime_data = {x:get_regions_while_you_still_can(view_information,x)
139+
for x in ["result","origin","alternatives","initial_origin"]}
140+
print("\nsublime date at ease ",sublime_data,"\n")
141+
121142
state = retrieve_primitive(state,sublime_data)
122143
convert_multiple_to_single(state)
144+
print(" after conversion ",state,"\n")
123145
except:
124146
clear_state(state)
125147
raise
148+
retrieve_text(state,code)
149+
return True
126150

127151

128-
for k in ["result","origin","alternatives"]:
129-
state[k+"_text"] = get_location_text(state[k],code)
130-
131152

132153

133154

134155

156+
def update_changes(state,locations_text):
157+
def forward(x,m):
158+
if x is None:
159+
return x
160+
if isinstance(x,list):
161+
return [forward(y,m) for y in x]
162+
return m.forward(x)
163+
164+
m = ModificationHandler()
165+
for location,t in locations_text:
166+
m.modify_from(0, location, t)
167+
168+
for k in ["result","origin","initial_origin","alternatives"]:
169+
state[k] = forward(state[k],m)
135170

136171

137172

138173

139-
def update_state(state,view_information,code):
140-
'''
141-
normally I would like these to be implemented by means of modification handlers
142-
And an event listener transmitting every chains in the code. However it seems
143-
but the provided event listener does not provide me with the location that was changed.
144-
So in order to keep track over the location of important regions when the code changes,
145-
The current solution is to outsource everything to the sublime add_regions/get_regions
146-
functionality
147-
'''
148-
state["result"] = view_information["get_regions"]("result")
149-
state["alternatives"] = view_information["get_regions"]("alternatives")
150-
state["origin"] = view_information["get_regions"]("origin")
151-
state["initial_origin"] = view_information["get_regions"]("initial_origin")

interface/common/actions.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import html
22

3+
from PythonVoiceCodingPlugin.interface.common.utility import make_region,make_sequence,all_or_nothing
4+
35
class InterfaceAction():
46
"""docstring for InterfaceAction"""
57
def __init__(self):
@@ -54,6 +56,7 @@ def __init__(self,name):
5456
def execute(self,view,**kwargs):
5557
index = 0
5658
while True:
59+
print(self.data["name"],index," attempted deletion ")
5760
index+=1
5861
r = view.get_regions(self.data["name"]+str(index))
5962
if r:
@@ -92,8 +95,8 @@ def execute(self,view,sublime,**kwargs):
9295

9396
class HighlightCleverAction(InterfaceAction):
9497
"""docstring for HighlightAction(InterfaceAction"""
95-
def __init__(self, region,name, result):
96-
self.data = {"region":region, "name":name , "result":result}
98+
def __init__(self, region,name, avoid = [],colorize = False):
99+
self.data = {"region":region, "name":name , "avoid":avoid,"colorize":colorize}
97100

98101
def execute(self,view,sublime,**kwargs):
99102
# these are the standard color maps for highlighting using the region-ish api
@@ -115,33 +118,35 @@ def execute(self,view,sublime,**kwargs):
115118
color_order = ["red","blue","green","yellow","orange"]
116119

117120
# transform the region variable into at list of sublime regions
121+
single_mode = False
118122
region = self.data["region"]
119123
if not isinstance(region ,list):
120-
region = [region]
121-
region = [ sublime.Region(x[0],x[1]) for x in region if x]
124+
region = [[region]] if region else [[]]
125+
elif isinstance(region,list):
126+
assert all_or_nothing(region,isinstance,list)," singular regions and sequences of regions are mixed"
127+
if all(not isinstance(x,list) for x in region):
128+
region = [[x] for x in region]
129+
single_mode = True
130+
region = make_region(region)
122131

123132
# transform the result into a sublime region
124-
result = self.data["result"]
125-
result = sublime.Region(result[0],result[1])
126-
127-
for i,(r,c) in enumerate(zip(region,color_order)):
133+
avoid = self.data["avoid"]
134+
avoid = make_region(avoid)
135+
avoid_sequence = make_sequence(avoid)
136+
overlapping = make_sequence(region) + make_sequence(avoid)
137+
for i,(br,c) in enumerate(zip(region,color_order)):
128138
use_reinforced = False
129-
# we use reinforced color if the region is contained within another
130-
for x,y in zip(region,color_order):
131-
if x.contains(r) and x is not r:
132-
use_reinforced = True
133-
134-
# to avoid foreground background problems if the region contains the selection it will not be reinforced
135-
if r.contains(result):
136-
use_reinforced = False
137-
# however if the region is contained within the selection it must be reinforced
138-
elif result.contains(r):
139-
use_reinforced = True
140-
if r.b-r.a==1:
141-
use_reinforced = True
142-
# we add the region alongside with an index
143-
view.add_regions(self.data["name"]+str(i+1), [r], reinforced_color[c] if use_reinforced else standard_color[c],
144-
"circle")
139+
for r in br:
140+
use_reinforced = any(
141+
(x.contains(r) and x is not r) or (r.contains(x) and x in avoid_sequence)
142+
for x in overlapping
143+
) or r.b-r.a==1 or use_reinforced
144+
if self.data["colorize"] and i<5:
145+
view.add_regions(self.data["name"]+str(i+1), br,
146+
reinforced_color[c] if use_reinforced and single_mode else standard_color[c],"circle")
147+
else:
148+
view.add_regions(self.data["name"]+str(i+1),br)
149+
145150

146151

147152

0 commit comments

Comments
 (0)