-
Notifications
You must be signed in to change notification settings - Fork 1
/
document_editor.py
255 lines (215 loc) · 9.04 KB
/
document_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
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
import json
import random
from typing import Tuple
from prompt_toolkit.application import get_app
from prompt_toolkit.clipboard import ClipboardData
from prompt_toolkit.document import Document
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from prompt_toolkit.keys import Keys
from prompt_toolkit.widgets import SearchToolbar
from author_lexer import AuthorLexer
from docengine import Doc
from message_service import MessageService
from text_editor import TextEditor
class DocumentEditor:
"""
Document Editor responds to keypress events and copy/cut/paste/delete
events by applying patches to internal CRDT document and then updating
the changed text on outer TextEditor document object.
"""
WINDOWS_LINE_ENDING = '\r\n'
UNIX_LINE_ENDING = '\n'
CR_CHAR = '\r'
def __init__(self, msg_service: MessageService):
self.doc = Doc()
self.doc.site = int(random.getrandbits(32))
self.patch_set = []
self.msg_service = msg_service
self.text_field = TextEditor(
scrollbar=True,
line_numbers=False,
search_field=SearchToolbar(),
key_bindings=self.__init_bindings(),
lexer=AuthorLexer(self.doc)
)
def __register_patch(self, patch) -> None:
"""
Put provided patch to internal patch set and send patch to
the server using Message Service
:type patch: str
"""
self.patch_set.append(patch)
message = self.msg_service.prepare_send_request({"type": "patch",
"content": patch})
self.msg_service.put_message(message)
def do_cut(self) -> None:
"""
Handle selection cut.
"""
new_doc, cut_data = self.__get_selection(cut=True)
get_app().clipboard.set_data(cut_data)
self.text_field.document = new_doc
def do_copy(self) -> None:
"""
Handle selection copy
"""
_, cut_data = self.__get_selection(cut=False)
get_app().clipboard.set_data(cut_data)
def do_delete(self) -> None:
"""
Handle selection delete
"""
self.text_field.document, _ = self.__get_selection(cut=True)
def do_paste(self) -> None:
"""
Handle paste from clipboard
"""
paste_text = get_app().clipboard.get_data().text
# replace CRLF with LF
paste_text = paste_text.replace(self.WINDOWS_LINE_ENDING,
self.UNIX_LINE_ENDING)
paste_text = paste_text.replace(self.CR_CHAR, self.UNIX_LINE_ENDING)
cursor_pos = self.text_field.buffer.cursor_position
for idx, char in enumerate(paste_text):
self.text_field.buffer.text += str(idx)
patch = self.doc.insert(cursor_pos + idx, char)
self.__register_patch(patch)
self.text_field.buffer.text = self.doc.text
self.text_field.buffer.cursor_position += len(paste_text)
def __get_selection(self, cut=False) -> Tuple[Document, ClipboardData]:
"""
Get selection from document selection and the resulting document.
If cut is true, remove selection from internal CRDT document
immediately.
:param cut: cut selected part of document
:type cut: bool
:return: resulting document and selection text fragment
"""
if self.text_field.document.selection:
cut_parts = []
remaining_parts = []
new_cursor_position = self.text_field.document.cursor_position
last_end = 0
for start, end in self.text_field.document.selection_ranges():
if cut:
# remove from internal doc
for pos in range(end, start, -1):
patch = self.doc.delete(pos - 1)
self.__register_patch(patch)
if last_end == 0:
new_cursor_position = start
remaining_parts.append(
self.text_field.document.text[last_end:start])
cut_parts.append(self.text_field.document.text[start:end])
last_end = end
remaining_parts.append(self.text_field.document.text[last_end:])
cut_text = "\n".join(cut_parts)
return (
Document(text=self.doc.text,
cursor_position=new_cursor_position),
ClipboardData(cut_text,
self.text_field.document.selection.type),
)
else:
return self.text_field.document, ClipboardData("")
def __init_bindings(self) -> KeyBindings:
"""
Generate bindings that handles KeyPress events, make changes to
internal doc and then displaying them in outer TextEdit buffer.
:return:
"""
bindings = KeyBindings()
@bindings.add('delete')
def handle_delete(event: KeyPressEvent) -> None:
"""
Captures Delete KeyPress event
and removes char next to cursor pos from internal Doc
"""
if not self.text_field.buffer.text:
return
if self.text_field.buffer.selection_state:
self.do_delete()
elif self.text_field.buffer.cursor_position \
!= len(self.text_field.buffer.text):
cursor_pos = self.text_field.buffer.cursor_position
patch = self.doc.delete(cursor_pos)
self.text_field.buffer.text = self.doc.text
self.__register_patch(patch)
@bindings.add('c-h')
def handle_backspace(event: KeyPressEvent) -> None:
"""
Captures Backspace KeyPress event
and removes char before cursor pos from internal Doc
"""
if not self.text_field.buffer.text or \
self.text_field.buffer.cursor_position == 0:
return
if self.text_field.buffer.selection_state:
self.do_delete()
else:
cursor_pos = self.text_field.buffer.cursor_position
patch = self.doc.delete(cursor_pos - 1)
if cursor_pos != len(self.text_field.buffer.text):
self.text_field.buffer.cursor_position -= 1
self.text_field.buffer.text = self.doc.text
self.__register_patch(patch)
@bindings.add('c-m')
def handle_enter(event: KeyPressEvent) -> None:
"""
Captures Enter KeyPress events and applies it to internal Doc
"""
if self.text_field.buffer.selection_state:
self.do_delete()
cursor_pos = self.text_field.buffer.cursor_position
patch = self.doc.insert(cursor_pos, self.UNIX_LINE_ENDING)
self.text_field.buffer.text = self.doc.text
self.text_field.buffer.cursor_position += 1
self.__register_patch(patch)
@bindings.add('c-i')
@bindings.add('<any>')
def handle_text_enter(event: KeyPressEvent) -> None:
"""
Captures General / Tab
KeyPress events and applies it to internal Doc
"""
if self.text_field.buffer.selection_state:
self.do_delete()
cursor_pos = self.text_field.buffer.cursor_position
patch = self.doc.insert(cursor_pos, event.data)
self.text_field.buffer.text = self.doc.text
self.text_field.buffer.cursor_right()
self.__register_patch(patch)
@bindings.add(Keys.BracketedPaste)
def handle_paste(event: KeyPressEvent) -> None:
"""
Handle paste event from terminal.
:param event:
:return:
"""
self.do_paste()
return bindings
def update_text(self, patch) -> None:
"""
Apply patch to internal document and update
TextEdit window buffer.
:param patch: raw patch
:type patch: str
"""
json_patch = json.loads(patch)
operation = json_patch["op"]
if operation == "d":
patch_pos = self.doc.get_real_position(patch)
self.doc.apply_patch(patch)
else:
self.doc.apply_patch(patch)
patch_pos = self.doc.get_real_position(patch)
old_pos = self.text_field.buffer.cursor_position
self.text_field.buffer.text = self.doc.text
if patch_pos == -1 or patch_pos > old_pos + 1:
self.text_field.buffer.cursor_position = old_pos
else:
if operation == "i":
self.text_field.buffer.cursor_right()
if operation == "d":
self.text_field.buffer.cursor_left()