1
0

edit.go 2.3 KB

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