2018-12-11 19:07:17 +01:00
|
|
|
mod text_edit;
|
2018-12-12 18:51:43 +01:00
|
|
|
pub mod test_utils;
|
2018-12-10 22:09:12 +01:00
|
|
|
|
2018-12-11 19:07:17 +01:00
|
|
|
pub use crate::text_edit::{TextEdit, TextEditBuilder};
|
2018-12-10 22:09:12 +01:00
|
|
|
|
|
|
|
use text_unit::{TextRange, TextUnit};
|
|
|
|
|
2018-12-23 15:49:14 +01:00
|
|
|
/// Must not overlap with other `AtomTextEdit`s
|
2018-12-10 22:09:12 +01:00
|
|
|
#[derive(Debug, Clone)]
|
2018-12-11 19:07:17 +01:00
|
|
|
pub struct AtomTextEdit {
|
2018-12-23 15:49:14 +01:00
|
|
|
/// Refers to offsets in the original text
|
2018-12-10 22:09:12 +01:00
|
|
|
pub delete: TextRange,
|
|
|
|
pub insert: String,
|
|
|
|
}
|
|
|
|
|
2018-12-11 19:07:17 +01:00
|
|
|
impl AtomTextEdit {
|
|
|
|
pub fn replace(range: TextRange, replace_with: String) -> AtomTextEdit {
|
|
|
|
AtomTextEdit {
|
2018-12-10 22:09:12 +01:00
|
|
|
delete: range,
|
|
|
|
insert: replace_with,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 19:07:17 +01:00
|
|
|
pub fn delete(range: TextRange) -> AtomTextEdit {
|
|
|
|
AtomTextEdit::replace(range, String::new())
|
2018-12-10 22:09:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-11 19:07:17 +01:00
|
|
|
pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit {
|
|
|
|
AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text)
|
2018-12-10 22:09:12 +01:00
|
|
|
}
|
2019-01-08 21:59:55 +03:00
|
|
|
|
|
|
|
pub fn apply(&self, mut text: String) -> String {
|
|
|
|
let start = u32::from(self.delete.start()) as usize;
|
|
|
|
let end = u32::from(self.delete.end()) as usize;
|
|
|
|
text.replace_range(start..end, &self.insert);
|
|
|
|
text
|
|
|
|
}
|
2018-12-10 22:09:12 +01:00
|
|
|
}
|