2019-09-30 11:58:53 +03:00
|
|
|
//! FIXME: write short doc here
|
|
|
|
|
|
2018-12-11 19:07:17 +01:00
|
|
|
mod text_edit;
|
2018-12-10 22:09:12 +01:00
|
|
|
|
2020-04-24 23:40:41 +02:00
|
|
|
use text_size::{TextRange, TextSize};
|
2018-12-10 22:09:12 +01:00
|
|
|
|
2020-02-17 16:57:06 +01:00
|
|
|
pub use crate::text_edit::{TextEdit, TextEditBuilder};
|
|
|
|
|
|
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 {
|
2019-02-08 14:49:43 +03:00
|
|
|
AtomTextEdit { delete: range, insert: replace_with }
|
2018-12-10 22:09:12 +01:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2020-04-24 23:40:41 +02:00
|
|
|
pub fn insert(offset: TextSize, text: String) -> AtomTextEdit {
|
|
|
|
|
AtomTextEdit::replace(TextRange::empty(offset), 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 {
|
2020-04-24 23:40:41 +02:00
|
|
|
let start: usize = self.delete.start().into();
|
|
|
|
|
let end: usize = self.delete.end().into();
|
2019-01-08 21:59:55 +03:00
|
|
|
text.replace_range(start..end, &self.insert);
|
|
|
|
|
text
|
|
|
|
|
}
|
2018-12-10 22:09:12 +01:00
|
|
|
}
|