diff options
Diffstat (limited to 'crates/ra_ide_db/src/line_index.rs')
-rw-r--r-- | crates/ra_ide_db/src/line_index.rs | 60 |
1 files changed, 0 insertions, 60 deletions
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs index 452c87ac5..af7b759e5 100644 --- a/crates/ra_ide_db/src/line_index.rs +++ b/crates/ra_ide_db/src/line_index.rs | |||
@@ -125,30 +125,8 @@ impl LineIndex { | |||
125 | } | 125 | } |
126 | 126 | ||
127 | #[cfg(test)] | 127 | #[cfg(test)] |
128 | /// Simple reference implementation to use in proptests | ||
129 | pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | ||
130 | let mut res = LineCol { line: 0, col_utf16: 0 }; | ||
131 | for (i, c) in text.char_indices() { | ||
132 | if i + c.len_utf8() > offset.to_usize() { | ||
133 | // if it's an invalid offset, inside a multibyte char | ||
134 | // return as if it was at the start of the char | ||
135 | break; | ||
136 | } | ||
137 | if c == '\n' { | ||
138 | res.line += 1; | ||
139 | res.col_utf16 = 0; | ||
140 | } else { | ||
141 | res.col_utf16 += 1; | ||
142 | } | ||
143 | } | ||
144 | res | ||
145 | } | ||
146 | |||
147 | #[cfg(test)] | ||
148 | mod test_line_index { | 128 | mod test_line_index { |
149 | use super::*; | 129 | use super::*; |
150 | use proptest::{prelude::*, proptest}; | ||
151 | use ra_text_edit::test_utils::{arb_offset, arb_text}; | ||
152 | 130 | ||
153 | #[test] | 131 | #[test] |
154 | fn test_line_index() { | 132 | fn test_line_index() { |
@@ -173,44 +151,6 @@ mod test_line_index { | |||
173 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); | 151 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); |
174 | } | 152 | } |
175 | 153 | ||
176 | fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> { | ||
177 | arb_text().prop_flat_map(|text| (arb_offset(&text), Just(text))).boxed() | ||
178 | } | ||
179 | |||
180 | fn to_line_col(text: &str, offset: TextUnit) -> LineCol { | ||
181 | let mut res = LineCol { line: 0, col_utf16: 0 }; | ||
182 | for (i, c) in text.char_indices() { | ||
183 | if i + c.len_utf8() > offset.to_usize() { | ||
184 | // if it's an invalid offset, inside a multibyte char | ||
185 | // return as if it was at the start of the char | ||
186 | break; | ||
187 | } | ||
188 | if c == '\n' { | ||
189 | res.line += 1; | ||
190 | res.col_utf16 = 0; | ||
191 | } else { | ||
192 | res.col_utf16 += 1; | ||
193 | } | ||
194 | } | ||
195 | res | ||
196 | } | ||
197 | |||
198 | proptest! { | ||
199 | #[test] | ||
200 | fn test_line_index_proptest((offset, text) in arb_text_with_offset()) { | ||
201 | let expected = to_line_col(&text, offset); | ||
202 | let line_index = LineIndex::new(&text); | ||
203 | let actual = line_index.line_col(offset); | ||
204 | |||
205 | assert_eq!(actual, expected); | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | |||
210 | #[cfg(test)] | ||
211 | mod test_utf8_utf16_conv { | ||
212 | use super::*; | ||
213 | |||
214 | #[test] | 154 | #[test] |
215 | fn test_char_len() { | 155 | fn test_char_len() { |
216 | assert_eq!('メ'.len_utf8(), 3); | 156 | assert_eq!('メ'.len_utf8(), 3); |