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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use enso_prelude::*;
use serde::Deserialize;
use serde::Serialize;
use std::ops::Add;
use std::ops::AddAssign;
use std::ops::Range;
use std::ops::Sub;
use std::ops::SubAssign;
#[allow(missing_docs)]
#[derive(Clone,Copy,Debug,Default,Hash,PartialEq,Eq,PartialOrd,Ord,Serialize,Deserialize)]
pub struct Index { pub value:usize }
impl Index {
pub fn new(value:usize) -> Self {
Index {value}
}
pub fn convert_byte_index(content:impl Str, index:ByteIndex) -> Self {
let slice = &content.as_ref()[..index.value];
Self::new(slice.chars().count())
}
pub fn checked_sub(self, rhs:Size) -> Option<Self> {
self.value.checked_sub(rhs.value).map(Self::new)
}
}
impl Display for Index {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"{}",self.value)
}
}
#[allow(missing_docs)]
#[derive(Clone,Copy,Debug,Default,Hash,PartialEq,Eq,PartialOrd,Ord,Serialize,Deserialize)]
pub struct ByteIndex { pub value:usize }
impl ByteIndex {
pub fn new(value:usize) -> Self {
ByteIndex {value}
}
pub fn new_range(value:Range<usize>) -> Range<Self> {
ByteIndex::new(value.start) .. ByteIndex::new(value.end)
}
pub fn next(self) -> Self {
ByteIndex {value: self.value + 1}
}
}
#[allow(missing_docs)]
#[derive(Clone,Copy,Debug,Default,Hash,PartialEq,Eq,PartialOrd,Ord,Serialize,Deserialize)]
pub struct Size { pub value:usize }
impl Size {
pub fn new(value:usize) -> Self {
Size {value}
}
pub fn from_text(value:impl AsRef<str>) -> Self {
Size::new(value.as_ref().chars().count())
}
pub fn non_empty(self) -> bool {
self.value > 0
}
pub fn is_empty(self) -> bool {
self.value == 0
}
pub fn checked_sub(self, rhs:Size) -> Option<Self> {
self.value.checked_sub(rhs.value).map(Self::new)
}
}
impl Add for Size {
type Output = Size;
fn add(self, rhs:Size) -> Size {
Size {value:self.value + rhs.value}
}
}
impl AddAssign for Size {
fn add_assign(&mut self, rhs: Size) {
*self = *self + rhs;
}
}
impl Sub for Size {
type Output = Size;
fn sub(self, rhs:Size) -> Size {
Size{value: self.value - rhs.value}
}
}
impl SubAssign for Size {
fn sub_assign(&mut self, rhs: Size) {
*self = *self - rhs;
}
}
impl Display for Size {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"{}",self.value)
}
}
#[allow(missing_docs)]
#[derive(Clone,Copy,Debug,Default,Hash,PartialEq,Eq,PartialOrd,Ord,Serialize,Deserialize)]
pub struct Span { pub index:Index, pub size:Size }
impl Span {
pub fn new(index:Index, size:Size) -> Self {
Span {index,size}
}
pub fn from_indices(begin:Index, end:Index) -> Self {
if end < begin {
Self::from_indices(end,begin)
} else {
let index = begin;
let size = end - begin;
Span {index,size}
}
}
pub fn from_beginning_to(index:Index) -> Self {
Span::from_indices(Index::new(0), index)
}
pub fn from_beginning(size:Size) -> Self {
Span {index:Index::new(0), size}
}
pub fn last(&self) -> Option<Index> {
if self.is_empty() {
None
} else {
self.end().checked_sub(Size::new(1))
}
}
pub fn end(&self) -> Index {
self.index + self.size
}
pub fn contains(&self, index:Index) -> bool {
self.index <= index && self.end() > index
}
pub fn contains_span(&self, span:&Span) -> bool {
self.index <= span.index && self.end() >= span.end()
}
pub fn range(self) -> Range<usize> {
let start = self.index.value;
let end = self.end().value;
start .. end
}
pub fn extend_left(&mut self, size:Size) {
self.index -= size;
self.size += size;
}
pub fn extend_right(&mut self, size:Size) {
self.size += size;
}
pub fn shrink_left(&mut self, size:Size) {
self.index += size;
self.size -= size;
}
pub fn shrink_right(&mut self, size:Size) {
self.size -= size;
}
pub fn move_left(&mut self, size:Size) {
self.index -= size;
}
pub fn move_right(&mut self, size:Size) {
self.index += size;
}
pub fn set_left(&mut self, new_left:Index) {
let end = self.end();
self.index = new_left;
self.size = end - new_left;
}
pub fn set_right(&mut self, new_right:Index) {
self.size = new_right - self.index;
}
pub fn is_empty(self) -> bool {
self.size.is_empty()
}
}
impls! { From + &From <Range<usize>> for Span { |range|
Span::from_indices(Index::new(range.start), Index::new(range.end))
}}
impls! { From + &From<Span> for Range<usize> { |this|
this.range()
}}
impl PartialEq<Range<usize>> for Span {
fn eq(&self, other:&Range<usize>) -> bool {
&self.range() == other
}
}
impl Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"{}..{}",self.index.value,self.end().value)
}
}
impl std::ops::Index<Span> for str {
type Output = str;
fn index(&self, span:Span) -> &Self::Output {
let mut iter = self.char_indices();
let first = iter.nth(span.index.value).unwrap();
let to_last = span.last().map(|last| last - span.index);
let last_as_nth = to_last.and_then(|i| i.checked_sub(Size::new(1)));
let last = last_as_nth.map_or(first,|nth| iter.nth(nth.value).unwrap());
if span.is_empty() {
&self[first.0 .. first.0]
} else {
&self[first.0 .. last.0 + last.1.len_utf8()]
}
}
}
impl std::ops::Index<Span> for String {
type Output = str;
fn index(&self, index:Span) -> &Self::Output {
&self.as_str()[index]
}
}
impl From<Range<Index>> for Span {
fn from(range:Range<Index>) -> Self {
Span::from_indices(range.start,range.end)
}
}
impl Add<Size> for Index {
type Output = Index;
fn add(self, rhs:Size) -> Index {
Index {value:self.value + rhs.value}
}
}
impl AddAssign<Size> for Index {
fn add_assign(&mut self, rhs: Size) {
*self = *self + rhs;
}
}
impl Sub<Size> for Index {
type Output = Index;
fn sub(self, rhs:Size) -> Index {
Index {value:self.value - rhs.value}
}
}
impl SubAssign<Size> for Index {
fn sub_assign(&mut self, rhs: Size) {
*self = *self - rhs;
}
}
impl Sub for Index {
type Output = Size;
fn sub(self, rhs:Index) -> Size {
Size {value:self.value - rhs.value}
}
}
#[derive(Copy,Clone,Debug,PartialEq,Eq,PartialOrd,Ord)]
pub struct TextLocation {
pub line: usize,
pub column: usize,
}
impl Display for TextLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"{}:{}",self.line,self.column)
}
}
impl TextLocation {
pub fn at_line_begin(line_index:usize) -> Self {
TextLocation {
line : line_index,
column : 0,
}
}
pub fn at_document_begin() -> Self {
TextLocation {
line : 0,
column : 0,
}
}
pub fn at_document_end(content:impl Str) -> Self {
Self::after_chars(content.as_ref().chars())
}
pub fn from_index(content:impl Str, index:Index) -> Self {
let before = content.as_ref().chars().take(index.value);
Self::after_chars(before)
}
pub fn to_index(self, content:impl AsRef<str>) -> Index {
let line_index = match self.line {
0 => 0,
_ => {
let content = content.as_ref();
newline_indices(content).nth(self.line.saturating_sub(1)).map_or(0, |i| i + 1)
}
};
Index::new(line_index + self.column)
}
pub fn convert_range(content:impl Str, range:&Range<Index>) -> Range<Self> {
let content = content.as_ref();
Self::from_index(content,range.start)..Self::from_index(content,range.end)
}
pub fn convert_span(content:impl Str, span:&Span) -> Range<Self> {
let range = span.index..span.end();
Self::convert_range(content,&range)
}
pub fn convert_byte_range(content:impl Str, range:&Range<ByteIndex>) -> Range<Self> {
let start = Index::convert_byte_index(content.as_ref(), range.start);
let end = Index::convert_byte_index(content.as_ref(), range.end);
Self::convert_range(content,&(start..end))
}
fn after_chars<IntoCharsIter>(chars:IntoCharsIter) -> Self
where IntoCharsIter : IntoIterator<Item=char, IntoIter:Clone> {
let iter = chars.into_iter();
let len = iter.clone().count();
let newlines = iter.enumerate().filter(|(_,c)| *c == '\n');
let newlines_indices = newlines.map(|(i,_)| i);
TextLocation {
line : newlines_indices.clone().count(),
column : len - newlines_indices.last().map_or(0, |i| i + 1),
}
}
}
#[derive(Clone,Debug,Eq,Hash,PartialEq)]
pub struct TextChangeTemplate<Index,Content> {
pub replaced: Range<Index>,
pub inserted: Content,
}
pub type TextChange = TextChangeTemplate<Index,String>;
impl<Index:Copy,Content> TextChangeTemplate<Index,Content> {
pub fn insert(at:Index, text:Content) -> Self {
TextChangeTemplate {
replaced : at..at,
inserted: text,
}
}
}
impl<Index,Content> TextChangeTemplate<Index,Content> {
pub fn replace(replaced:Range<Index>, text:Content) -> Self {
let inserted = text;
TextChangeTemplate {replaced,inserted}
}
}
impl<Index:Sub+Clone,Content> TextChangeTemplate<Index,Content> {
pub fn replaced_size(&self) -> Index::Output {
self.replaced.end.clone() - self.replaced.start.clone()
}
}
impl<Content> TextChangeTemplate<Index,Content> {
pub fn replaced_span(&self) -> Span {
let index = self.replaced.start;
let size = self.replaced_size();
Span {index,size}
}
pub fn apply(&self, target:&mut String) where Content:AsRef<str> {
let replaced_indices = self.replaced.start.value..self.replaced.end.value;
target.replace_range(replaced_indices,self.inserted.as_ref());
}
pub fn applied(&self, target:&str) -> String where Content:AsRef<str> {
let mut target = target.to_string();
self.apply(&mut target);
target
}
}
impl<Index,Content:Default> TextChangeTemplate<Index,Content> {
pub fn delete(range:Range<Index>) -> Self {
TextChangeTemplate {
replaced : range,
inserted : default(),
}
}
}
pub fn newline_indices(text:&str) -> impl Iterator<Item=usize> + '_ {
text.chars().enumerate().filter_map(|(ix,c)| (c == '\n').as_some(ix))
}
pub fn newline_byte_indices(text:&str) -> impl Iterator<Item=usize> + '_ {
text.as_bytes().iter().enumerate().filter_map(|(ix,c)|(*c == b'\n').as_some(ix))
}
pub fn rev_newline_byte_indices(text:&str) -> impl Iterator<Item=usize> + '_ {
text.as_bytes().iter().enumerate().rev().filter_map(|(ix,c)| (*c == b'\n').as_some(ix))
}
pub fn split_to_lines(text:&str) -> impl Iterator<Item=String> + '_ {
text.split('\n').map(cut_cr_at_end_of_line).map(|s| s.to_string())
}
#[rustversion::since(2020-02-01)]
fn cut_cr_at_end_of_line(from:&str) -> &str {
from.strip_suffix('\r').unwrap_or(from)
}
#[cfg(test)]
mod test {
use super::*;
use super::Index;
fn assert_round_trip(str:&str, index:Index, location:TextLocation) {
assert_eq!(TextLocation::from_index(str,index),location);
assert_eq!(location.to_index(str),index);
}
#[test]
fn converting_index_to_location() {
let str = "first\nsecond\nthird";
assert_round_trip(str,Index::new(0), TextLocation {line:0, column:0});
assert_round_trip(str,Index::new(5), TextLocation {line:0, column:5});
assert_round_trip(str,Index::new(6), TextLocation {line:1, column:0});
assert_round_trip(str,Index::new(9), TextLocation {line:1, column:3});
assert_round_trip(str,Index::new(12), TextLocation {line:1, column:6});
assert_round_trip(str,Index::new(13), TextLocation {line:2, column:0});
assert_round_trip(str,Index::new(18), TextLocation {line:2, column:5});
let str = "";
assert_round_trip(str,Index::new(0), TextLocation {line:0, column:0});
let str= "\n";
assert_round_trip(str,Index::new(0), TextLocation {line:0, column:0});
assert_round_trip(str,Index::new(1), TextLocation {line:1, column:0});
}
#[test]
fn text_location_at_end() {
let str = "first\nsecond\nthird";
assert_eq!(TextLocation::at_document_end(str) , TextLocation {line:2, column:5});
assert_eq!(TextLocation::at_document_end("") , TextLocation {line:0, column:0});
assert_eq!(TextLocation::at_document_end("\n"), TextLocation {line:1, column:0});
}
#[test]
fn indexing_utf8() {
let str = "zazó黄ć gęślą jaźń";
assert_eq!(&str[Span::from(2..5)],"zó黄");
assert_eq!(&str[Span::from(5..5)],"");
assert_eq!(Size::from_text("日本語").value, 3);
assert_eq!(&"日本語"[Span::from(0..0)],"");
assert_eq!(&"日本語"[Span::from(0..3)],"日本語");
assert_eq!(&"日本語"[Span::from(0..1)],"日");
assert_eq!(&"日本語"[Span::from(2..3)],"語");
}
}