-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.py
170 lines (139 loc) · 4.53 KB
/
editor.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
# -*- coding: utf-8 -*-
import threading
from time import sleep
class Editor():
count = [[]]
tab_width = 0
tab_worthy = ['{', ':']
values = {"new_line": "\u2029", 'tab': " "}
curr_word = ""
end_new_word = False
lines = []
def return_space(self, val):
self.space_return.emit(val)
def returnCoOrd(self, co_ord):
self.sendCoOrd.emit(co_ord)
def return_char(self, val):
self.char_return.emit(val)
def return_enter(self, val):
self.enter_return.emit(val)
def return_backspace(self, pos):
self.backspace_return.emit(pos)
def return_backtab(self, pos):
self.backtab_return.emit(pos)
def _pressed_space(self, full_text, char, line, cur_pos, breaks):
self._handle_text(full_text, breaks)
self.co_ord(cur_pos)
def _pressed_enter(self, full_text, cur_pos):
# Calculate tabs
if cur_pos != 0:
query = self.values["new_line"] + self._tabs(full_text[-1])
else:
query = self.values["new_line"]
stat = query
self.return_enter(stat)
def _pressed_mouse(self, cur_pos):
sleep(0.2)
self.wakeUp.emit("")
def _pressed_char(self, text, char, line, cur_pos, breaks):
self.counter(text, breaks)
self.co_ord(cur_pos)
# self._check_spelling(char)
# self._handle_text(full_text)
def _pressed_tab(self, some_text, cur_pos, pure):
if pure:
# add tabs
tabs = self._tabs(some_text[-1])
self.return_enter(tabs)
else:
# delete tabs
self._pressed_backtab(some_text, cur_pos)
def _pressed_backspace(self, some_text, cur_pos):
start = -1
end = -1
if some_text[-4:] == '\xa0' * 4:
# tab delete
if self.tab_width > 0:
self.tab_width -= 1
start = cur_pos - 4
end = cur_pos - 1
else:
start = cur_pos - 4
end = cur_pos - 1
else:
# normal delete
start = cur_pos - 1
end = cur_pos - 1
self.return_backspace([start, end])
def _pressed_backtab(self, some_text, cur_pos):
if some_text[-4:] == '\xa0' * 4:
# tab delete
if self.tab_width > 0:
self.tab_width -= 1
start = cur_pos - 4
end = cur_pos
else:
start = cur_pos - 4
end = cur_pos
self.return_backtab([start, end])
def co_ord(self, cur_pos):
c_thread = threading.Thread(target=self._co_ord,
args=[cur_pos])
c_thread.daemon = True
c_thread.start()
def counter(self, text, breaks):
c_thread = threading.Thread(target=self._counter,
args=[text, breaks])
c_thread.daemon = True
c_thread.start()
def _break_lots(self, text):
pass
def _check_spelling(self, word):
pass
def _co_ord(self, cur_pos):
# change cursor position
cur_pos += 1
ln_no = 0
for line in self.count:
if cur_pos in line:
col = line.index(cur_pos) + 1
self.returnCoOrd([ln_no, col])
ln_no += 1
break
else:
pass
ln_no += 1
def _counter(self, text, breaks):
if "\u2029" in text:
splits = text.split("\u2029")
else:
splits = [text]
self.count = [[]]
line_no = 0
char_no = 0
for line in splits:
line_no += 1
self.count.append([])
for char in line:
char_no += 1
self.count[line_no].append(char_no)
else:
char_no += 1
self.count[line_no].append(char_no)
def _editor_count(self, text, cur_pos):
breaks = []
self._handle_text(text, breaks)
self.co_ord(cur_pos)
def _handle_text(self, text, breaks):
self.counter(text, breaks)
def _send_text(self, full_text, line, cur_pos):
pass
def _tabs(self, last_char):
if last_char in self.tab_worthy:
self.tab_width += 1
return self.values['tab'] * self.tab_width
def _wake_enter_up(self, text, cur_pos, breaks):
self._handle_text(text, breaks)
self.co_ord(cur_pos)
def _wake_me_up(self, cur_pos):
self.co_ord(cur_pos)