-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistory.cs
More file actions
114 lines (94 loc) · 2.74 KB
/
History.cs
File metadata and controls
114 lines (94 loc) · 2.74 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
namespace maple
{
public enum HistoryEventType
{
Add,
Remove,
AddLine,
RemoveLine,
AddSelection,
RemoveSelection,
IndentLine,
DeindentLine,
}
public class History
{
private List<HistoryEvent> events = new();
private List<HistoryEvent> redoEvents = new();
private static ReaderWriterLock rwLock = new ReaderWriterLock();
private const int WriterLockTimeout = 500;
public History() { }
public void PushEvent(HistoryEvent e)
{
events.Add(e);
redoEvents.Clear();
}
public HistoryEvent PopEvent()
{
HistoryEvent e = events[^1];
events.RemoveAt(events.Count - 1);
redoEvents.Add(new HistoryEvent(
e.EventType,
e.TextDelta,
e.DeltaPos,
new Point(Editor.DocCursor.DX, Editor.DocCursor.DY),
e.SelectionPoints,
e.Combined
));
return e;
}
public bool HasNext()
{
return events.Count > 0;
}
public HistoryEvent PopRedoEvent()
{
HistoryEvent e = redoEvents[^1];
redoEvents.RemoveAt(redoEvents.Count - 1);
events.Add(new HistoryEvent(
e.EventType,
e.TextDelta,
e.DeltaPos,
new Point(Editor.DocCursor.DX, Editor.DocCursor.DY),
e.SelectionPoints,
e.Combined
));
return e;
}
public bool HasNextRedo()
{
return redoEvents.Count > 0;
}
public bool NextRedoCombined()
{
return redoEvents.Count > 0 && redoEvents[^1].Combined;
}
public void Clear()
{
events.Clear();
redoEvents.Clear();
}
}
public struct HistoryEvent
{
public HistoryEventType EventType { get; set; }
public string TextDelta { get; set; }
public Point DeltaPos { get; set; }
public Point CursorPos { get; set; }
public Point[] SelectionPoints { get; set; }
public bool Combined { get; set; }
public HistoryEvent(HistoryEventType eventType, string textDelta, Point deltaPos, Point cursorPos, Point[] selectionPoints = null, bool combined = false)
{
EventType = eventType;
TextDelta = textDelta;
DeltaPos = deltaPos;
CursorPos = cursorPos;
SelectionPoints = selectionPoints;
Combined = combined;
}
}
}