-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorRow.cs
174 lines (141 loc) · 4.32 KB
/
EditorRow.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
class EditorRow
{
private readonly EditorSettings editorSettings;
private readonly List<char> rawChars = new List<char>();
private readonly List<char> renderChars = new List<char>();
private HighlightMode[] highlightModes = Array.Empty<HighlightMode>();
public EditorRow(EditorSettings editorSettings, int index, IEnumerable<char> chars)
{
this.editorSettings = editorSettings;
RowIndex = index;
rawChars.AddRange(chars);
}
public int RowIndex { get; set; }
public IReadOnlyList<char> Chars { get { return rawChars.AsReadOnly(); } }
public int CharSize
{
get { return rawChars.Count; }
}
public IReadOnlyList<char> RenderChars { get { return renderChars.AsReadOnly(); } }
public IReadOnlyList<HighlightMode> HighlightModes { get { return highlightModes.AsReadOnly(); } }
public int RenderSize
{
get { return renderChars.Count; }
}
public bool HasHighlightOpenComment { get; set; }
public void InsertCharAt(int index, char ch)
{
rawChars.Insert(index, ch);
}
public void RemoveCharAt(int index)
{
rawChars.RemoveAt(index);
}
public void Append(IEnumerable<char> newChars)
{
rawChars.AddRange(newChars);
}
public void Truncate(int startIndex)
{
if (startIndex < rawChars.Count)
{
rawChars.RemoveRange(startIndex, rawChars.Count - startIndex);
}
}
public int GetRenderIndex(int charIndex)
{
int renderIndex = 0;
for (int ii = 0; ii < rawChars.Count; ii += 1)
{
if (ii == charIndex)
{
return renderIndex;
}
if (rawChars[ii] == '\t')
{
renderIndex += (editorSettings.KILO_TAB_STOP - 1) - (renderIndex % editorSettings.KILO_TAB_STOP);
}
renderIndex += 1;
}
return renderIndex;
}
public int Find(string query)
{
return new string(rawChars.ToArray()).IndexOf(query, StringComparison.OrdinalIgnoreCase);
}
public bool StartsWithAtPosition(string input, int position)
{
return CompareOrdinalString(renderChars, position, input.Length, input);
}
public void SetHighlightAt(int index, HighlightMode highlightMode)
{
highlightModes[index] = highlightMode;
}
public void SetHighlightRange(int startIndex, HighlightMode highlightMode)
{
for (int ii = startIndex; ii < highlightModes.Length; ii += 1)
{
highlightModes[ii] = highlightMode;
}
}
public void SetHighlightRange(int startIndex, int length, HighlightMode highlightMode)
{
for (int ii = startIndex; ii < startIndex + length && ii < highlightModes.Length; ii += 1)
{
highlightModes[ii] = highlightMode;
}
}
public void UpdateRow()
{
renderChars.Clear();
foreach (var ch in rawChars)
{
if (ch == '\t')
{
renderChars.Add(' ');
while ((renderChars.Count % editorSettings.KILO_TAB_STOP) != 0)
{
renderChars.Add(' ');
}
}
else
{
renderChars.Add(ch);
}
}
if (highlightModes.Length != renderChars.Count)
{
highlightModes = new HighlightMode[renderChars.Count];
}
for (int ii = 0; ii < renderChars.Count; ii += 1)
{
highlightModes[ii] = HighlightMode.Normal;
}
}
private bool CompareOrdinalString(IReadOnlyList<char> source, int sourceStartIndex, int sourceLength, string target)
{
if (target.Length == 0 || sourceLength == 0)
{
return false;
}
if (sourceLength != target.Length)
{
return false;
}
bool isEqual = false;
for (int index = 0; index < target.Length; index += 1)
{
if (sourceStartIndex + index < source.Count)
{
if (source[sourceStartIndex + index] == target[index])
{
isEqual = true;
continue;
}
}
isEqual = false;
break;
}
return isEqual;
}
}