diff options
Diffstat (limited to 'crates/test_utils/src')
-rw-r--r-- | crates/test_utils/src/lib.rs | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 068eb80ce..ee73153f0 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs | |||
@@ -38,22 +38,44 @@ pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) { | |||
38 | } | 38 | } |
39 | 39 | ||
40 | pub fn extract_offset(text: &str) -> (TextUnit, String) { | 40 | pub fn extract_offset(text: &str) -> (TextUnit, String) { |
41 | let cursor = "<|>"; | 41 | match try_extract_offset(text) { |
42 | let cursor_pos = match text.find(cursor) { | ||
43 | None => panic!("text should contain cursor marker"), | 42 | None => panic!("text should contain cursor marker"), |
44 | Some(pos) => pos, | 43 | Some(result) => result, |
45 | }; | 44 | } |
45 | } | ||
46 | |||
47 | pub fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> { | ||
48 | let cursor = "<|>"; | ||
49 | let cursor_pos = text.find(cursor)?; | ||
46 | let mut new_text = String::with_capacity(text.len() - cursor.len()); | 50 | let mut new_text = String::with_capacity(text.len() - cursor.len()); |
47 | new_text.push_str(&text[..cursor_pos]); | 51 | new_text.push_str(&text[..cursor_pos]); |
48 | new_text.push_str(&text[cursor_pos + cursor.len()..]); | 52 | new_text.push_str(&text[cursor_pos + cursor.len()..]); |
49 | let cursor_pos = TextUnit::from(cursor_pos as u32); | 53 | let cursor_pos = TextUnit::from(cursor_pos as u32); |
50 | (cursor_pos, new_text) | 54 | Some((cursor_pos, new_text)) |
51 | } | 55 | } |
52 | 56 | ||
53 | pub fn extract_range(text: &str) -> (TextRange, String) { | 57 | pub fn extract_range(text: &str) -> (TextRange, String) { |
54 | let (start, text) = extract_offset(text); | 58 | match try_extract_range(text) { |
55 | let (end, text) = extract_offset(&text); | 59 | None => panic!("text should contain cursor marker"), |
56 | (TextRange::from_to(start, end), text) | 60 | Some(result) => result, |
61 | } | ||
62 | } | ||
63 | |||
64 | pub fn try_extract_range(text: &str) -> Option<(TextRange, String)> { | ||
65 | let (start, text) = try_extract_offset(text)?; | ||
66 | let (end, text) = try_extract_offset(&text)?; | ||
67 | Some((TextRange::from_to(start, end), text)) | ||
68 | } | ||
69 | |||
70 | pub fn extract_ranges(text: &str) -> (Vec<TextRange>, String) { | ||
71 | let mut ranges = Vec::new(); | ||
72 | let mut text = String::from(text); | ||
73 | while let Some((range, new_text)) = try_extract_range(&text) { | ||
74 | text = new_text; | ||
75 | ranges.push(range); | ||
76 | } | ||
77 | |||
78 | (ranges, text) | ||
57 | } | 79 | } |
58 | 80 | ||
59 | pub fn add_cursor(text: &str, offset: TextUnit) -> String { | 81 | pub fn add_cursor(text: &str, offset: TextUnit) -> String { |