aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_db
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-04-24 22:40:41 +0100
committerAleksey Kladov <[email protected]>2020-04-25 10:59:18 +0100
commitb1d5817dd18b7b5fc102a63b084b1ee7ff4f9996 (patch)
treee5d136c5ba4a6ba96aeeb423e6e3f64ca7cea3f9 /crates/ra_ide_db
parent27a7718880d93f55f905da606d108d3b3c682ab4 (diff)
Convert code to text-size
Diffstat (limited to 'crates/ra_ide_db')
-rw-r--r--crates/ra_ide_db/src/line_index.rs54
-rw-r--r--crates/ra_ide_db/src/line_index_utils.rs48
-rw-r--r--crates/ra_ide_db/src/search.rs9
3 files changed, 55 insertions, 56 deletions
diff --git a/crates/ra_ide_db/src/line_index.rs b/crates/ra_ide_db/src/line_index.rs
index 8ae745ff2..7794dc9fd 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// TODO: un TextSize
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,7 @@ 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 curr_row += TextSize::of(c);
46 if c == '\n' { 46 if c == '\n' {
47 newlines.push(curr_row); 47 newlines.push(curr_row);
48 48
@@ -58,8 +58,8 @@ impl LineIndex {
58 continue; 58 continue;
59 } 59 }
60 60
61 let char_len = TextUnit::of_char(c); 61 let char_len = TextSize::of(c);
62 if char_len > TextUnit::from_usize(1) { 62 if char_len > TextSize::from_usize(1) {
63 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len }); 63 utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
64 } 64 }
65 65
@@ -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,16 +97,16 @@ 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 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 105 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
106 let mut correction = 0; 106 let mut correction = 0;
107 for c in utf16_chars { 107 for c in utf16_chars {
108 if col >= c.end { 108 if col >= c.end {
109 correction += c.len().to_usize() - 1; 109 correction += 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
@@ -114,18 +114,18 @@ impl LineIndex {
114 } 114 }
115 } 115 }
116 116
117 col.to_usize() - correction 117 usize::from(col) - correction
118 } else { 118 } else {
119 col.to_usize() 119 usize::from(col)
120 } 120 }
121 } 121 }
122 122
123 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextUnit { 123 fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextSize {
124 let mut col: TextUnit = col.into(); 124 let mut col: TextSize = col.into();
125 if let Some(utf16_chars) = self.utf16_lines.get(&line) { 125 if let Some(utf16_chars) = self.utf16_lines.get(&line) {
126 for c in utf16_chars { 126 for c in utf16_chars {
127 if col >= c.start { 127 if col >= c.start {
128 col += c.len() - TextUnit::from_usize(1); 128 col += c.len() - TextSize::from_usize(1);
129 } else { 129 } else {
130 // From here on, all utf16 characters come *after* the character we are mapping, 130 // From here on, all utf16 characters come *after* the character we are mapping,
131 // so we don't need to take them into account 131 // so we don't need to take them into account
@@ -200,10 +200,10 @@ const C: char = 'メ';
200 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20); 200 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20);
201 201
202 // UTF-16 to UTF-8, no changes 202 // UTF-16 to UTF-8, no changes
203 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from(15)); 203 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
204 204
205 // UTF-16 to UTF-8 205 // UTF-16 to UTF-8
206 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from(21)); 206 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21));
207 } 207 }
208 208
209 #[test] 209 #[test]
@@ -228,18 +228,18 @@ const C: char = \"メ メ\";
228 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15); 228 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
229 229
230 // UTF-16 to UTF-8 230 // UTF-16 to UTF-8
231 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from_usize(15)); 231 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from_usize(15));
232 232
233 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextUnit::from_usize(20)); 233 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from_usize(20));
234 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from_usize(23)); 234 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from_usize(23));
235 235
236 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15)); 236 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from_usize(15));
237 } 237 }
238 238
239 #[test] 239 #[test]
240 fn test_splitlines() { 240 fn test_splitlines() {
241 fn r(lo: u32, hi: u32) -> TextRange { 241 fn r(lo: u32, hi: u32) -> TextRange {
242 TextRange::from_to(lo.into(), hi.into()) 242 TextRange::new(lo.into(), hi.into())
243 } 243 }
244 244
245 let text = "a\nbb\nccc\n"; 245 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..f050fe77f 100644
--- a/crates/ra_ide_db/src/line_index_utils.rs
+++ b/crates/ra_ide_db/src/line_index_utils.rs
@@ -1,20 +1,20 @@
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 ra_syntax::{TextRange, TextSize};
11use ra_text_edit::{AtomTextEdit, TextEdit}; 11use ra_text_edit::{AtomTextEdit, TextEdit};
12 12
13use crate::line_index::{LineCol, LineIndex, Utf16Char}; 13use crate::line_index::{LineCol, LineIndex, Utf16Char};
14 14
15pub fn translate_offset_with_edit( 15pub fn translate_offset_with_edit(
16 line_index: &LineIndex, 16 line_index: &LineIndex,
17 offset: TextUnit, 17 offset: TextSize,
18 text_edit: &TextEdit, 18 text_edit: &TextEdit,
19) -> LineCol { 19) -> LineCol {
20 let mut state = Edits::from_text_edit(&text_edit); 20 let mut state = Edits::from_text_edit(&text_edit);
@@ -84,7 +84,7 @@ pub fn translate_offset_with_edit(
84 84
85#[derive(Debug, Clone)] 85#[derive(Debug, Clone)]
86enum Step { 86enum Step {
87 Newline(TextUnit), 87 Newline(TextSize),
88 Utf16Char(TextRange), 88 Utf16Char(TextRange),
89} 89}
90 90
@@ -92,7 +92,7 @@ enum Step {
92struct LineIndexStepIter<'a> { 92struct LineIndexStepIter<'a> {
93 line_index: &'a LineIndex, 93 line_index: &'a LineIndex,
94 next_newline_idx: usize, 94 next_newline_idx: usize,
95 utf16_chars: Option<(TextUnit, std::slice::Iter<'a, Utf16Char>)>, 95 utf16_chars: Option<(TextSize, std::slice::Iter<'a, Utf16Char>)>,
96} 96}
97 97
98impl LineIndexStepIter<'_> { 98impl LineIndexStepIter<'_> {
@@ -111,7 +111,7 @@ impl Iterator for LineIndexStepIter<'_> {
111 .as_mut() 111 .as_mut()
112 .and_then(|(newline, x)| { 112 .and_then(|(newline, x)| {
113 let x = x.next()?; 113 let x = x.next()?;
114 Some(Step::Utf16Char(TextRange::from_to(*newline + x.start, *newline + x.end))) 114 Some(Step::Utf16Char(TextRange::new(*newline + x.start, *newline + x.end)))
115 }) 115 })
116 .or_else(|| { 116 .or_else(|| {
117 let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?; 117 let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?;
@@ -129,7 +129,7 @@ impl Iterator for LineIndexStepIter<'_> {
129#[derive(Debug)] 129#[derive(Debug)]
130struct OffsetStepIter<'a> { 130struct OffsetStepIter<'a> {
131 text: &'a str, 131 text: &'a str,
132 offset: TextUnit, 132 offset: TextSize,
133} 133}
134 134
135impl Iterator for OffsetStepIter<'_> { 135impl Iterator for OffsetStepIter<'_> {
@@ -140,15 +140,15 @@ impl Iterator for OffsetStepIter<'_> {
140 .char_indices() 140 .char_indices()
141 .filter_map(|(i, c)| { 141 .filter_map(|(i, c)| {
142 if c == '\n' { 142 if c == '\n' {
143 let next_offset = self.offset + TextUnit::from_usize(i + 1); 143 let next_offset = self.offset + TextSize::from_usize(i + 1);
144 let next = Step::Newline(next_offset); 144 let next = Step::Newline(next_offset);
145 Some((next, next_offset)) 145 Some((next, next_offset))
146 } else { 146 } else {
147 let char_len = TextUnit::of_char(c); 147 let char_len = TextSize::of(c);
148 if char_len > TextUnit::from_usize(1) { 148 if char_len > TextSize::from_usize(1) {
149 let start = self.offset + TextUnit::from_usize(i); 149 let start = self.offset + TextSize::from_usize(i);
150 let end = start + char_len; 150 let end = start + char_len;
151 let next = Step::Utf16Char(TextRange::from_to(start, end)); 151 let next = Step::Utf16Char(TextRange::new(start, end));
152 let next_offset = end; 152 let next_offset = end;
153 Some((next, next_offset)) 153 Some((next, next_offset))
154 } else { 154 } else {
@@ -157,7 +157,7 @@ impl Iterator for OffsetStepIter<'_> {
157 } 157 }
158 }) 158 })
159 .next()?; 159 .next()?;
160 let next_idx = (next_offset - self.offset).to_usize(); 160 let next_idx: usize = (next_offset - self.offset).into();
161 self.text = &self.text[next_idx..]; 161 self.text = &self.text[next_idx..];
162 self.offset = next_offset; 162 self.offset = next_offset;
163 Some(next) 163 Some(next)
@@ -195,7 +195,7 @@ impl<'a> Edits<'a> {
195 match self.edits.split_first() { 195 match self.edits.split_first() {
196 Some((next, rest)) => { 196 Some((next, rest)) => {
197 let delete = self.translate_range(next.delete); 197 let delete = self.translate_range(next.delete);
198 let diff = next.insert.len() as i64 - next.delete.len().to_usize() as i64; 198 let diff = next.insert.len() as i64 - usize::from(next.delete.len()) as i64;
199 self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff }); 199 self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff });
200 self.edits = rest; 200 self.edits = rest;
201 } 201 }
@@ -244,15 +244,15 @@ impl<'a> Edits<'a> {
244 } else { 244 } else {
245 let start = self.translate(range.start()); 245 let start = self.translate(range.start());
246 let end = self.translate(range.end()); 246 let end = self.translate(range.end());
247 TextRange::from_to(start, end) 247 TextRange::new(start, end)
248 } 248 }
249 } 249 }
250 250
251 fn translate(&self, x: TextUnit) -> TextUnit { 251 fn translate(&self, x: TextSize) -> TextSize {
252 if self.acc_diff == 0 { 252 if self.acc_diff == 0 {
253 x 253 x
254 } else { 254 } else {
255 TextUnit::from((x.to_usize() as i64 + self.acc_diff) as u32) 255 TextSize::from((usize::from(x) as i64 + self.acc_diff) as u32)
256 } 256 }
257 } 257 }
258 258
@@ -271,29 +271,29 @@ impl<'a> Edits<'a> {
271#[derive(Debug)] 271#[derive(Debug)]
272struct RunningLineCol { 272struct RunningLineCol {
273 line: u32, 273 line: u32,
274 last_newline: TextUnit, 274 last_newline: TextSize,
275 col_adjust: TextUnit, 275 col_adjust: TextSize,
276} 276}
277 277
278impl RunningLineCol { 278impl RunningLineCol {
279 fn new() -> RunningLineCol { 279 fn new() -> RunningLineCol {
280 RunningLineCol { line: 0, last_newline: TextUnit::from(0), col_adjust: TextUnit::from(0) } 280 RunningLineCol { line: 0, last_newline: TextSize::from(0), col_adjust: TextSize::from(0) }
281 } 281 }
282 282
283 fn to_line_col(&self, offset: TextUnit) -> LineCol { 283 fn to_line_col(&self, offset: TextSize) -> LineCol {
284 LineCol { 284 LineCol {
285 line: self.line, 285 line: self.line,
286 col_utf16: ((offset - self.last_newline) - self.col_adjust).into(), 286 col_utf16: ((offset - self.last_newline) - self.col_adjust).into(),
287 } 287 }
288 } 288 }
289 289
290 fn add_line(&mut self, newline: TextUnit) { 290 fn add_line(&mut self, newline: TextSize) {
291 self.line += 1; 291 self.line += 1;
292 self.last_newline = newline; 292 self.last_newline = newline;
293 self.col_adjust = TextUnit::from(0); 293 self.col_adjust = TextSize::from(0);
294 } 294 }
295 295
296 fn adjust_col(&mut self, range: TextRange) { 296 fn adjust_col(&mut self, range: TextRange) {
297 self.col_adjust += range.len() - TextUnit::from(1); 297 self.col_adjust += range.len() - TextSize::from(1);
298 } 298 }
299} 299}
diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs
index 1bf014149..c66de4f42 100644
--- a/crates/ra_ide_db/src/search.rs
+++ b/crates/ra_ide_db/src/search.rs
@@ -10,7 +10,7 @@ use 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 }
@@ -200,14 +200,13 @@ impl Definition {
200 200
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 = search_range.unwrap_or(TextRange::up_to(TextSize::of(&text)));
204 search_range.unwrap_or(TextRange::offset_len(0.into(), TextUnit::of_str(&text)));
205 204
206 let sema = Semantics::new(db); 205 let sema = Semantics::new(db);
207 let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); 206 let tree = Lazy::new(|| sema.parse(file_id).syntax().clone());
208 207
209 for (idx, _) in text.match_indices(pat) { 208 for (idx, _) in text.match_indices(pat) {
210 let offset = TextUnit::from_usize(idx); 209 let offset = TextSize::from_usize(idx);
211 if !search_range.contains_inclusive(offset) { 210 if !search_range.contains_inclusive(offset) {
212 tested_by!(search_filters_by_range; force); 211 tested_by!(search_filters_by_range; force);
213 continue; 212 continue;