Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.1, 1.1.1, and 1.1.2

textview: support undoable 'replace' operation

When set_text() API of UndoableTextBuffer is called, it recognizes
'replace' operation as a combined operation of 'delete' and 'insert'.
To undo/redo 'replace' operation, I needed to introduce UndoableReplace
object which consists of UndoableInsert and UndoableDelete objects.

When 'insert'/'delete' operation is happened, UndoableReplace object
will be generated if the previous object is 'delete'/'insert' operation
and has same time stamp.

Changeset d411b35c5d13

Parent 0e1796d258f2

by Yuki KODAMA

Changes to one file · Browse files at d411b35c5d13 Showing diff from parent 0e1796d258f2 Diff from another changeset...

 
7
8
9
 
10
11
12
13
14
15
 
16
17
18
 
24
25
26
 
27
28
29
 
39
40
41
 
 
 
 
 
42
43
44
 
95
96
97
98
 
 
 
 
99
100
101
 
141
142
143
144
 
 
 
 
145
146
147
 
179
180
181
182
183
 
 
184
185
 
186
187
188
189
190
191
192
193
 
 
 
 
 
194
195
196
 
 
 
 
 
 
 
 
 
 
197
198
199
 
207
208
209
210
211
212
 
 
 
213
214
 
215
216
217
218
219
 
 
 
220
221
 
 
 
 
 
 
 
 
 
 
222
223
224
 
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
26
27
28
29
30
31
32
 
42
43
44
45
46
47
48
49
50
51
52
 
103
104
105
 
106
107
108
109
110
111
112
 
152
153
154
 
155
156
157
158
159
160
161
 
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
 
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
256
257
258
@@ -7,12 +7,14 @@
 # GNU General Public License version 2, incorporated herein by reference.    import gtk +import gobject    from tortoisehg.hgtk import gtklib    class UndoableInsert(object):   """something that has been inserted into our textbuffer"""   def __init__(self, text_iter, text, length): + self.time = gobject.get_current_time()   self.offset = text_iter.get_offset()   self.text = text   self.length = length @@ -24,6 +26,7 @@
 class UndoableDelete(object):   """something that has ben deleted from our textbuffer"""   def __init__(self, text_buffer, start_iter, end_iter): + self.time = gobject.get_current_time()   self.text = text_buffer.get_text(start_iter, end_iter)   self.start = start_iter.get_offset()   self.end = end_iter.get_offset() @@ -39,6 +42,11 @@
  else:   self.mergeable = True   +class UndoableReplace(object): + def __init__(self, first, second): + self.first = first + self.second = second +  class UndoableTextBuffer(gtk.TextBuffer):   """text buffer with added undo capabilities   @@ -95,7 +103,10 @@
  self.undo_stack.append(undo_action)   return   if not isinstance(prev_insert, UndoableInsert): - self.undo_stack.append(prev_insert) + if prev_insert.time == undo_action.time: + undo_action = UndoableReplace(prev_insert, undo_action) + else: + self.undo_stack.append(prev_insert)   self.undo_stack.append(undo_action)   return   if can_be_merged(prev_insert, undo_action): @@ -141,7 +152,10 @@
  self.undo_stack.append(undo_action)   return   if not isinstance(prev_delete, UndoableDelete): - self.undo_stack.append(prev_delete) + if prev_delete.time == undo_action.time: + undo_action = UndoableReplace(prev_delete, undo_action) + else: + self.undo_stack.append(prev_delete)   self.undo_stack.append(undo_action)   return   if can_be_merged(prev_delete, undo_action): @@ -179,21 +193,31 @@
  self.undo_in_progress = True   undo_action = self.undo_stack.pop()   self.redo_stack.append(undo_action) - if isinstance(undo_action, UndoableInsert): - start = self.get_iter_at_offset(undo_action.offset) + def do_insert(action): + start = self.get_iter_at_offset(action.offset)   stop = self.get_iter_at_offset( - undo_action.offset + undo_action.length + action.offset + action.length   )   self.delete(start, stop)   self.place_cursor(start) - else: - start = self.get_iter_at_offset(undo_action.start) - self.insert(start, undo_action.text) - stop = self.get_iter_at_offset(undo_action.end) - if undo_action.delete_key_used: + def do_delete(action): + start = self.get_iter_at_offset(action.start) + self.insert(start, action.text) + stop = self.get_iter_at_offset(action.end) + if action.delete_key_used:   self.place_cursor(start)   else:   self.place_cursor(stop) + def do(action): + if isinstance(action, UndoableInsert): + do_insert(action) + elif isinstance(action, UndoableDelete): + do_delete(action) + if isinstance(undo_action, UndoableReplace): + do(undo_action.second) + do(undo_action.first) + else: + do(undo_action)   self.end_not_undoable_action()   self.undo_in_progress = False   @@ -207,18 +231,28 @@
  self.undo_in_progress = True   redo_action = self.redo_stack.pop()   self.undo_stack.append(redo_action) - if isinstance(redo_action, UndoableInsert): - start = self.get_iter_at_offset(redo_action.offset) - self.insert(start, redo_action.text) + def do_insert(action): + start = self.get_iter_at_offset(action.offset) + self.insert(start, action.text)   new_cursor_pos = self.get_iter_at_offset( - redo_action.offset + redo_action.length + action.offset + action.length   )   self.place_cursor(new_cursor_pos) - else: - start = self.get_iter_at_offset(redo_action.start) - stop = self.get_iter_at_offset(redo_action.end) + def do_delete(action): + start = self.get_iter_at_offset(action.start) + stop = self.get_iter_at_offset(action.end)   self.delete(start, stop)   self.place_cursor(start) + def do(action): + if isinstance(action, UndoableInsert): + do_insert(action) + elif isinstance(action, UndoableDelete): + do_delete(action) + if isinstance(redo_action, UndoableReplace): + do(redo_action.first) + do(redo_action.second) + else: + do(redo_action)   self.end_not_undoable_action()   self.undo_in_progress = False