ultron-core 0.4.0

The core component of ultron editor
Documentation
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use nalgebra::Point2;
use ultron_core::TextBuffer;

#[test]
fn line_length() {
    let raw = "Hello world";
    let buffer = TextBuffer::from_str(raw);
    assert_eq!(buffer.total_lines(), 1);
    assert_eq!(buffer.line_width(0), 11);
}

#[test]
fn cjk_length() {
    let raw = "CJK文件";
    let buffer = TextBuffer::from_str(raw);
    assert_eq!(buffer.total_chars(), 5);
    assert_eq!(buffer.total_lines(), 1);
    assert_eq!(buffer.line_width(0), 7);
}

#[test]
fn test_get_char() {
    let raw = "Hello world";
    let buffer = TextBuffer::from_str(raw);
    assert_eq!(buffer.get_char(Point2::new(6, 0)), Some('w'));
}

#[test]
fn test_get_text() {
    let raw = "Hello world";
    let buffer = TextBuffer::from_str(raw);
    let txt = buffer.get_text_in_linear_mode(Point2::new(0, 0), Point2::new(4, 0));
    assert_eq!(txt, "Hello");
}

#[test]
fn test_get_text_world() {
    let raw = "Hello world";
    let buffer = TextBuffer::from_str(raw);
    let txt = buffer.get_text_in_linear_mode(Point2::new(6, 0), Point2::new(10, 0));
    assert_eq!(txt, "world");
}

#[test]
fn test_get_text_with_cjk() {
    let raw = "Hello 文件系统 world";
    let buffer = TextBuffer::from_str(raw);
    let txt = buffer.get_text_in_linear_mode(Point2::new(8, 0), Point2::new(10, 0));
    assert_eq!(txt, "件系");
}

#[test]
fn test_cut_text_with_cjk() {
    let raw = "Hello 文件系统 world";
    let mut buffer = TextBuffer::from_str(raw);
    let txt = buffer.cut_text_in_linear_mode(Point2::new(8, 0), Point2::new(10, 0));
    assert_eq!(txt, "件系");
    assert_eq!(buffer.to_string(), "Hello 文统 world");
}

#[test]
fn test_get_text_with_cjk_world() {
    let raw = "Hello 文件系统 world";
    let buffer = TextBuffer::from_str(raw);
    let txt = buffer.get_text_in_linear_mode(Point2::new(15, 0), Point2::new(19, 0));
    assert_eq!(txt, "world");
}

#[test]
fn test_cut_text_with_cjk_world() {
    let raw = "Hello 文件系统 world";
    let mut buffer = TextBuffer::from_str(raw);
    let txt = buffer.cut_text_in_linear_mode(Point2::new(15, 0), Point2::new(19, 0));
    assert_eq!(txt, "world");
    assert_eq!(buffer.to_string(), "Hello 文件系统 ");
}

#[test]
fn test_get_text_multi_line_world() {
    let raw = "Hello\nworld and\neverywhere";
    let buffer = TextBuffer::from_str(raw);
    let txt = buffer.get_text_in_linear_mode(Point2::new(0, 1), Point2::new(4, 1));
    assert_eq!(txt, "world");
}

#[test]
fn test_get_text_multi_line_and() {
    let raw = "Hello\nworld and\neverywhere";
    let buffer = TextBuffer::from_str(raw);
    let txt = buffer.get_text_in_linear_mode(Point2::new(6, 1), Point2::new(8, 1));
    assert_eq!(txt, "and");
}

#[test]
fn test_get_text_multi_line_and_every() {
    let raw = "Hello\nworld and\neverywhere";
    let buffer = TextBuffer::from_str(raw);
    let txt = buffer.get_text_in_linear_mode(Point2::new(6, 1), Point2::new(4, 2));
    assert_eq!(txt, "and\nevery");
}

#[test]
fn delete_last_char() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.delete_char(Point2::new(10, 0));
    assert_eq!(buffer.to_string(), "Hello worl");
}

#[test]
fn delete_first_char() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.delete_char(Point2::new(0, 0));
    assert_eq!(buffer.to_string(), "ello world");
}

#[test]
fn delete_5th_char() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.delete_char(Point2::new(5, 0));
    assert_eq!(buffer.to_string(), "Helloworld");
}

#[test]
fn delete_in_2nd_line() {
    let raw = "Hello\nworld\nthere";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.delete_char(Point2::new(0, 2));
    assert_eq!(buffer.to_string(), "Hello\nworld\nhere");
}

#[test]
fn break_line() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(5, 0));
    assert_eq!(buffer.to_string(), "Hello\n world");
}

#[test]
fn break_line_then_insert_1() {
    let raw = "Hello world\n\nHowdy";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(0, 1));
    assert_eq!(buffer.to_string(), "Hello world\n\n\nHowdy");
    buffer.insert_char(Point2::new(0, 1), '1');
    assert_eq!(buffer.to_string(), "Hello world\n1\n\nHowdy");
}

#[test]
fn break_line_in_non_existing_cell() {
    let raw = "Hello world\n\nHowdy";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(2, 1));
    assert_eq!(buffer.to_string(), "Hello world\n  \n\nHowdy");
    buffer.insert_char(Point2::new(0, 2), '1');
    assert_eq!(buffer.to_string(), "Hello world\n  \n1\nHowdy");
}

#[test]
fn break_line_then_insert_1_below() {
    let raw = "Hello world\n\nHowdy";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(0, 1));
    assert_eq!(buffer.to_string(), "Hello world\n\n\nHowdy");
    buffer.insert_char(Point2::new(0, 2), '1');
    assert_eq!(buffer.to_string(), "Hello world\n\n1\nHowdy");
}

#[test]
fn break_line_then_insert_2_below() {
    let raw = "Hello world\n\nHowdy";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(0, 1));
    buffer.break_line(Point2::new(0, 1));
    assert_eq!(buffer.to_string(), "Hello world\n\n\n\nHowdy");
    buffer.insert_char(Point2::new(0, 3), '2');
    assert_eq!(buffer.to_string(), "Hello world\n\n\n2\nHowdy");
}

#[test]
fn join_line() {
    let raw = "Hello\n world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.join_line(Point2::new(0, 0));
    assert_eq!(buffer.to_string(), "Hello world");
}

#[test]
fn ensure_line_exist5() {
    let mut buffer = TextBuffer::from_str("");
    buffer.ensure_line_exist(5);
    assert_eq!(buffer.total_lines(), 6);
}

#[test]
fn ensure_cell_exist5_2() {
    let mut buffer = TextBuffer::from_str("");
    buffer.ensure_cell_exist(Point2::new(5, 2));
    assert_eq!(buffer.to_string(), "\n\n      ");
}

#[test]
fn ensure_cell_exist0_0() {
    let mut buffer = TextBuffer::from_str("");
    buffer.ensure_cell_exist(Point2::new(0, 0));
    assert_eq!(buffer.to_string(), " ");
}

#[test]
fn insert_5_lines() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(5, 0));
    buffer.break_line(Point2::new(0, 1));
    buffer.break_line(Point2::new(0, 1));
    buffer.break_line(Point2::new(0, 1));
    buffer.break_line(Point2::new(0, 1));
    assert_eq!(buffer.to_string(), "Hello\n\n\n\n\n world");
}

#[test]
fn join_5_lines() {
    let raw = "Hello\n\n\n\n\n world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.join_line(Point2::new(0, 0));
    buffer.join_line(Point2::new(0, 0));
    buffer.join_line(Point2::new(0, 0));
    buffer.join_line(Point2::new(0, 0));
    buffer.join_line(Point2::new(0, 0));
    assert_eq!(buffer.to_string(), "Hello world");
}

#[test]
fn insert_new_line_at_start() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(0, 0));
    assert_eq!(buffer.to_string(), "\nHello world");
}

#[test]
fn insert_new_line_at_end() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.break_line(Point2::new(11, 0));
    assert_eq!(buffer.to_string(), "Hello world\n");
}

#[test]
fn insert_anywhere_col() {
    let mut buffer = TextBuffer::from_str("");
    buffer.insert_char(Point2::new(5, 0), 'Y');
    assert_eq!(buffer.to_string(), "     Y");
}

#[test]
fn insert_anywhere_line() {
    let mut buffer = TextBuffer::from_str("");
    buffer.insert_char(Point2::new(0, 5), 'Y');
    assert_eq!(buffer.to_string(), "\n\n\n\n\nY");
}

#[test]
fn insert_anywhere_cell() {
    let mut buffer = TextBuffer::from_str("");
    buffer.insert_char(Point2::new(2, 5), 'Y');
    assert_eq!(buffer.to_string(), "\n\n\n\n\n  Y");
}

#[test]
fn insert_anywhere_cell_10_10() {
    let mut buffer = TextBuffer::from_str("");
    buffer.insert_char(Point2::new(10, 10), 'Y');
    assert_eq!(buffer.to_string(), "\n\n\n\n\n\n\n\n\n\n          Y");
}

#[test]
fn breaking_line_anywhere_will_make_lines_on_it() {
    let mut buffer = TextBuffer::from_str("");
    buffer.break_line(Point2::new(2, 5));
    assert_eq!(buffer.to_string(), "\n\n\n\n\n  \n");
}

#[test]
fn lines_2() {
    let raw = "Hello\nworld";
    let buffer = TextBuffer::from_str(raw);
    assert_eq!(buffer.total_lines(), 2);
    assert_eq!(buffer.line_width(0), 5);
    assert_eq!(buffer.line_width(1), 5);
    assert_eq!(buffer.line_width(2), 0);
}

#[test]
fn cjk() {
    let raw = "Hello 文件系统";
    let buffer = TextBuffer::from_str(raw);
    assert_eq!(buffer.total_lines(), 1);
    assert_eq!(buffer.line_width(0), 14);
}

#[test]
fn insert_end_cjk() {
    let raw = "Hello 文件系统";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_char(Point2::new(14, 0), 'Y');
    assert_eq!(buffer.to_string(), "Hello 文件系统Y");
}

#[test]
fn insert_end_cjk_same_insert_on_13th_or_14th() {
    let raw = "Hello 文件系统";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_char(Point2::new(14, 0), 'Y');
    assert_eq!(buffer.to_string(), "Hello 文件系统Y");
}

#[test]
fn insert_end_cjk_but_not_15th() {
    let raw = "Hello 文件系统";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_char(Point2::new(15, 0), 'Y');
    assert_eq!(buffer.to_string(), "Hello 文件系统 Y");
}

#[test]
fn replace_start() {
    let raw = "Hello";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.replace_char(Point2::new(0, 0), 'Y');
    assert_eq!(buffer.to_string(), "Yello");
}

#[test]
fn replace_middle() {
    let raw = "Hello";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.replace_char(Point2::new(2, 0), 'Y');
    assert_eq!(buffer.to_string(), "HeYlo");
}

#[test]
fn replace_end() {
    let raw = "Hello";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.replace_char(Point2::new(4, 0), 'Y');
    assert_eq!(buffer.to_string(), "HellY");
}

#[test]
fn replace_char_on_next_page() {
    let raw = "Hello";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.replace_char(Point2::new(4, 30), 'Y');
    assert_eq!(
        buffer.to_string(),
        "Hello\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n    Y"
    );
}

#[test]
fn insert_start() {
    let raw = "Hello";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_char(Point2::new(0, 0), 'Y');
    assert_eq!(buffer.to_string(), "YHello");
}

#[test]
fn insert_middle() {
    let raw = "Hello";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_char(Point2::new(2, 0), 'Y');
    assert_eq!(buffer.to_string(), "HeYllo");
}

#[test]
fn insert_end() {
    let raw = "Hello";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_char(Point2::new(5, 0), 'Y');
    assert_eq!(buffer.to_string(), "HelloY");
}

#[test]
fn test_cut_text() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    let txt = buffer.cut_text_in_linear_mode(Point2::new(0, 0), Point2::new(4, 0));
    assert_eq!(txt, "Hello");
    assert_eq!(buffer.to_string(), " world");
}

#[test]
fn test_cut_text_multi_line() {
    let raw = "before text\nHello world\nafter text";
    let mut buffer = TextBuffer::from_str(raw);
    let txt = buffer.cut_text_in_linear_mode(Point2::new(0, 1), Point2::new(4, 1));
    assert_eq!(txt, "Hello");
    assert_eq!(buffer.to_string(), "before text\n world\nafter text");
}

#[test]
// before text
// Hello world
// after text
//
// before text
// text
fn test_cut_text_2lines_multi_line() {
    let raw = "before text\nHello world\nafter text";
    let mut buffer = TextBuffer::from_str(raw);
    let txt = buffer.cut_text_in_linear_mode(Point2::new(0, 1), Point2::new(4, 2));
    assert_eq!(txt, "Hello world\nafter");
    //FIXME: There should only be 1 \n here
    assert_eq!(buffer.to_string(), "before text\n\n text");
}

// before text
// Hello world
// after text
//
// world
// text
//
// before text
// Hello
// after
//
//#[test]
//FIXME:
fn test_cut_text_2lines_multi_line_block_mode() {
    let raw = "before text\nHello world\nafter text";
    let mut buffer = TextBuffer::from_str(raw);
    let txt = buffer.cut_text_in_linear_mode(Point2::new(6, 1), Point2::new(10, 2));
    assert_eq!(txt, "world\ntext");
    assert_eq!(buffer.to_string(), "before text\nHello \nafter ");
}

#[test]
fn test_insert_text() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_text(Point2::new(5, 0), "YYYY");
    assert_eq!(buffer.to_string(), "HelloYYYY world");
}

//#[test]
//FIXME:
fn test_insert_multi_line_text() {
    let raw = "Hello world";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_text(Point2::new(5, 0), "XXXX\nYYYY");
    assert_eq!(buffer.to_string(), "HelloXXXX\nYYYY world");
}

//#[test]
//FIXME:
fn test_insert_multi_line_text_to_multi_line_text() {
    let raw = "before text\nHello world\nafter text";
    let mut buffer = TextBuffer::from_str(raw);
    buffer.insert_text(Point2::new(5, 1), "XXXX\nYYYY");
    assert_eq!(
        buffer.to_string(),
        "before text\nHelloXXXX\nYYYY world\nafter text"
    );
}

#[test]
fn test_get_text_block_mode() {
    let raw = "0000\n01234 Hello 5678\n01234 world 5678\n01234 wazup 5678\n0000";
    let buffer = TextBuffer::from_str(raw);
    let selection = buffer.get_text_in_block_mode(Point2::new(6, 1), Point2::new(10, 3));
    assert_eq!(selection, "Hello\nworld\nwazup");
    assert_eq!(raw, buffer.to_string());
}

#[test]
fn test_cut_text_block_mode() {
    let raw = "0000\n01234 Hello 5678\n01234 world 5678\n01234 wazup 5678\n0000";
    let mut buffer = TextBuffer::from_str(raw);
    let selection = buffer.cut_text_in_block_mode(Point2::new(6, 1), Point2::new(10, 3));
    assert_eq!(selection, "Hello\nworld\nwazup");
    assert_eq!(
        buffer.to_string(),
        "0000\n01234  5678\n01234  5678\n01234  5678\n0000"
    );
}

#[test]
fn test_split_line_at_point() {
    let raw = "Hello world";
    let text_buffer = TextBuffer::from_str(raw);
    let (first, second) = text_buffer.split_line_at_point(Point2::new(5, 0));
    assert_eq!(first, "Hello");
    assert_eq!(second, " world");
}

#[test]
fn test_split_line_at_2_points() {
    let raw = "Hello world and everywhere";
    let text_buffer = TextBuffer::from_str(raw);
    let (first, second, third) =
        text_buffer.split_line_at_2_points(Point2::new(5, 0), Point2::new(15, 0));
    assert_eq!(first, "Hello");
    assert_eq!(second, " world and");
    assert_eq!(third, " everywhere");
}