diff options
Diffstat (limited to 'libeditor/src/line_index.rs')
-rw-r--r-- | libeditor/src/line_index.rs | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/libeditor/src/line_index.rs b/libeditor/src/line_index.rs index feb482b32..801726aa5 100644 --- a/libeditor/src/line_index.rs +++ b/libeditor/src/line_index.rs | |||
@@ -1,6 +1,7 @@ | |||
1 | use superslice::Ext; | 1 | use superslice::Ext; |
2 | use ::{TextUnit}; | 2 | use ::TextUnit; |
3 | 3 | ||
4 | #[derive(Clone, Debug)] | ||
4 | pub struct LineIndex { | 5 | pub struct LineIndex { |
5 | newlines: Vec<TextUnit>, | 6 | newlines: Vec<TextUnit>, |
6 | } | 7 | } |
@@ -24,11 +25,16 @@ impl LineIndex { | |||
24 | LineIndex { newlines } | 25 | LineIndex { newlines } |
25 | } | 26 | } |
26 | 27 | ||
27 | pub fn translate(&self, offset: TextUnit) -> LineCol { | 28 | pub fn line_col(&self, offset: TextUnit) -> LineCol { |
28 | let line = self.newlines.upper_bound(&offset) - 1; | 29 | let line = self.newlines.upper_bound(&offset) - 1; |
29 | let line_start_offset = self.newlines[line]; | 30 | let line_start_offset = self.newlines[line]; |
30 | let col = offset - line_start_offset; | 31 | let col = offset - line_start_offset; |
31 | return LineCol { line: line as u32, col } | 32 | return LineCol { line: line as u32, col }; |
33 | } | ||
34 | |||
35 | pub fn offset(&self, line_col: LineCol) -> TextUnit { | ||
36 | //TODO: return Result | ||
37 | self.newlines[line_col.line as usize] + line_col.col | ||
32 | } | 38 | } |
33 | } | 39 | } |
34 | 40 | ||
@@ -36,21 +42,21 @@ impl LineIndex { | |||
36 | fn test_line_index() { | 42 | fn test_line_index() { |
37 | let text = "hello\nworld"; | 43 | let text = "hello\nworld"; |
38 | let index = LineIndex::new(text); | 44 | let index = LineIndex::new(text); |
39 | assert_eq!(index.translate(0.into()), LineCol { line: 0, col: 0.into()}); | 45 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0.into() }); |
40 | assert_eq!(index.translate(1.into()), LineCol { line: 0, col: 1.into()}); | 46 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col: 1.into() }); |
41 | assert_eq!(index.translate(5.into()), LineCol { line: 0, col: 5.into()}); | 47 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col: 5.into() }); |
42 | assert_eq!(index.translate(6.into()), LineCol { line: 1, col: 0.into()}); | 48 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 0.into() }); |
43 | assert_eq!(index.translate(7.into()), LineCol { line: 1, col: 1.into()}); | 49 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col: 1.into() }); |
44 | assert_eq!(index.translate(8.into()), LineCol { line: 1, col: 2.into()}); | 50 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col: 2.into() }); |
45 | assert_eq!(index.translate(10.into()), LineCol { line: 1, col: 4.into()}); | 51 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col: 4.into() }); |
46 | assert_eq!(index.translate(11.into()), LineCol { line: 1, col: 5.into()}); | 52 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col: 5.into() }); |
47 | assert_eq!(index.translate(12.into()), LineCol { line: 1, col: 6.into()}); | 53 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col: 6.into() }); |
48 | 54 | ||
49 | let text = "\nhello\nworld"; | 55 | let text = "\nhello\nworld"; |
50 | let index = LineIndex::new(text); | 56 | let index = LineIndex::new(text); |
51 | assert_eq!(index.translate(0.into()), LineCol { line: 0, col: 0.into()}); | 57 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0.into() }); |
52 | assert_eq!(index.translate(1.into()), LineCol { line: 1, col: 0.into()}); | 58 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col: 0.into() }); |
53 | assert_eq!(index.translate(2.into()), LineCol { line: 1, col: 1.into()}); | 59 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col: 1.into() }); |
54 | assert_eq!(index.translate(6.into()), LineCol { line: 1, col: 5.into()}); | 60 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 5.into() }); |
55 | assert_eq!(index.translate(7.into()), LineCol { line: 2, col: 0.into()}); | 61 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col: 0.into() }); |
56 | } | 62 | } |