From 5211e7d97771aa7f8d7cc99e5131fb3cc71a1627 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Aug 2018 14:30:54 +0300 Subject: move --- crates/test_utils/Cargo.toml | 1 + crates/test_utils/src/lib.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'crates/test_utils') diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 3d336a9b4..41316581e 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml @@ -6,3 +6,4 @@ authors = ["Aleksey Kladov "] [dependencies] difference = "2.0.0" itertools = "0.7.8" +text_unit = "0.1.2" diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 26b9bfb38..068eb80ce 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -1,8 +1,10 @@ extern crate difference; extern crate itertools; +extern crate text_unit; use std::fmt; use itertools::Itertools; +use text_unit::{TextUnit, TextRange}; pub use self::difference::Changeset as __Changeset; @@ -34,3 +36,32 @@ pub fn assert_eq_dbg(expected: &str, actual: &impl fmt::Debug) { let expected = expected.lines().map(|l| l.trim()).join(" "); assert_eq!(expected, actual); } + +pub fn extract_offset(text: &str) -> (TextUnit, String) { + let cursor = "<|>"; + let cursor_pos = match text.find(cursor) { + None => panic!("text should contain cursor marker"), + Some(pos) => pos, + }; + let mut new_text = String::with_capacity(text.len() - cursor.len()); + new_text.push_str(&text[..cursor_pos]); + new_text.push_str(&text[cursor_pos + cursor.len()..]); + let cursor_pos = TextUnit::from(cursor_pos as u32); + (cursor_pos, new_text) +} + +pub fn extract_range(text: &str) -> (TextRange, String) { + let (start, text) = extract_offset(text); + let (end, text) = extract_offset(&text); + (TextRange::from_to(start, end), text) +} + +pub fn add_cursor(text: &str, offset: TextUnit) -> String { + let offset: u32 = offset.into(); + let offset: usize = offset as usize; + let mut res = String::new(); + res.push_str(&text[..offset]); + res.push_str("<|>"); + res.push_str(&text[offset..]); + res +} -- cgit v1.2.3