diff options
Diffstat (limited to 'crates/ide_db')
-rw-r--r-- | crates/ide_db/Cargo.toml | 3 | ||||
-rw-r--r-- | crates/ide_db/src/apply_change.rs | 2 | ||||
-rw-r--r-- | crates/ide_db/src/line_index.rs | 29 | ||||
-rw-r--r-- | crates/ide_db/src/line_index/tests.rs | 33 | ||||
-rw-r--r-- | crates/ide_db/src/search.rs | 2 |
5 files changed, 43 insertions, 26 deletions
diff --git a/crates/ide_db/Cargo.toml b/crates/ide_db/Cargo.toml index d3d3dc688..d4612a75e 100644 --- a/crates/ide_db/Cargo.toml +++ b/crates/ide_db/Cargo.toml | |||
@@ -9,9 +9,6 @@ edition = "2018" | |||
9 | [lib] | 9 | [lib] |
10 | doctest = false | 10 | doctest = false |
11 | 11 | ||
12 | [features] | ||
13 | wasm = [] | ||
14 | |||
15 | [dependencies] | 12 | [dependencies] |
16 | log = "0.4.8" | 13 | log = "0.4.8" |
17 | rayon = "1.5.0" | 14 | rayon = "1.5.0" |
diff --git a/crates/ide_db/src/apply_change.rs b/crates/ide_db/src/apply_change.rs index 9d9b6de7a..23974cff8 100644 --- a/crates/ide_db/src/apply_change.rs +++ b/crates/ide_db/src/apply_change.rs | |||
@@ -67,7 +67,7 @@ impl RootDatabase { | |||
67 | } | 67 | } |
68 | 68 | ||
69 | pub fn collect_garbage(&mut self) { | 69 | pub fn collect_garbage(&mut self) { |
70 | if cfg!(feature = "wasm") { | 70 | if cfg!(target_arch = "wasm32") { |
71 | return; | 71 | return; |
72 | } | 72 | } |
73 | 73 | ||
diff --git a/crates/ide_db/src/line_index.rs b/crates/ide_db/src/line_index.rs index 41226305e..8e9d8cca2 100644 --- a/crates/ide_db/src/line_index.rs +++ b/crates/ide_db/src/line_index.rs | |||
@@ -15,11 +15,19 @@ pub struct LineIndex { | |||
15 | } | 15 | } |
16 | 16 | ||
17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
18 | pub struct LineCol { | 18 | pub struct LineColUtf16 { |
19 | /// Zero-based | 19 | /// Zero-based |
20 | pub line: u32, | 20 | pub line: u32, |
21 | /// Zero-based | 21 | /// Zero-based |
22 | pub col_utf16: u32, | 22 | pub col: u32, |
23 | } | ||
24 | |||
25 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
26 | pub struct LineCol { | ||
27 | /// Zero-based | ||
28 | pub line: u32, | ||
29 | /// Zero-based utf8 offset | ||
30 | pub col: u32, | ||
23 | } | 31 | } |
24 | 32 | ||
25 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | 33 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] |
@@ -92,14 +100,21 @@ impl LineIndex { | |||
92 | let line = partition_point(&self.newlines, |&it| it <= offset) - 1; | 100 | let line = partition_point(&self.newlines, |&it| it <= offset) - 1; |
93 | let line_start_offset = self.newlines[line]; | 101 | let line_start_offset = self.newlines[line]; |
94 | let col = offset - line_start_offset; | 102 | let col = offset - line_start_offset; |
95 | 103 | LineCol { line: line as u32, col: col.into() } | |
96 | LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 } | ||
97 | } | 104 | } |
98 | 105 | ||
99 | pub fn offset(&self, line_col: LineCol) -> TextSize { | 106 | pub fn offset(&self, line_col: LineCol) -> TextSize { |
100 | //FIXME: return Result | 107 | self.newlines[line_col.line as usize] + TextSize::from(line_col.col) |
101 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16); | 108 | } |
102 | self.newlines[line_col.line as usize] + col | 109 | |
110 | pub fn to_utf16(&self, line_col: LineCol) -> LineColUtf16 { | ||
111 | let col = self.utf8_to_utf16_col(line_col.line, line_col.col.into()); | ||
112 | LineColUtf16 { line: line_col.line, col: col as u32 } | ||
113 | } | ||
114 | |||
115 | pub fn to_utf8(&self, line_col: LineColUtf16) -> LineCol { | ||
116 | let col = self.utf16_to_utf8_col(line_col.line, line_col.col); | ||
117 | LineCol { line: line_col.line, col: col.into() } | ||
103 | } | 118 | } |
104 | 119 | ||
105 | pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { | 120 | pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ { |
diff --git a/crates/ide_db/src/line_index/tests.rs b/crates/ide_db/src/line_index/tests.rs index 05f7484e8..09f3bca62 100644 --- a/crates/ide_db/src/line_index/tests.rs +++ b/crates/ide_db/src/line_index/tests.rs | |||
@@ -3,24 +3,29 @@ use super::*; | |||
3 | #[test] | 3 | #[test] |
4 | fn test_line_index() { | 4 | fn test_line_index() { |
5 | let text = "hello\nworld"; | 5 | let text = "hello\nworld"; |
6 | let table = [ | ||
7 | (00, 0, 0), | ||
8 | (01, 0, 1), | ||
9 | (05, 0, 5), | ||
10 | (06, 1, 0), | ||
11 | (07, 1, 1), | ||
12 | (08, 1, 2), | ||
13 | (10, 1, 4), | ||
14 | (11, 1, 5), | ||
15 | (12, 1, 6), | ||
16 | ]; | ||
17 | |||
6 | let index = LineIndex::new(text); | 18 | let index = LineIndex::new(text); |
7 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | 19 | for &(offset, line, col) in &table { |
8 | assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 }); | 20 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
9 | assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 }); | 21 | } |
10 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 }); | ||
11 | assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 }); | ||
12 | assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 }); | ||
13 | assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 }); | ||
14 | assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 }); | ||
15 | assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 }); | ||
16 | 22 | ||
17 | let text = "\nhello\nworld"; | 23 | let text = "\nhello\nworld"; |
24 | let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)]; | ||
18 | let index = LineIndex::new(text); | 25 | let index = LineIndex::new(text); |
19 | assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 }); | 26 | for &(offset, line, col) in &table { |
20 | assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 }); | 27 | assert_eq!(index.line_col(offset.into()), LineCol { line, col }); |
21 | assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 }); | 28 | } |
22 | assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 }); | ||
23 | assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 }); | ||
24 | } | 29 | } |
25 | 30 | ||
26 | #[test] | 31 | #[test] |
diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 38b20f2dc..22dd172f7 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs | |||
@@ -345,7 +345,7 @@ impl<'a> FindUsages<'a> { | |||
345 | for (file_id, search_range) in search_scope { | 345 | for (file_id, search_range) in search_scope { |
346 | let text = sema.db.file_text(file_id); | 346 | let text = sema.db.file_text(file_id); |
347 | let search_range = | 347 | let search_range = |
348 | search_range.unwrap_or(TextRange::up_to(TextSize::of(text.as_str()))); | 348 | search_range.unwrap_or_else(|| TextRange::up_to(TextSize::of(text.as_str()))); |
349 | 349 | ||
350 | let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); | 350 | let tree = Lazy::new(|| sema.parse(file_id).syntax().clone()); |
351 | 351 | ||