-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.java
276 lines (232 loc) · 5.49 KB
/
Buffer.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package TextEditor;
import java.util.LinkedList;
import java.lang.StringBuilder;
class Buffer {
private Cursor cursor;
private LinkedList<StringBuilder> lines;
Buffer() {
this.cursor = new Cursor();
this.lines = new LinkedList<StringBuilder>();
lines.add(new StringBuilder(""));
}
/**
* Get Cursor Position.
*
* @return cursor
*/
public Cursor getCursor() {
return cursor;
}
/**
* Set Cursor Position.
*
* @param cursor
*/
public void setCursor(Cursor cursor) {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (cursorX >= 0 && cursorX <= getSize()) {
int linesLength = lines.get(cursorY).length();
if (cursorY >= 0 && cursorY <= linesLength) {
cursor.setX(cursorX);
cursor.setY(cursorY);
}
}
}
/**
* Set cursor to previous position.
*/
public void moveToPreviousCursor() {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (cursorX > 0 && (cursorY - 1) == 0) {
cursor.setX(cursorX - 1);
cursor.setY(lines.get(cursorX - 1).length());
} else if (cursorY - 1 > 0) {
cursor.setY(cursorY - 1);
}
}
/**
* Set cursor to next position.
*/
public void moveToNextCursor() {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (cursorY + 1 <= lines.get(cursorX).length()) {
cursor.setY(cursorY + 1);
} else if (cursorX + 1 < getSize()) {
cursor.setX(cursorX + 1);
cursor.setY(0);
}
}
/**
* Set cursor to the previous line.
*/
public void movePreviousLine() {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (cursorX > 0) {
cursor.setX(cursorX - 1);
int previousLength = lines.get(cursorX - 1).length();
if (cursorY < previousLength) {
cursor.setY(cursorY);
} else
cursor.setY(previousLength);
}
}
/**
* Set cursor to the next line.
*/
public void moveNextLine() {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (cursorX + 1 < getSize()) {
cursor.setX(cursorX + 1);
int nextLength = lines.get(cursorX + 1).length();
if (cursorY < nextLength) {
cursor.setY(cursorY);
} else
cursor.setY(nextLength);
}
}
/**
* Set cursor to between lines.
*/
public void moveToBetweenLine() {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (cursorX + 1 < getSize()) {
cursor.setX(cursorX);
int nextLength = lines.get(cursorX + 1).length();
if (cursorY < nextLength) {
cursor.setY(cursorY);
} else
cursor.setY(nextLength);
}
}
/**
* Set cursor to the beginning of the line.
*/
public void moveToStartLine() {
getCursor().setY(0);
}
/**
* Set cursor to the end of the line.
*/
public void moveToEndLine() {
String line = getNthLine(getCursor().getX()).toString();
getCursor().setY(line.length());
}
/**
* Get buffer.
*
* @return LinkedList
*/
public LinkedList<StringBuilder> getAllLines() {
return lines;
}
/**
* Get buffer size.
*
* @return int
*/
public int getSize() {
return lines.size();
}
/**
* Get nth line.
*
* @param pos
* @return StringBuilder
*/
public StringBuilder getNthLine(int pos) {
if (pos >= 0 && pos < lines.size())
return lines.get(pos);
throw new IndexOutOfBoundsException();
}
/**
* Insert string into the buffer.
*
* @param str
*/
public void insertString(String str) {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (!str.contains("\n")) {
cursor.setY(cursorY + str.length());
StringBuilder currentLine = lines.get(cursorX);
currentLine.insert(cursorY, str);
} else
throw new IllegalArgumentException();
}
/**
* Insert line (\n).
*
*/
public void insertLine() {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
StringBuilder currentLine = lines.get(cursorX);
StringBuilder nextLine = new StringBuilder(new String(currentLine.substring(cursorY, currentLine.length())));
currentLine.delete(cursorY, currentLine.length());
lines.add(cursorX + 1, nextLine);
cursor.setX(cursorX + 1);
cursor.setY(0);
}
/**
* Delete line from the buffer.
*
*/
private void deleteLine() {
int cursorX = cursor.getX();
StringBuilder previousLine = lines.get(cursorX - 1);
int previousLineLength = previousLine.length();
StringBuilder currentLine = lines.get(cursorX);
previousLine.insert(previousLineLength, currentLine);
lines.remove(cursorX);
cursor.setX(cursorX - 1);
cursor.setY(previousLineLength);
}
/**
* Folds line to the next position.
*/
public void foldLine() {
int cursorX = cursor.getX();
StringBuilder currentLine = lines.get(cursorX);
StringBuilder nextLine = new StringBuilder(new String(currentLine.substring(currentLine.lastIndexOf(" ") + 1)));
currentLine.delete(currentLine.lastIndexOf(" "), currentLine.length());
lines.add(cursorX + 1, nextLine);
cursor.setX(cursorX + 1);
cursor.setY(nextLine.length());
}
/**
* Insert char into the String.
*
* @param character
*/
public void insertChar(Character character) {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (!(character == '\n')) {
cursor.setY(cursorY + 1);
StringBuilder currentLine = lines.get(cursorX);
currentLine.insert(cursorY, character);
} else
insertLine();
}
/**
* Delete char from the buffer.
*
*/
public void deleteChar() {
int cursorX = cursor.getX();
int cursorY = cursor.getY();
if (cursorY > 0) {
StringBuilder currentLine = lines.get(cursorX);
currentLine.deleteCharAt(cursorY - 1);
cursor.setY(cursorY - 1);
} else if (cursorX != 0) {
deleteLine();
}
}
}