aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils/src
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/test_utils/src
parent27a7718880d93f55f905da606d108d3b3c682ab4 (diff)
Convert code to text-size
Diffstat (limited to 'crates/test_utils/src')
-rw-r--r--crates/test_utils/src/lib.rs26
1 files changed, 13 insertions, 13 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index 4164bfd5e..b1365444a 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -15,7 +15,7 @@ use std::{
15}; 15};
16 16
17use serde_json::Value; 17use serde_json::Value;
18use text_unit::{TextRange, TextUnit}; 18use text_size::{TextRange, TextSize};
19 19
20pub use difference::Changeset as __Changeset; 20pub use difference::Changeset as __Changeset;
21 21
@@ -49,7 +49,7 @@ macro_rules! assert_eq_text {
49} 49}
50 50
51/// Infallible version of `try_extract_offset()`. 51/// Infallible version of `try_extract_offset()`.
52pub fn extract_offset(text: &str) -> (TextUnit, String) { 52pub fn extract_offset(text: &str) -> (TextSize, String) {
53 match try_extract_offset(text) { 53 match try_extract_offset(text) {
54 None => panic!("text should contain cursor marker"), 54 None => panic!("text should contain cursor marker"),
55 Some(result) => result, 55 Some(result) => result,
@@ -58,12 +58,12 @@ pub fn extract_offset(text: &str) -> (TextUnit, String) {
58 58
59/// Returns the offset of the first occurence of `<|>` marker and the copy of `text` 59/// Returns the offset of the first occurence of `<|>` marker and the copy of `text`
60/// without the marker. 60/// without the marker.
61fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> { 61fn try_extract_offset(text: &str) -> Option<(TextSize, String)> {
62 let cursor_pos = text.find(CURSOR_MARKER)?; 62 let cursor_pos = text.find(CURSOR_MARKER)?;
63 let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len()); 63 let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len());
64 new_text.push_str(&text[..cursor_pos]); 64 new_text.push_str(&text[..cursor_pos]);
65 new_text.push_str(&text[cursor_pos + CURSOR_MARKER.len()..]); 65 new_text.push_str(&text[cursor_pos + CURSOR_MARKER.len()..]);
66 let cursor_pos = TextUnit::from(cursor_pos as u32); 66 let cursor_pos = TextSize::from(cursor_pos as u32);
67 Some((cursor_pos, new_text)) 67 Some((cursor_pos, new_text))
68} 68}
69 69
@@ -80,25 +80,25 @@ pub fn extract_range(text: &str) -> (TextRange, String) {
80fn try_extract_range(text: &str) -> Option<(TextRange, String)> { 80fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
81 let (start, text) = try_extract_offset(text)?; 81 let (start, text) = try_extract_offset(text)?;
82 let (end, text) = try_extract_offset(&text)?; 82 let (end, text) = try_extract_offset(&text)?;
83 Some((TextRange::from_to(start, end), text)) 83 Some((TextRange::new(start, end), text))
84} 84}
85 85
86#[derive(Clone, Copy)] 86#[derive(Clone, Copy)]
87pub enum RangeOrOffset { 87pub enum RangeOrOffset {
88 Range(TextRange), 88 Range(TextRange),
89 Offset(TextUnit), 89 Offset(TextSize),
90} 90}
91 91
92impl From<RangeOrOffset> for TextRange { 92impl From<RangeOrOffset> for TextRange {
93 fn from(selection: RangeOrOffset) -> Self { 93 fn from(selection: RangeOrOffset) -> Self {
94 match selection { 94 match selection {
95 RangeOrOffset::Range(it) => it, 95 RangeOrOffset::Range(it) => it,
96 RangeOrOffset::Offset(it) => TextRange::from_to(it, it), 96 RangeOrOffset::Offset(it) => TextRange::new(it, it),
97 } 97 }
98 } 98 }
99} 99}
100 100
101/// Extracts `TextRange` or `TextUnit` depending on the amount of `<|>` markers 101/// Extracts `TextRange` or `TextSize` depending on the amount of `<|>` markers
102/// found in `text`. 102/// found in `text`.
103/// 103///
104/// # Panics 104/// # Panics
@@ -129,13 +129,13 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
129 text = &text[i..]; 129 text = &text[i..];
130 if text.starts_with(&open) { 130 if text.starts_with(&open) {
131 text = &text[open.len()..]; 131 text = &text[open.len()..];
132 let from = TextUnit::of_str(&res); 132 let from = TextSize::of(&res);
133 stack.push(from); 133 stack.push(from);
134 } else if text.starts_with(&close) { 134 } else if text.starts_with(&close) {
135 text = &text[close.len()..]; 135 text = &text[close.len()..];
136 let from = stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag)); 136 let from = stack.pop().unwrap_or_else(|| panic!("unmatched </{}>", tag));
137 let to = TextUnit::of_str(&res); 137 let to = TextSize::of(&res);
138 ranges.push(TextRange::from_to(from, to)); 138 ranges.push(TextRange::new(from, to));
139 } 139 }
140 } 140 }
141 } 141 }
@@ -146,8 +146,8 @@ pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
146} 146}
147 147
148/// Inserts `<|>` marker into the `text` at `offset`. 148/// Inserts `<|>` marker into the `text` at `offset`.
149pub fn add_cursor(text: &str, offset: TextUnit) -> String { 149pub fn add_cursor(text: &str, offset: TextSize) -> String {
150 let offset: usize = offset.to_usize(); 150 let offset: usize = offset.into();
151 let mut res = String::new(); 151 let mut res = String::new();
152 res.push_str(&text[..offset]); 152 res.push_str(&text[..offset]);
153 res.push_str("<|>"); 153 res.push_str("<|>");