Module: Yatte::UndoReplayer

Defined in:
lib/yatte/undo_replayer.rb

Class Method Summary collapse

Class Method Details

.apply_redo(action, buffer, cursor) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yatte/undo_replayer.rb', line 19

def apply_redo(action, buffer, cursor)
  case action.type
  when :insert
    end_row, end_col = buffer.insert_text(action.row, action.col, action.text)
    cursor.set(end_row, end_col)
  when :delete
    end_row, end_col = compute_end(action)
    buffer.delete_range(action.row, action.col, end_row, end_col)
    cursor.set(action.row, action.col)
  end
end

.apply_undo(action, buffer, cursor) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/yatte/undo_replayer.rb', line 7

def apply_undo(action, buffer, cursor)
  case action.type
  when :insert
    end_row, end_col = compute_end(action)
    buffer.delete_range(action.row, action.col, end_row, end_col)
    cursor.set(action.cursor_row, action.cursor_col)
  when :delete
    buffer.insert_text(action.row, action.col, action.text)
    cursor.set(action.cursor_row, action.cursor_col)
  end
end

.compute_end(action) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/yatte/undo_replayer.rb', line 31

def compute_end(action)
  lines = action.text.split("\n", -1)
  end_row = action.row + lines.length - 1
  end_col = if lines.length == 1
    action.col + action.text.length
  else
    lines.last.length
  end
  [end_row, end_col]
end