aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/ide_db
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-06 11:17:40 +0000
committerAleksey Kladov <[email protected]>2020-02-06 11:17:40 +0000
commitee2ee1a8ff7ad13211d2f66d2f2f1daaf3a00bd9 (patch)
treec0956465525077f4318ff6575ec6a3ac24bc57ff /crates/ra_ide/src/ide_db
parent551f33d754c5131f548baee4f58fd5892a9c7efc (diff)
Move line_index
Diffstat (limited to 'crates/ra_ide/src/ide_db')
-rw-r--r--crates/ra_ide/src/ide_db/line_index.rs283
-rw-r--r--crates/ra_ide/src/ide_db/line_index_utils.rs332
-rw-r--r--crates/ra_ide/src/ide_db/mod.rs6
3 files changed, 620 insertions, 1 deletions
diff --git a/crates/ra_ide/src/ide_db/line_index.rs b/crates/ra_ide/src/ide_db/line_index.rs
new file mode 100644
index 000000000..6f99ca3a7
--- /dev/null
+++ b/crates/ra_ide/src/ide_db/line_index.rs
@@ -0,0 +1,283 @@
1//! FIXME: write short doc here
2
3use ra_syntax::TextUnit;
4use rustc_hash::FxHashMap;
5use superslice::Ext;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct LineIndex {
9 pub(crate) newlines: Vec<TextUnit>,
10 pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
11}
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub struct LineCol {
15 /// Zero-based
16 pub line: u32,
17 /// Zero-based
18 pub col_utf16: u32,
19}
20
21#[derive(Clone, Debug, Hash, PartialEq, Eq)]
22pub(crate) struct Utf16Char {
23 pub(crate) start: TextUnit,
24 pub(crate) end: TextUnit,
25}
26
27impl Utf16Char {
28 fn len(&self) -> TextUnit {
29 self.end - self.start
30 }
31}
32
33impl LineIndex {
34 pub fn new(text: &str) -> LineIndex {
35 let mut utf16_lines = FxHashMap::default();
36 let mut utf16_chars = Vec::new();
37
38 let mut newlines = vec![0.into()];
39 let mut curr_row = 0.into();
40 let mut curr_col = 0.into();
41 let mut line = 0;
42 for c in text.chars() {
43 curr_row += TextUnit::of_char(c);
44 if c == '\n' {
45 newlines.push(curr_row);
46
47 // Save any utf-16 characters seen in the previous line
48 if !utf16_chars.is_empty() {
49 utf16_lines.insert(line, utf16_chars);
50 utf16_chars = Vec::new();
51 }
52
53 // Prepare for processing the next line
54 curr_col = 0.into();
55 line += 1;
56 continue;
57 }
58
59 let char_len = TextUnit::of_char(c);
60 if char_len.to_usize() > 1 {
61 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
62 }
63
64 curr_col += char_len;
65 }
66
67 // Save any utf-16 characters seen in the last line
68 if !utf16_chars.is_empty() {
69 utf16_lines.insert(line, utf16_chars);
70 }
71
72 LineIndex { newlines, utf16_lines }
73 }
74
75 pub fn line_col(&self, offset: TextUnit) -> LineCol {
76 let line = self.newlines.upper_bound(&offset) - 1;
77 let line_start_offset = self.newlines[line];
78 let col = offset - line_start_offset;
79
80 LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 }
81 }
82
83 pub fn offset(&self, line_col: LineCol) -> TextUnit {
84 //FIXME: return Result
85 let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16);
86 self.newlines[line_col.line as usize] + col
87 }
88
89 fn utf8_to_utf16_col(&self, line: u32, mut col: TextUnit) -> usize {
90 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
91 let mut correction = TextUnit::from_usize(0);
92 for c in utf16_chars {
93 if col >= c.end {
94 correction += c.len() - TextUnit::from_usize(1);
95 } else {
96 // From here on, all utf16 characters come *after* the character we are mapping,
97 // so we don't need to take them into account
98 break;
99 }
100 }
101
102 col -= correction;
103 }
104
105 col.to_usize()
106 }
107
108 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextUnit {
109 let mut col: TextUnit = col.into();
110 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
111 for c in utf16_chars {
112 if col >= c.start {
113 col += c.len() - TextUnit::from_usize(1);
114 } else {
115 // From here on, all utf16 characters come *after* the character we are mapping,
116 // so we don't need to take them into account
117 break;
118 }
119 }
120 }
121
122 col
123 }
124}
125
126#[cfg(test)]
127/// Simple reference implementation to use in proptests
128pub fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
129 let mut res = LineCol { line: 0, col_utf16: 0 };
130 for (i, c) in text.char_indices() {
131 if i + c.len_utf8() > offset.to_usize() {
132 // if it's an invalid offset, inside a multibyte char
133 // return as if it was at the start of the char
134 break;
135 }
136 if c == '\n' {
137 res.line += 1;
138 res.col_utf16 = 0;
139 } else {
140 res.col_utf16 += 1;
141 }
142 }
143 res
144}
145
146#[cfg(test)]
147mod test_line_index {
148 use super::*;
149 use proptest::{prelude::*, proptest};
150 use ra_text_edit::test_utils::{arb_offset, arb_text};
151
152 #[test]
153 fn test_line_index() {
154 let text = "hello\nworld";
155 let index = LineIndex::new(text);
156 assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
157 assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 });
158 assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 });
159 assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 });
160 assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 });
161 assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 });
162 assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 });
163 assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 });
164 assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 });
165
166 let text = "\nhello\nworld";
167 let index = LineIndex::new(text);
168 assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
169 assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 });
170 assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 });
171 assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 });
172 assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 });
173 }
174
175 fn arb_text_with_offset() -> BoxedStrategy<(TextUnit, String)> {
176 arb_text().prop_flat_map(|text| (arb_offset(&text), Just(text))).boxed()
177 }
178
179 fn to_line_col(text: &str, offset: TextUnit) -> LineCol {
180 let mut res = LineCol { line: 0, col_utf16: 0 };
181 for (i, c) in text.char_indices() {
182 if i + c.len_utf8() > offset.to_usize() {
183 // if it's an invalid offset, inside a multibyte char
184 // return as if it was at the start of the char
185 break;
186 }
187 if c == '\n' {
188 res.line += 1;
189 res.col_utf16 = 0;
190 } else {
191 res.col_utf16 += 1;
192 }
193 }
194 res
195 }
196
197 proptest! {
198 #[test]
199 fn test_line_index_proptest((offset, text) in arb_text_with_offset()) {
200 let expected = to_line_col(&text, offset);
201 let line_index = LineIndex::new(&text);
202 let actual = line_index.line_col(offset);
203
204 assert_eq!(actual, expected);
205 }
206 }
207}
208
209#[cfg(test)]
210mod test_utf8_utf16_conv {
211 use super::*;
212
213 #[test]
214 fn test_char_len() {
215 assert_eq!('メ'.len_utf8(), 3);
216 assert_eq!('メ'.len_utf16(), 1);
217 }
218
219 #[test]
220 fn test_empty_index() {
221 let col_index = LineIndex::new(
222 "
223const C: char = 'x';
224",
225 );
226 assert_eq!(col_index.utf16_lines.len(), 0);
227 }
228
229 #[test]
230 fn test_single_char() {
231 let col_index = LineIndex::new(
232 "
233const C: char = 'メ';
234",
235 );
236
237 assert_eq!(col_index.utf16_lines.len(), 1);
238 assert_eq!(col_index.utf16_lines[&1].len(), 1);
239 assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
240
241 // UTF-8 to UTF-16, no changes
242 assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
243
244 // UTF-8 to UTF-16
245 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20);
246
247 // UTF-16 to UTF-8, no changes
248 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from(15));
249
250 // UTF-16 to UTF-8
251 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from(21));
252 }
253
254 #[test]
255 fn test_string() {
256 let col_index = LineIndex::new(
257 "
258const C: char = \"メ メ\";
259",
260 );
261
262 assert_eq!(col_index.utf16_lines.len(), 1);
263 assert_eq!(col_index.utf16_lines[&1].len(), 2);
264 assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
265 assert_eq!(col_index.utf16_lines[&1][1], Utf16Char { start: 21.into(), end: 24.into() });
266
267 // UTF-8 to UTF-16
268 assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
269
270 assert_eq!(col_index.utf8_to_utf16_col(1, 21.into()), 19);
271 assert_eq!(col_index.utf8_to_utf16_col(1, 25.into()), 21);
272
273 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
274
275 // UTF-16 to UTF-8
276 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from_usize(15));
277
278 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextUnit::from_usize(20));
279 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from_usize(23));
280
281 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15));
282 }
283}
diff --git a/crates/ra_ide/src/ide_db/line_index_utils.rs b/crates/ra_ide/src/ide_db/line_index_utils.rs
new file mode 100644
index 000000000..70bf7253c
--- /dev/null
+++ b/crates/ra_ide/src/ide_db/line_index_utils.rs
@@ -0,0 +1,332 @@
1//! FIXME: write short doc here
2
3use ra_syntax::{TextRange, TextUnit};
4use ra_text_edit::{AtomTextEdit, TextEdit};
5
6use crate::ide_db::line_index::{LineCol, LineIndex, Utf16Char};
7
8#[derive(Debug, Clone)]
9enum Step {
10 Newline(TextUnit),
11 Utf16Char(TextRange),
12}
13
14#[derive(Debug)]
15struct LineIndexStepIter<'a> {
16 line_index: &'a LineIndex,
17 next_newline_idx: usize,
18 utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>,
19}
20
21impl<'a> LineIndexStepIter<'a> {
22 fn from(line_index: &LineIndex) -> LineIndexStepIter {
23 let mut x = LineIndexStepIter { line_index, next_newline_idx: 0, utf16_chars: None };
24 // skip first newline since it's not real
25 x.next();
26 x
27 }
28}
29
30impl<'a> Iterator for LineIndexStepIter<'a> {
31 type Item = Step;
32 fn next(&mut self) -> Option<Step> {
33 self.utf16_chars
34 .as_mut()
35 .and_then(|(newline, x)| {
36 let x = x.next()?;
37 Some(Step::Utf16Char(TextRange::from_to(*newline + x.start, *newline + x.end)))
38 })
39 .or_else(|| {
40 let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?;
41 self.utf16_chars = self
42 .line_index
43 .utf16_lines
44 .get(&(self.next_newline_idx as u32))
45 .map(|x| (next_newline, x.iter()));
46 self.next_newline_idx += 1;
47 Some(Step::Newline(next_newline))
48 })
49 }
50}
51
52#[derive(Debug)]
53struct OffsetStepIter<'a> {
54 text: &'a str,
55 offset: TextUnit,
56}
57
58impl<'a> Iterator for OffsetStepIter<'a> {
59 type Item = Step;
60 fn next(&mut self) -> Option<Step> {
61 let (next, next_offset) = self
62 .text
63 .char_indices()
64 .filter_map(|(i, c)| {
65 if c == '\n' {
66 let next_offset = self.offset + TextUnit::from_usize(i + 1);
67 let next = Step::Newline(next_offset);
68 Some((next, next_offset))
69 } else {
70 let char_len = TextUnit::of_char(c);
71 if char_len.to_usize() > 1 {
72 let start = self.offset + TextUnit::from_usize(i);
73 let end = start + char_len;
74 let next = Step::Utf16Char(TextRange::from_to(start, end));
75 let next_offset = end;
76 Some((next, next_offset))
77 } else {
78 None
79 }
80 }
81 })
82 .next()?;
83 let next_idx = (next_offset - self.offset).to_usize();
84 self.text = &self.text[next_idx..];
85 self.offset = next_offset;
86 Some(next)
87 }
88}
89
90#[derive(Debug)]
91enum NextSteps<'a> {
92 Use,
93 ReplaceMany(OffsetStepIter<'a>),
94 AddMany(OffsetStepIter<'a>),
95}
96
97#[derive(Debug)]
98struct TranslatedEdit<'a> {
99 delete: TextRange,
100 insert: &'a str,
101 diff: i64,
102}
103
104struct Edits<'a> {
105 edits: &'a [AtomTextEdit],
106 current: Option<TranslatedEdit<'a>>,
107 acc_diff: i64,
108}
109
110impl<'a> Edits<'a> {
111 fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> {
112 let mut x = Edits { edits: text_edit.as_atoms(), current: None, acc_diff: 0 };
113 x.advance_edit();
114 x
115 }
116 fn advance_edit(&mut self) {
117 self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff);
118 match self.edits.split_first() {
119 Some((next, rest)) => {
120 let delete = self.translate_range(next.delete);
121 let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64;
122 self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff });
123 self.edits = rest;
124 }
125 None => {
126 self.current = None;
127 }
128 }
129 }
130
131 fn next_inserted_steps(&mut self) -> Option<OffsetStepIter<'a>> {
132 let cur = self.current.as_ref()?;
133 let res = Some(OffsetStepIter { offset: cur.delete.start(), text: &cur.insert });
134 self.advance_edit();
135 res
136 }
137
138 fn next_steps(&mut self, step: &Step) -> NextSteps {
139 let step_pos = match *step {
140 Step::Newline(n) => n,
141 Step::Utf16Char(r) => r.end(),
142 };
143 match &mut self.current {
144 Some(edit) => {
145 if step_pos <= edit.delete.start() {
146 NextSteps::Use
147 } else if step_pos <= edit.delete.end() {
148 let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert };
149 // empty slice to avoid returning steps again
150 edit.insert = &edit.insert[edit.insert.len()..];
151 NextSteps::ReplaceMany(iter)
152 } else {
153 let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert };
154 // empty slice to avoid returning steps again
155 edit.insert = &edit.insert[edit.insert.len()..];
156 self.advance_edit();
157 NextSteps::AddMany(iter)
158 }
159 }
160 None => NextSteps::Use,
161 }
162 }
163
164 fn translate_range(&self, range: TextRange) -> TextRange {
165 if self.acc_diff == 0 {
166 range
167 } else {
168 let start = self.translate(range.start());
169 let end = self.translate(range.end());
170 TextRange::from_to(start, end)
171 }
172 }
173
174 fn translate(&self, x: TextUnit) -> TextUnit {
175 if self.acc_diff == 0 {
176 x
177 } else {
178 TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32)
179 }
180 }
181
182 fn translate_step(&self, x: &Step) -> Step {
183 if self.acc_diff == 0 {
184 x.clone()
185 } else {
186 match *x {
187 Step::Newline(n) => Step::Newline(self.translate(n)),
188 Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)),
189 }
190 }
191 }
192}
193
194#[derive(Debug)]
195struct RunningLineCol {
196 line: u32,
197 last_newline: TextUnit,
198 col_adjust: TextUnit,
199}
200
201impl RunningLineCol {
202 fn new() -> RunningLineCol {
203 RunningLineCol { line: 0, last_newline: TextUnit::from(0), col_adjust: TextUnit::from(0) }
204 }
205
206 fn to_line_col(&self, offset: TextUnit) -> LineCol {
207 LineCol {
208 line: self.line,
209 col_utf16: ((offset - self.last_newline) - self.col_adjust).into(),
210 }
211 }
212
213 fn add_line(&mut self, newline: TextUnit) {
214 self.line += 1;
215 self.last_newline = newline;
216 self.col_adjust = TextUnit::from(0);
217 }
218
219 fn adjust_col(&mut self, range: TextRange) {
220 self.col_adjust += range.len() - TextUnit::from(1);
221 }
222}
223
224pub fn translate_offset_with_edit(
225 line_index: &LineIndex,
226 offset: TextUnit,
227 text_edit: &TextEdit,
228) -> LineCol {
229 let mut state = Edits::from_text_edit(&text_edit);
230
231 let mut res = RunningLineCol::new();
232
233 macro_rules! test_step {
234 ($x:ident) => {
235 match &$x {
236 Step::Newline(n) => {
237 if offset < *n {
238 return res.to_line_col(offset);
239 } else {
240 res.add_line(*n);
241 }
242 }
243 Step::Utf16Char(x) => {
244 if offset < x.end() {
245 // if the offset is inside a multibyte char it's invalid
246 // clamp it to the start of the char
247 let clamp = offset.min(x.start());
248 return res.to_line_col(clamp);
249 } else {
250 res.adjust_col(*x);
251 }
252 }
253 }
254 };
255 }
256
257 for orig_step in LineIndexStepIter::from(line_index) {
258 loop {
259 let translated_step = state.translate_step(&orig_step);
260 match state.next_steps(&translated_step) {
261 NextSteps::Use => {
262 test_step!(translated_step);
263 break;
264 }
265 NextSteps::ReplaceMany(ns) => {
266 for n in ns {
267 test_step!(n);
268 }
269 break;
270 }
271 NextSteps::AddMany(ns) => {
272 for n in ns {
273 test_step!(n);
274 }
275 }
276 }
277 }
278 }
279
280 loop {
281 match state.next_inserted_steps() {
282 None => break,
283 Some(ns) => {
284 for n in ns {
285 test_step!(n);
286 }
287 }
288 }
289 }
290
291 res.to_line_col(offset)
292}
293
294#[cfg(test)]
295mod test {
296 use super::*;
297 use crate::line_index;
298 use proptest::{prelude::*, proptest};
299 use ra_text_edit::test_utils::{arb_offset, arb_text_with_edit};
300 use ra_text_edit::TextEdit;
301
302 #[derive(Debug)]
303 struct ArbTextWithEditAndOffset {
304 text: String,
305 edit: TextEdit,
306 edited_text: String,
307 offset: TextUnit,
308 }
309
310 fn arb_text_with_edit_and_offset() -> BoxedStrategy<ArbTextWithEditAndOffset> {
311 arb_text_with_edit()
312 .prop_flat_map(|x| {
313 let edited_text = x.edit.apply(&x.text);
314 let arb_offset = arb_offset(&edited_text);
315 (Just(x), Just(edited_text), arb_offset).prop_map(|(x, edited_text, offset)| {
316 ArbTextWithEditAndOffset { text: x.text, edit: x.edit, edited_text, offset }
317 })
318 })
319 .boxed()
320 }
321
322 proptest! {
323 #[test]
324 fn test_translate_offset_with_edit(x in arb_text_with_edit_and_offset()) {
325 let expected = line_index::to_line_col(&x.edited_text, x.offset);
326 let line_index = LineIndex::new(&x.text);
327 let actual = translate_offset_with_edit(&line_index, x.offset, &x.edit);
328
329 assert_eq!(actual, expected);
330 }
331 }
332}
diff --git a/crates/ra_ide/src/ide_db/mod.rs b/crates/ra_ide/src/ide_db/mod.rs
index 47d0aed6f..cd47132ce 100644
--- a/crates/ra_ide/src/ide_db/mod.rs
+++ b/crates/ra_ide/src/ide_db/mod.rs
@@ -1,5 +1,8 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3pub mod line_index;
4pub mod line_index_utils;
5
3use std::sync::Arc; 6use std::sync::Arc;
4 7
5use ra_db::{ 8use ra_db::{
@@ -10,8 +13,9 @@ use ra_db::{
10use rustc_hash::FxHashMap; 13use rustc_hash::FxHashMap;
11 14
12use crate::{ 15use crate::{
16 ide_db::line_index::LineIndex,
13 symbol_index::{self, SymbolsDatabase}, 17 symbol_index::{self, SymbolsDatabase},
14 FeatureFlags, LineIndex, 18 FeatureFlags,
15}; 19};
16 20
17#[salsa::database( 21#[salsa::database(