aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r--crates/ra_ide_db/src/line_index.rs66
-rw-r--r--crates/ra_ide_db/src/line_index_utils.rs51
-rw-r--r--crates/ra_ide_db/src/search.rs10
3 files changed, 63 insertions, 64 deletions
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs
index 8ae745ff2..00ba95913 100644
--- a/crates/ra_ide_db/src/line_index.rs
+++ b/crates/ra_ide_db/src/line_index.rs
@@ -1,14 +1,14 @@
1//! `LineIndex` maps flat `TextUnit` offsets into `(Line, Column)` 1//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
2//! representation. 2//! representation.
3use std::iter; 3use std::iter;
4 4
5use ra_syntax::{TextRange, TextUnit}; 5use ra_syntax::{TextRange, TextSize};
6use rustc_hash::FxHashMap; 6use rustc_hash::FxHashMap;
7use superslice::Ext; 7use superslice::Ext;
8 8
9#[derive(Clone, Debug, PartialEq, Eq)] 9#[derive(Clone, Debug, PartialEq, Eq)]
10pub struct LineIndex { 10pub struct LineIndex {
11 pub(crate) newlines: Vec<TextUnit>, 11 pub(crate) newlines: Vec<TextSize>,
12 pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>, 12 pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
13} 13}
14 14
@@ -22,12 +22,12 @@ pub struct LineCol {
22 22
23#[derive(Clone, Debug, Hash, PartialEq, Eq)] 23#[derive(Clone, Debug, Hash, PartialEq, Eq)]
24pub(crate) struct Utf16Char { 24pub(crate) struct Utf16Char {
25 pub(crate) start: TextUnit, 25 pub(crate) start: TextSize,
26 pub(crate) end: TextUnit, 26 pub(crate) end: TextSize,
27} 27}
28 28
29impl Utf16Char { 29impl Utf16Char {
30 fn len(&self) -> TextUnit { 30 fn len(&self) -> TextSize {
31 self.end - self.start 31 self.end - self.start
32 } 32 }
33} 33}
@@ -42,7 +42,8 @@ impl LineIndex {
42 let mut curr_col = 0.into(); 42 let mut curr_col = 0.into();
43 let mut line = 0; 43 let mut line = 0;
44 for c in text.chars() { 44 for c in text.chars() {
45 curr_row += TextUnit::of_char(c); 45 let c_len = TextSize::of(c);
46 curr_row += c_len;
46 if c == '\n' { 47 if c == '\n' {
47 newlines.push(curr_row); 48 newlines.push(curr_row);
48 49
@@ -58,12 +59,11 @@ impl LineIndex {
58 continue; 59 continue;
59 } 60 }
60 61
61 let char_len = TextUnit::of_char(c); 62 if !c.is_ascii() {
62 if char_len > TextUnit::from_usize(1) { 63 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len });
63 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
64 } 64 }
65 65
66 curr_col += char_len; 66 curr_col += c_len;
67 } 67 }
68 68
69 // Save any utf-16 characters seen in the last line 69 // Save any utf-16 characters seen in the last line
@@ -74,7 +74,7 @@ impl LineIndex {
74 LineIndex { newlines, utf16_lines } 74 LineIndex { newlines, utf16_lines }
75 } 75 }
76 76
77 pub fn line_col(&self, offset: TextUnit) -> LineCol { 77 pub fn line_col(&self, offset: TextSize) -> LineCol {
78 let line = self.newlines.upper_bound(&offset) - 1; 78 let line = self.newlines.upper_bound(&offset) - 1;
79 let line_start_offset = self.newlines[line]; 79 let line_start_offset = self.newlines[line];
80 let col = offset - line_start_offset; 80 let col = offset - line_start_offset;
@@ -82,7 +82,7 @@ impl LineIndex {
82 LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } 82 LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 }
83 } 83 }
84 84
85 pub fn offset(&self, line_col: LineCol) -> TextUnit { 85 pub fn offset(&self, line_col: LineCol) -> TextSize {
86 //FIXME: return Result 86 //FIXME: return Result
87 let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16); 87 let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16);
88 self.newlines[line_col.line as usize] + col 88 self.newlines[line_col.line as usize] + col
@@ -97,35 +97,31 @@ impl LineIndex {
97 97
98 all.clone() 98 all.clone()
99 .zip(all.skip(1)) 99 .zip(all.skip(1))
100 .map(|(lo, hi)| TextRange::from_to(lo, hi)) 100 .map(|(lo, hi)| TextRange::new(lo, hi))
101 .filter(|it| !it.is_empty()) 101 .filter(|it| !it.is_empty())
102 } 102 }
103 103
104 fn utf8_to_utf16_col(&self, line: u32, col: TextUnit) -> usize { 104 fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize {
105 let mut res: usize = col.into();
105 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 106 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
106 let mut correction = 0;
107 for c in utf16_chars { 107 for c in utf16_chars {
108 if col >= c.end { 108 if c.end <= col {
109 correction += c.len().to_usize() - 1; 109 res -= usize::from(c.len()) - 1;
110 } else { 110 } else {
111 // From here on, all utf16 characters come *after* the character we are mapping, 111 // From here on, all utf16 characters come *after* the character we are mapping,
112 // so we don't need to take them into account 112 // so we don't need to take them into account
113 break; 113 break;
114 } 114 }
115 } 115 }
116
117 col.to_usize() - correction
118 } else {
119 col.to_usize()
120 } 116 }
117 res
121 } 118 }
122 119
123 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextUnit { 120 fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize {
124 let mut col: TextUnit = col.into();
125 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 121 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
126 for c in utf16_chars { 122 for c in utf16_chars {
127 if col >= c.start { 123 if col >= u32::from(c.start) {
128 col += c.len() - TextUnit::from_usize(1); 124 col += u32::from(c.len()) - 1;
129 } else { 125 } else {
130 // From here on, all utf16 characters come *after* the character we are mapping, 126 // From here on, all utf16 characters come *after* the character we are mapping,
131 // so we don't need to take them into account 127 // so we don't need to take them into account
@@ -134,12 +130,12 @@ impl LineIndex {
134 } 130 }
135 } 131 }
136 132
137 col 133 col.into()
138 } 134 }
139} 135}
140 136
141#[cfg(test)] 137#[cfg(test)]
142mod test_line_index { 138mod tests {
143 use super::*; 139 use super::*;
144 140
145 #[test] 141 #[test]
@@ -200,10 +196,10 @@ const C: char = 'メ';
200 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20); 196 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20);
201 197
202 // UTF-16 to UTF-8, no changes 198 // UTF-16 to UTF-8, no changes
203 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from(15)); 199 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
204 200
205 // UTF-16 to UTF-8 201 // UTF-16 to UTF-8
206 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from(21)); 202 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21));
207 } 203 }
208 204
209 #[test] 205 #[test]
@@ -228,18 +224,18 @@ const C: char = \"メ メ\";
228 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); 224 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
229 225
230 // UTF-16 to UTF-8 226 // UTF-16 to UTF-8
231 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from_usize(15)); 227 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
232 228
233 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextUnit::from_usize(20)); 229 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20));
234 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from_usize(23)); 230 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(23));
235 231
236 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); 232 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15));
237 } 233 }
238 234
239 #[test] 235 #[test]
240 fn test_splitlines() { 236 fn test_splitlines() {
241 fn r(lo: u32, hi: u32) -> TextRange { 237 fn r(lo: u32, hi: u32) -> TextRange {
242 TextRange::from_to(lo.into(), hi.into()) 238 TextRange::new(lo.into(), hi.into())
243 } 239 }
244 240
245 let text = "a\nbb\nccc\n"; 241 let text = "a\nbb\nccc\n";
diff --git a/crates/ra_ide_db/src/line_index_utils.rs b/crates/ra_ide_db/src/line_index_utils.rs
index 2ebbabdc6..039a12c0d 100644
--- a/crates/ra_ide_db/src/line_index_utils.rs
+++ b/crates/ra_ide_db/src/line_index_utils.rs
@@ -1,20 +1,22 @@
1//! Code actions can specify desirable final position of the cursor. 1//! Code actions can specify desirable final position of the cursor.
2//! 2//!
3//! The position is specified as a `TextUnit` in the final file. We need to send 3//! The position is specified as a `TextSize` in the final file. We need to send
4//! it in `(Line, Column)` coordinate though. However, we only have a LineIndex 4//! it in `(Line, Column)` coordinate though. However, we only have a LineIndex
5//! for a file pre-edit! 5//! for a file pre-edit!
6//! 6//!
7//! Code in this module applies this "to (Line, Column) after edit" 7//! Code in this module applies this "to (Line, Column) after edit"
8//! transformation. 8//! transformation.
9 9
10use ra_syntax::{TextRange, TextUnit}; 10use std::convert::TryInto;
11
12use ra_syntax::{TextRange, TextSize};
11use ra_text_edit::{AtomTextEdit, TextEdit}; 13use ra_text_edit::{AtomTextEdit, TextEdit};
12 14
13use crate::line_index::{LineCol, LineIndex, Utf16Char}; 15use crate::line_index::{LineCol, LineIndex, Utf16Char};
14 16
15pub fn translate_offset_with_edit( 17pub fn translate_offset_with_edit(
16 line_index: &LineIndex, 18 line_index: &LineIndex,
17 offset: TextUnit, 19 offset: TextSize,
18 text_edit: &TextEdit, 20 text_edit: &TextEdit,
19) -> LineCol { 21) -> LineCol {
20 let mut state = Edits::from_text_edit(&text_edit); 22 let mut state = Edits::from_text_edit(&text_edit);
@@ -84,7 +86,7 @@ pub fn translate_offset_with_edit(
84 86
85#[derive(Debug, Clone)] 87#[derive(Debug, Clone)]
86enum Step { 88enum Step {
87 Newline(TextUnit), 89 Newline(TextSize),
88 Utf16Char(TextRange), 90 Utf16Char(TextRange),
89} 91}
90 92
@@ -92,7 +94,7 @@ enum Step {
92struct LineIndexStepIter<'a> { 94struct LineIndexStepIter<'a> {
93 line_index: &'a LineIndex, 95 line_index: &'a LineIndex,
94 next_newline_idx: usize, 96 next_newline_idx: usize,
95 utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>, 97 utf16_chars: Option<(TextSize, std::slice::Iter<'a, Utf16Char>)>,
96} 98}
97 99
98impl LineIndexStepIter<'_> { 100impl LineIndexStepIter<'_> {
@@ -111,7 +113,7 @@ impl Iterator for LineIndexStepIter<'_> {
111 .as_mut() 113 .as_mut()
112 .and_then(|(newline, x)| { 114 .and_then(|(newline, x)| {
113 let x = x.next()?; 115 let x = x.next()?;
114 Some(Step::Utf16Char(TextRange::from_to(*newline + x.start, *newline + x.end))) 116 Some(Step::Utf16Char(TextRange::new(*newline + x.start, *newline + x.end)))
115 }) 117 })
116 .or_else(|| { 118 .or_else(|| {
117 let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?; 119 let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?;
@@ -129,7 +131,7 @@ impl Iterator for LineIndexStepIter<'_> {
129#[derive(Debug)] 131#[derive(Debug)]
130struct OffsetStepIter<'a> { 132struct OffsetStepIter<'a> {
131 text: &'a str, 133 text: &'a str,
132 offset: TextUnit, 134 offset: TextSize,
133} 135}
134 136
135impl Iterator for OffsetStepIter<'_> { 137impl Iterator for OffsetStepIter<'_> {
@@ -139,16 +141,17 @@ impl Iterator for OffsetStepIter<'_> {
139 .text 141 .text
140 .char_indices() 142 .char_indices()
141 .filter_map(|(i, c)| { 143 .filter_map(|(i, c)| {
144 let i: TextSize = i.try_into().unwrap();
145 let char_len = TextSize::of(c);
142 if c == '\n' { 146 if c == '\n' {
143 let next_offset = self.offset + TextUnit::from_usize(i + 1); 147 let next_offset = self.offset + i + char_len;
144 let next = Step::Newline(next_offset); 148 let next = Step::Newline(next_offset);
145 Some((next, next_offset)) 149 Some((next, next_offset))
146 } else { 150 } else {
147 let char_len = TextUnit::of_char(c); 151 if !c.is_ascii() {
148 if char_len > TextUnit::from_usize(1) { 152 let start = self.offset + i;
149 let start = self.offset + TextUnit::from_usize(i);
150 let end = start + char_len; 153 let end = start + char_len;
151 let next = Step::Utf16Char(TextRange::from_to(start, end)); 154 let next = Step::Utf16Char(TextRange::new(start, end));
152 let next_offset = end; 155 let next_offset = end;
153 Some((next, next_offset)) 156 Some((next, next_offset))
154 } else { 157 } else {
@@ -157,7 +160,7 @@ impl Iterator for OffsetStepIter<'_> {
157 } 160 }
158 }) 161 })
159 .next()?; 162 .next()?;
160 let next_idx = (next_offset - self.offset).to_usize(); 163 let next_idx: usize = (next_offset - self.offset).into();
161 self.text = &self.text[next_idx..]; 164 self.text = &self.text[next_idx..];
162 self.offset = next_offset; 165 self.offset = next_offset;
163 Some(next) 166 Some(next)
@@ -195,7 +198,7 @@ impl<'a> Edits<'a> {
195 match self.edits.split_first() { 198 match self.edits.split_first() {
196 Some((next, rest)) => { 199 Some((next, rest)) => {
197 let delete = self.translate_range(next.delete); 200 let delete = self.translate_range(next.delete);
198 let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64; 201 let diff = next.insert.len() as i64 - usize::from(next.delete.len()) as i64;
199 self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff }); 202 self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff });
200 self.edits = rest; 203 self.edits = rest;
201 } 204 }
@@ -244,15 +247,15 @@ impl<'a> Edits<'a> {
244 } else { 247 } else {
245 let start = self.translate(range.start()); 248 let start = self.translate(range.start());
246 let end = self.translate(range.end()); 249 let end = self.translate(range.end());
247 TextRange::from_to(start, end) 250 TextRange::new(start, end)
248 } 251 }
249 } 252 }
250 253
251 fn translate(&self, x: TextUnit) -> TextUnit { 254 fn translate(&self, x: TextSize) -> TextSize {
252 if self.acc_diff == 0 { 255 if self.acc_diff == 0 {
253 x 256 x
254 } else { 257 } else {
255 TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32) 258 TextSize::from((usize::from(x) as i64 + self.acc_diff) as u32)
256 } 259 }
257 } 260 }
258 261
@@ -271,29 +274,29 @@ impl<'a> Edits<'a> {
271#[derive(Debug)] 274#[derive(Debug)]
272struct RunningLineCol { 275struct RunningLineCol {
273 line: u32, 276 line: u32,
274 last_newline: TextUnit, 277 last_newline: TextSize,
275 col_adjust: TextUnit, 278 col_adjust: TextSize,
276} 279}
277 280
278impl RunningLineCol { 281impl RunningLineCol {
279 fn new() -> RunningLineCol { 282 fn new() -> RunningLineCol {
280 RunningLineCol { line: 0, last_newline: TextUnit::from(0), col_adjust: TextUnit::from(0) } 283 RunningLineCol { line: 0, last_newline: TextSize::from(0), col_adjust: TextSize::from(0) }
281 } 284 }
282 285
283 fn to_line_col(&self, offset: TextUnit) -> LineCol { 286 fn to_line_col(&self, offset: TextSize) -> LineCol {
284 LineCol { 287 LineCol {
285 line: self.line, 288 line: self.line,
286 col_utf16: ((offset - self.last_newline) - self.col_adjust).into(), 289 col_utf16: ((offset - self.last_newline) - self.col_adjust).into(),
287 } 290 }
288 } 291 }
289 292
290 fn add_line(&mut self, newline: TextUnit) { 293 fn add_line(&mut self, newline: TextSize) {
291 self.line += 1; 294 self.line += 1;
292 self.last_newline = newline; 295 self.last_newline = newline;
293 self.col_adjust = TextUnit::from(0); 296 self.col_adjust = TextSize::from(0);
294 } 297 }
295 298
296 fn adjust_col(&mut self, range: TextRange) { 299 fn adjust_col(&mut self, range: TextRange) {
297 self.col_adjust += range.len() - TextUnit::from(1); 300 self.col_adjust += range.len() - TextSize::from(1);
298 } 301 }
299} 302}
diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs
index 1bf014149..596f957b8 100644
--- a/crates/ra_ide_db/src/search.rs
+++ b/crates/ra_ide_db/src/search.rs
@@ -4,13 +4,13 @@
4//! get a super-set of matches. Then, we we confirm each match using precise 4//! get a super-set of matches. Then, we we confirm each match using precise
5//! name resolution. 5//! name resolution.
6 6
7use std::mem; 7use std::{convert::TryInto, mem};
8 8
9use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility}; 9use hir::{DefWithBody, HasSource, Module, ModuleSource, Semantics, Visibility};
10use once_cell::unsync::Lazy; 10use once_cell::unsync::Lazy;
11use ra_db::{FileId, FileRange, SourceDatabaseExt}; 11use ra_db::{FileId, FileRange, SourceDatabaseExt};
12use ra_prof::profile; 12use ra_prof::profile;
13use ra_syntax::{ast, match_ast, AstNode, TextRange, TextUnit}; 13use ra_syntax::{ast, match_ast, AstNode, TextRange, TextSize};
14use rustc_hash::FxHashMap; 14use rustc_hash::FxHashMap;
15use test_utils::tested_by; 15use test_utils::tested_by;
16 16
@@ -85,7 +85,7 @@ impl SearchScope {
85 match (r1, r2) { 85 match (r1, r2) {
86 (None, r) | (r, None) => Some(r), 86 (None, r) | (r, None) => Some(r),
87 (Some(r1), Some(r2)) => { 87 (Some(r1), Some(r2)) => {
88 let r = r1.intersection(&r2)?; 88 let r = r1.intersect(r2)?;
89 Some(Some(r)) 89 Some(Some(r))
90 } 90 }
91 } 91 }
@@ -201,13 +201,13 @@ impl Definition {
201 for (file_id, search_range) in search_scope { 201 for (file_id, search_range) in search_scope {
202 let text = db.file_text(file_id); 202 let text = db.file_text(file_id);
203 let search_range = 203 let search_range =
204 search_range.unwrap_or(TextRange::offset_len(0.into(), TextUnit::of_str(&text))); 204 search_range.unwrap_or(TextRange::up_to(TextSize::of(text.as_str())));
205 205
206 let sema = Semantics::new(db); 206 let sema = Semantics::new(db);
207 let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); 207 let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
208 208
209 for (idx, _) in text.match_indices(pat) { 209 for (idx, _) in text.match_indices(pat) {
210 let offset = TextUnit::from_usize(idx); 210 let offset: TextSize = idx.try_into().unwrap();
211 if !search_range.contains_inclusive(offset) { 211 if !search_range.contains_inclusive(offset) {
212 tested_by!(search_filters_by_range; force); 212 tested_by!(search_filters_by_range; force);
213 continue; 213 continue;