aboutsummaryrefslogtreecommitdiff
path: root/crates/test_utils
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-10-26 18:58:12 +0100
committerGitHub <[email protected]>2019-10-26 18:58:12 +0100
commit106004139375b64817145d8279cd7904a477c637 (patch)
treeb8944447b98e8b0b25ac95d0465210312bf4cd57 /crates/test_utils
parent733fd64260793a0f7335e4f75ba9197d5fa98b70 (diff)
parent4a83aae09849123dbbbc5726b07c2601a14397a8 (diff)
Merge #2078
2078: support range selection in assist docs r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/test_utils')
-rw-r--r--crates/test_utils/src/lib.rs34
1 files changed, 31 insertions, 3 deletions
diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs
index c40943b63..1244ea8cf 100644
--- a/crates/test_utils/src/lib.rs
+++ b/crates/test_utils/src/lib.rs
@@ -1,4 +1,10 @@
1//! FIXME: write short doc here 1//! Assorted testing utilities.
2//!
3//! Most notable things are:
4//!
5//! * Rich text comparison, which outputs a diff.
6//! * Extracting markup (mainly, `<|>` markers) out of fixture strings.
7//! * marks (see the eponymous module).
2 8
3#[macro_use] 9#[macro_use]
4pub mod marks; 10pub mod marks;
@@ -43,7 +49,7 @@ pub fn extract_offset(text: &str) -> (TextUnit, String) {
43 } 49 }
44} 50}
45 51
46pub fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> { 52fn try_extract_offset(text: &str) -> Option<(TextUnit, String)> {
47 let cursor_pos = text.find(CURSOR_MARKER)?; 53 let cursor_pos = text.find(CURSOR_MARKER)?;
48 let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len()); 54 let mut new_text = String::with_capacity(text.len() - CURSOR_MARKER.len());
49 new_text.push_str(&text[..cursor_pos]); 55 new_text.push_str(&text[..cursor_pos]);
@@ -59,12 +65,34 @@ pub fn extract_range(text: &str) -> (TextRange, String) {
59 } 65 }
60} 66}
61 67
62pub fn try_extract_range(text: &str) -> Option<(TextRange, String)> { 68fn try_extract_range(text: &str) -> Option<(TextRange, String)> {
63 let (start, text) = try_extract_offset(text)?; 69 let (start, text) = try_extract_offset(text)?;
64 let (end, text) = try_extract_offset(&text)?; 70 let (end, text) = try_extract_offset(&text)?;
65 Some((TextRange::from_to(start, end), text)) 71 Some((TextRange::from_to(start, end), text))
66} 72}
67 73
74pub enum RangeOrOffset {
75 Range(TextRange),
76 Offset(TextUnit),
77}
78
79impl From<RangeOrOffset> for TextRange {
80 fn from(selection: RangeOrOffset) -> Self {
81 match selection {
82 RangeOrOffset::Range(it) => it,
83 RangeOrOffset::Offset(it) => TextRange::from_to(it, it),
84 }
85 }
86}
87
88pub fn extract_range_or_offset(text: &str) -> (RangeOrOffset, String) {
89 if let Some((range, text)) = try_extract_range(text) {
90 return (RangeOrOffset::Range(range), text);
91 }
92 let (offset, text) = extract_offset(text);
93 (RangeOrOffset::Offset(offset), text)
94}
95
68/// Extracts ranges, marked with `<tag> </tag>` paris from the `text` 96/// Extracts ranges, marked with `<tag> </tag>` paris from the `text`
69pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) { 97pub fn extract_ranges(mut text: &str, tag: &str) -> (Vec<TextRange>, String) {
70 let open = format!("<{}>", tag); 98 let open = format!("<{}>", tag);