edit.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package buffer
  2. type Edit struct {
  3. Offset int
  4. Deleted []byte
  5. Inserted []byte
  6. }
  7. func (e Edit) Invert() Edit {
  8. return Edit{
  9. Offset: e.Offset,
  10. Deleted: e.Inserted,
  11. Inserted: e.Deleted,
  12. }
  13. }
  14. func (buf *Buffer) apply(e Edit) {
  15. buf.ensureRope()
  16. if len(e.Deleted) > 0 {
  17. end := e.Offset + len(e.Deleted)
  18. buf.Rope.Remove(e.Offset, end)
  19. buf.LI.RemoveAt(e.Offset, end)
  20. }
  21. if len(e.Inserted) > 0 {
  22. buf.Rope.Insert(e.Offset, e.Inserted)
  23. buf.LI.InsertAt(e.Offset, e.Inserted)
  24. }
  25. }
  26. type BufferTransformation struct {
  27. Edits []Edit
  28. CursorsBefore []Cursor
  29. PrimaryBefore int
  30. CursorsAfter []Cursor
  31. PrimaryAfter int
  32. }
  33. type UndoStack struct {
  34. done []BufferTransformation
  35. undone []BufferTransformation
  36. }
  37. func (s *UndoStack) push(t BufferTransformation) {
  38. s.done = append(s.done, t)
  39. s.undone = s.undone[:0]
  40. }
  41. func (s *UndoStack) CanUndo() bool {
  42. return len(s.done) > 0
  43. }
  44. func (s *UndoStack) CanRedo() bool {
  45. return len(s.undone) > 0
  46. }
  47. func cloneCursors(cs []Cursor) []Cursor {
  48. out := make([]Cursor, len(cs))
  49. copy(out, cs)
  50. return out
  51. }
  52. func (buf *Buffer) begin() (cursorsBefore []Cursor, primaryBefore int) {
  53. return cloneCursors(buf.CM.Cursors), buf.CM.PrimaryIdx
  54. }
  55. func (buf *Buffer) commit(cursorsBefore []Cursor, primaryBefore int, edits []Edit) {
  56. if len(edits) == 0 {
  57. return
  58. }
  59. buf.History.push(BufferTransformation{
  60. Edits: edits,
  61. CursorsBefore: cursorsBefore,
  62. PrimaryBefore: primaryBefore,
  63. CursorsAfter: cloneCursors(buf.CM.Cursors),
  64. PrimaryAfter: buf.CM.PrimaryIdx,
  65. })
  66. }
  67. func (buf *Buffer) Undo() bool {
  68. if !buf.History.CanUndo() {
  69. return false
  70. }
  71. t := buf.History.done[len(buf.History.done)-1]
  72. buf.History.done = buf.History.done[:len(buf.History.done)-1]
  73. for i := len(t.Edits) - 1; i >= 0; i-- {
  74. buf.apply(t.Edits[i].Invert())
  75. }
  76. buf.CM.Cursors = cloneCursors(t.CursorsBefore)
  77. buf.CM.PrimaryIdx = t.PrimaryBefore
  78. buf.History.undone = append(buf.History.undone, t)
  79. return true
  80. }
  81. func (buf *Buffer) Redo() bool {
  82. if !buf.History.CanRedo() {
  83. return false
  84. }
  85. t := buf.History.undone[len(buf.History.undone)-1]
  86. buf.History.undone = buf.History.undone[:len(buf.History.undone)-1]
  87. for _, e := range t.Edits {
  88. buf.apply(e)
  89. }
  90. buf.CM.Cursors = cloneCursors(t.CursorsAfter)
  91. buf.CM.PrimaryIdx = t.PrimaryAfter
  92. buf.History.done = append(buf.History.done, t)
  93. return true
  94. }