diff options
Diffstat (limited to 'crates/ra_editor/src/line_index.rs')
-rw-r--r-- | crates/ra_editor/src/line_index.rs | 119 |
1 files changed, 103 insertions, 16 deletions
diff --git a/crates/ra_editor/src/line_index.rs b/crates/ra_editor/src/line_index.rs index 95d64b8a8..da0f2a7f7 100644 --- a/crates/ra_editor/src/line_index.rs +++ b/crates/ra_editor/src/line_index.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use superslice::Ext; | ||
2 | use crate::TextUnit; | 1 | use crate::TextUnit; |
2 | use superslice::Ext; | ||
3 | 3 | ||
4 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | 4 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] |
5 | pub struct LineIndex { | 5 | pub struct LineIndex { |
@@ -29,7 +29,10 @@ impl LineIndex { | |||
29 | let line = self.newlines.upper_bound(&offset) - 1; | 29 | let line = self.newlines.upper_bound(&offset) - 1; |
30 | let line_start_offset = self.newlines[line]; | 30 | let line_start_offset = self.newlines[line]; |
31 | let col = offset - line_start_offset; | 31 | let col = offset - line_start_offset; |
32 | return LineCol { line: line as u32, col }; | 32 | return LineCol { |
33 | line: line as u32, | ||
34 | col, | ||
35 | }; | ||
33 | } | 36 | } |
34 | 37 | ||
35 | pub fn offset(&self, line_col: LineCol) -> TextUnit { | 38 | pub fn offset(&self, line_col: LineCol) -> TextUnit { |
@@ -42,21 +45,105 @@ impl LineIndex { | |||
42 | fn test_line_index() { | 45 | fn test_line_index() { |
43 | let text = "hello\nworld"; | 46 | let text = "hello\nworld"; |
44 | let index = LineIndex::new(text); | 47 | let index = LineIndex::new(text); |
45 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0.into() }); | 48 | assert_eq!( |
46 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col: 1.into() }); | 49 | index.line_col(0.into()), |
47 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col: 5.into() }); | 50 | LineCol { |
48 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 0.into() }); | 51 | line: 0, |
49 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col: 1.into() }); | 52 | col: 0.into() |
50 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col: 2.into() }); | 53 | } |
51 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col: 4.into() }); | 54 | ); |
52 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col: 5.into() }); | 55 | assert_eq!( |
53 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col: 6.into() }); | 56 | index.line_col(1.into()), |
57 | LineCol { | ||
58 | line: 0, | ||
59 | col: 1.into() | ||
60 | } | ||
61 | ); | ||
62 | assert_eq!( | ||
63 | index.line_col(5.into()), | ||
64 | LineCol { | ||
65 | line: 0, | ||
66 | col: 5.into() | ||
67 | } | ||
68 | ); | ||
69 | assert_eq!( | ||
70 | index.line_col(6.into()), | ||
71 | LineCol { | ||
72 | line: 1, | ||
73 | col: 0.into() | ||
74 | } | ||
75 | ); | ||
76 | assert_eq!( | ||
77 | index.line_col(7.into()), | ||
78 | LineCol { | ||
79 | line: 1, | ||
80 | col: 1.into() | ||
81 | } | ||
82 | ); | ||
83 | assert_eq!( | ||
84 | index.line_col(8.into()), | ||
85 | LineCol { | ||
86 | line: 1, | ||
87 | col: 2.into() | ||
88 | } | ||
89 | ); | ||
90 | assert_eq!( | ||
91 | index.line_col(10.into()), | ||
92 | LineCol { | ||
93 | line: 1, | ||
94 | col: 4.into() | ||
95 | } | ||
96 | ); | ||
97 | assert_eq!( | ||
98 | index.line_col(11.into()), | ||
99 | LineCol { | ||
100 | line: 1, | ||
101 | col: 5.into() | ||
102 | } | ||
103 | ); | ||
104 | assert_eq!( | ||
105 | index.line_col(12.into()), | ||
106 | LineCol { | ||
107 | line: 1, | ||
108 | col: 6.into() | ||
109 | } | ||
110 | ); | ||
54 | 111 | ||
55 | let text = "\nhello\nworld"; | 112 | let text = "\nhello\nworld"; |
56 | let index = LineIndex::new(text); | 113 | let index = LineIndex::new(text); |
57 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col: 0.into() }); | 114 | assert_eq!( |
58 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col: 0.into() }); | 115 | index.line_col(0.into()), |
59 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col: 1.into() }); | 116 | LineCol { |
60 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col: 5.into() }); | 117 | line: 0, |
61 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col: 0.into() }); | 118 | col: 0.into() |
119 | } | ||
120 | ); | ||
121 | assert_eq!( | ||
122 | index.line_col(1.into()), | ||
123 | LineCol { | ||
124 | line: 1, | ||
125 | col: 0.into() | ||
126 | } | ||
127 | ); | ||
128 | assert_eq!( | ||
129 | index.line_col(2.into()), | ||
130 | LineCol { | ||
131 | line: 1, | ||
132 | col: 1.into() | ||
133 | } | ||
134 | ); | ||
135 | assert_eq!( | ||
136 | index.line_col(6.into()), | ||
137 | LineCol { | ||
138 | line: 1, | ||
139 | col: 5.into() | ||
140 | } | ||
141 | ); | ||
142 | assert_eq!( | ||
143 | index.line_col(7.into()), | ||
144 | LineCol { | ||
145 | line: 2, | ||
146 | col: 0.into() | ||
147 | } | ||
148 | ); | ||
62 | } | 149 | } |