Convert code to text-size

This commit is contained in:
Aleksey Kladov 2020-04-24 23:40:41 +02:00
parent 27a7718880
commit b1d5817dd1
75 changed files with 438 additions and 456 deletions

View file

@ -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<TextUnit> {
pub fn apply_to_offset(&self, offset: TextSize) -> Option<TextSize> {
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)