aboutsummaryrefslogtreecommitdiff
path: root/libeditor
diff options
context:
space:
mode:
Diffstat (limited to 'libeditor')
-rw-r--r--libeditor/src/lib.rs1
-rw-r--r--libeditor/src/line_index.rs40
2 files changed, 24 insertions, 17 deletions
diff --git a/libeditor/src/lib.rs b/libeditor/src/lib.rs
index 9da71743f..f77647338 100644
--- a/libeditor/src/lib.rs
+++ b/libeditor/src/lib.rs
@@ -10,6 +10,7 @@ use libsyntax2::{
10 SyntaxKind::*, 10 SyntaxKind::*,
11}; 11};
12pub use libsyntax2::{TextRange, TextUnit, ast}; 12pub use libsyntax2::{TextRange, TextUnit, ast};
13pub use self::line_index::{LineIndex, LineCol};
13 14
14#[derive(Debug)] 15#[derive(Debug)]
15pub struct HighlightedRange { 16pub struct HighlightedRange {
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 @@
1use superslice::Ext; 1use superslice::Ext;
2use ::{TextUnit}; 2use ::TextUnit;
3 3
4#[derive(Clone, Debug)]
4pub struct LineIndex { 5pub 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 {
36fn test_line_index() { 42fn 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}