From b1d5817dd18b7b5fc102a63b084b1ee7ff4f9996 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 24 Apr 2020 23:40:41 +0200 Subject: Convert code to text-size --- crates/ra_text_edit/Cargo.toml | 3 +-- crates/ra_text_edit/src/lib.rs | 10 +++++----- crates/ra_text_edit/src/text_edit.rs | 25 +++++++++++++------------ 3 files changed, 19 insertions(+), 19 deletions(-) (limited to 'crates/ra_text_edit') diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml index cae28389d..9b0567c98 100644 --- a/crates/ra_text_edit/Cargo.toml +++ b/crates/ra_text_edit/Cargo.toml @@ -9,5 +9,4 @@ publish = false doctest = false [dependencies] -text_unit = "0.1.10" - +text-size = { path = "../../../text-size" } diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index f6769e6a6..e656260c7 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -2,7 +2,7 @@ mod text_edit; -use text_unit::{TextRange, TextUnit}; +use text_size::{TextRange, TextSize}; pub use crate::text_edit::{TextEdit, TextEditBuilder}; @@ -23,13 +23,13 @@ impl AtomTextEdit { AtomTextEdit::replace(range, String::new()) } - pub fn insert(offset: TextUnit, text: String) -> AtomTextEdit { - AtomTextEdit::replace(TextRange::offset_len(offset, 0.into()), text) + pub fn insert(offset: TextSize, text: String) -> AtomTextEdit { + AtomTextEdit::replace(TextRange::empty(offset), text) } pub fn apply(&self, mut text: String) -> String { - let start = self.delete.start().to_usize(); - let end = self.delete.end().to_usize(); + let start: usize = self.delete.start().into(); + let end: usize = self.delete.end().into(); text.replace_range(start..end, &self.insert); text } diff --git a/crates/ra_text_edit/src/text_edit.rs b/crates/ra_text_edit/src/text_edit.rs index 5c37a08a8..db69a7e7b 100644 --- a/crates/ra_text_edit/src/text_edit.rs +++ b/crates/ra_text_edit/src/text_edit.rs @@ -1,7 +1,8 @@ //! FIXME: write short doc here use crate::AtomTextEdit; -use text_unit::{TextRange, TextUnit}; +// TODO: fix Cargo.toml +use text_size::{TextRange, TextSize}; #[derive(Debug, Clone)] pub struct TextEdit { @@ -20,19 +21,19 @@ impl TextEditBuilder { pub fn delete(&mut self, range: TextRange) { self.atoms.push(AtomTextEdit::delete(range)) } - pub fn insert(&mut self, offset: TextUnit, text: String) { + pub fn insert(&mut self, offset: TextSize, text: String) { self.atoms.push(AtomTextEdit::insert(offset, text)) } pub fn finish(self) -> TextEdit { TextEdit::from_atoms(self.atoms) } - pub fn invalidates_offset(&self, offset: TextUnit) -> bool { + pub fn invalidates_offset(&self, offset: TextSize) -> bool { self.atoms.iter().any(|atom| atom.delete.contains_inclusive(offset)) } } impl TextEdit { - pub fn insert(offset: TextUnit, text: String) -> TextEdit { + pub fn insert(offset: TextSize, text: String) -> TextEdit { let mut builder = TextEditBuilder::default(); builder.insert(offset, text); builder.finish() @@ -63,16 +64,16 @@ impl TextEdit { } pub fn apply(&self, text: &str) -> String { - let mut total_len = TextUnit::of_str(text); + let mut total_len = TextSize::of(text); for atom in self.atoms.iter() { - total_len += TextUnit::of_str(&atom.insert); + total_len += TextSize::of(&atom.insert); total_len -= atom.delete.end() - atom.delete.start(); } - let mut buf = String::with_capacity(total_len.to_usize()); + let mut buf = String::with_capacity(total_len.into()); let mut prev = 0; for atom in self.atoms.iter() { - let start = atom.delete.start().to_usize(); - let end = atom.delete.end().to_usize(); + let start: usize = atom.delete.start().into(); + let end: usize = atom.delete.end().into(); if start > prev { buf.push_str(&text[prev..start]); } @@ -80,11 +81,11 @@ impl TextEdit { prev = end; } buf.push_str(&text[prev..text.len()]); - assert_eq!(TextUnit::of_str(&buf), total_len); + assert_eq!(TextSize::of(&buf), total_len); buf } - pub fn apply_to_offset(&self, offset: TextUnit) -> Option { + pub fn apply_to_offset(&self, offset: TextSize) -> Option { let mut res = offset; for atom in self.atoms.iter() { if atom.delete.start() >= offset { @@ -93,7 +94,7 @@ impl TextEdit { if offset < atom.delete.end() { return None; } - res += TextUnit::of_str(&atom.insert); + res += TextSize::of(&atom.insert); res -= atom.delete.len(); } Some(res) -- cgit v1.2.3