aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide_db/src/change.rs3
-rw-r--r--crates/ra_ide_db/src/feature_flags.rs2
-rw-r--r--crates/ra_ide_db/src/line_index_utils.rs151
-rw-r--r--crates/ra_ide_db/src/symbol_index.rs1
-rw-r--r--crates/ra_ide_db/src/wasm_shims.rs2
5 files changed, 84 insertions, 75 deletions
diff --git a/crates/ra_ide_db/src/change.rs b/crates/ra_ide_db/src/change.rs
index 95a6ff287..4668784d3 100644
--- a/crates/ra_ide_db/src/change.rs
+++ b/crates/ra_ide_db/src/change.rs
@@ -1,4 +1,5 @@
1//! FIXME: write short doc here 1//! Defines a unit of change that can applied to a state of IDE to get the next
2//! state. Changes are transactional.
2 3
3use std::{fmt, sync::Arc, time}; 4use std::{fmt, sync::Arc, time};
4 5
diff --git a/crates/ra_ide_db/src/feature_flags.rs b/crates/ra_ide_db/src/feature_flags.rs
index 85617640d..1b3cabf4d 100644
--- a/crates/ra_ide_db/src/feature_flags.rs
+++ b/crates/ra_ide_db/src/feature_flags.rs
@@ -1,4 +1,4 @@
1//! FIXME: write short doc here 1//! See docs for `FeatureFlags`.
2 2
3use rustc_hash::FxHashMap; 3use rustc_hash::FxHashMap;
4 4
diff --git a/crates/ra_ide_db/src/line_index_utils.rs b/crates/ra_ide_db/src/line_index_utils.rs
index effbef11d..435b06511 100644
--- a/crates/ra_ide_db/src/line_index_utils.rs
+++ b/crates/ra_ide_db/src/line_index_utils.rs
@@ -1,10 +1,87 @@
1//! FIXME: write short doc here 1//! Code actions can specify desirable final position of the cursor.
2//!
3//! The position is specified as a `TextUnit` in the final file. We need to send
4//! it in `(Line, Column)` coordinate though. However, we only have a LineIndex
5//! for a file pre-edit!
6//!
7//! Code in this module applies this "to (Line, Column) after edit"
8//! transformation.
2 9
3use ra_syntax::{TextRange, TextUnit}; 10use ra_syntax::{TextRange, TextUnit};
4use ra_text_edit::{AtomTextEdit, TextEdit}; 11use ra_text_edit::{AtomTextEdit, TextEdit};
5 12
6use crate::line_index::{LineCol, LineIndex, Utf16Char}; 13use crate::line_index::{LineCol, LineIndex, Utf16Char};
7 14
15pub fn translate_offset_with_edit(
16 line_index: &LineIndex,
17 offset: TextUnit,
18 text_edit: &TextEdit,
19) -> LineCol {
20 let mut state = Edits::from_text_edit(&text_edit);
21
22 let mut res = RunningLineCol::new();
23
24 macro_rules! test_step {
25 ($x:ident) => {
26 match &$x {
27 Step::Newline(n) => {
28 if offset < *n {
29 return res.to_line_col(offset);
30 } else {
31 res.add_line(*n);
32 }
33 }
34 Step::Utf16Char(x) => {
35 if offset < x.end() {
36 // if the offset is inside a multibyte char it's invalid
37 // clamp it to the start of the char
38 let clamp = offset.min(x.start());
39 return res.to_line_col(clamp);
40 } else {
41 res.adjust_col(*x);
42 }
43 }
44 }
45 };
46 }
47
48 for orig_step in LineIndexStepIter::from(line_index) {
49 loop {
50 let translated_step = state.translate_step(&orig_step);
51 match state.next_steps(&translated_step) {
52 NextSteps::Use => {
53 test_step!(translated_step);
54 break;
55 }
56 NextSteps::ReplaceMany(ns) => {
57 for n in ns {
58 test_step!(n);
59 }
60 break;
61 }
62 NextSteps::AddMany(ns) => {
63 for n in ns {
64 test_step!(n);
65 }
66 }
67 }
68 }
69 }
70
71 loop {
72 match state.next_inserted_steps() {
73 None => break,
74 Some(ns) => {
75 for n in ns {
76 test_step!(n);
77 }
78 }
79 }
80 }
81
82 res.to_line_col(offset)
83}
84
8#[derive(Debug, Clone)] 85#[derive(Debug, Clone)]
9enum Step { 86enum Step {
10 Newline(TextUnit), 87 Newline(TextUnit),
@@ -55,7 +132,7 @@ struct OffsetStepIter<'a> {
55 offset: TextUnit, 132 offset: TextUnit,
56} 133}
57 134
58impl<'a> Iterator for OffsetStepIter<'a> { 135impl Iterator for OffsetStepIter<'_> {
59 type Item = Step; 136 type Item = Step;
60 fn next(&mut self) -> Option<Step> { 137 fn next(&mut self) -> Option<Step> {
61 let (next, next_offset) = self 138 let (next, next_offset) = self
@@ -221,76 +298,6 @@ impl RunningLineCol {
221 } 298 }
222} 299}
223 300
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)] 301#[cfg(test)]
295mod test { 302mod test {
296 use proptest::{prelude::*, proptest}; 303 use proptest::{prelude::*, proptest};
diff --git a/crates/ra_ide_db/src/symbol_index.rs b/crates/ra_ide_db/src/symbol_index.rs
index ddad03633..64ddf2f95 100644
--- a/crates/ra_ide_db/src/symbol_index.rs
+++ b/crates/ra_ide_db/src/symbol_index.rs
@@ -19,6 +19,7 @@
19//! for each library (which is assumed to never change) and an FST for each Rust 19//! for each library (which is assumed to never change) and an FST for each Rust
20//! file in the current workspace, and run a query against the union of all 20//! file in the current workspace, and run a query against the union of all
21//! those FSTs. 21//! those FSTs.
22
22use std::{ 23use std::{
23 fmt, 24 fmt,
24 hash::{Hash, Hasher}, 25 hash::{Hash, Hasher},
diff --git a/crates/ra_ide_db/src/wasm_shims.rs b/crates/ra_ide_db/src/wasm_shims.rs
index 088cc9be4..7af9f9d9b 100644
--- a/crates/ra_ide_db/src/wasm_shims.rs
+++ b/crates/ra_ide_db/src/wasm_shims.rs
@@ -1,4 +1,4 @@
1//! FIXME: write short doc here 1//! A version of `std::time::Instant` that doesn't panic in WASM.
2 2
3#[cfg(not(feature = "wasm"))] 3#[cfg(not(feature = "wasm"))]
4pub use std::time::Instant; 4pub use std::time::Instant;