From 7510048ec0a5d5e7136e3ea258954eb244d15baf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 16:58:56 +0200 Subject: Cleanup TextEdit API --- crates/ra_text_edit/src/lib.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'crates/ra_text_edit/src') diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index d68791cf1..ab8cd7fd1 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -3,8 +3,6 @@ //! `rust-analyzer` never mutates text itself and only sends diffs to clients, //! so `TextEdit` is the ultimate representation of the work done by //! rust-analyzer. -use std::{slice, vec}; - pub use text_size::{TextRange, TextSize}; /// `InsertDelete` -- a single "atomic" change to text @@ -46,20 +44,24 @@ impl Indel { } impl TextEdit { + pub fn builder() -> TextEditBuilder { + TextEditBuilder::default() + } + pub fn insert(offset: TextSize, text: String) -> TextEdit { - let mut builder = TextEditBuilder::default(); + let mut builder = TextEdit::builder(); builder.insert(offset, text); builder.finish() } pub fn delete(range: TextRange) -> TextEdit { - let mut builder = TextEditBuilder::default(); + let mut builder = TextEdit::builder(); builder.delete(range); builder.finish() } pub fn replace(range: TextRange, replace_with: String) -> TextEdit { - let mut builder = TextEditBuilder::default(); + let mut builder = TextEdit::builder(); builder.replace(range, replace_with); builder.finish() } @@ -72,8 +74,8 @@ impl TextEdit { self.indels.is_empty() } - pub fn iter(&self) -> slice::Iter<'_, Indel> { - self.indels.iter() + pub fn iter(&self) -> std::slice::Iter<'_, Indel> { + self.into_iter() } pub fn apply(&self, text: &mut String) { @@ -139,13 +141,22 @@ impl TextEdit { impl IntoIterator for TextEdit { type Item = Indel; - type IntoIter = vec::IntoIter; + type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.indels.into_iter() } } +impl<'a> IntoIterator for &'a TextEdit { + type Item = &'a Indel; + type IntoIter = std::slice::Iter<'a, Indel>; + + fn into_iter(self) -> Self::IntoIter { + self.indels.iter() + } +} + impl TextEditBuilder { pub fn replace(&mut self, range: TextRange, replace_with: String) { self.indels.push(Indel::replace(range, replace_with)) -- cgit v1.2.3