Strip carriage returns from all text in text::Buffer

* Moving the logic from Rope to text::Buffer makes it easier
  to keep the Rope in sync with the fragment tree.
* Removing carriage return characters is lossier, but is much
  simpler than incrementally maintaining the invariant that
  there are no carriage returns followed by newlines. We may
  want to do something smarter in the future.

Co-authored-by: Keith Simmons <keith@zed.dev>
This commit is contained in:
Max Brunsfeld 2022-07-05 17:14:12 -07:00
parent 116fa92e84
commit 7e9beaf4bb
9 changed files with 132 additions and 100 deletions

View file

@ -22,7 +22,7 @@ impl<T: Rng> Iterator for RandomCharIter<T> {
match self.0.gen_range(0..100) {
// whitespace
0..=19 => [' ', '\n', '\t'].choose(&mut self.0).copied(),
0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.0).copied(),
// two-byte greek letters
20..=32 => char::from_u32(self.0.gen_range(('α' as u32)..('ω' as u32 + 1))),
// // three-byte characters

View file

@ -58,19 +58,12 @@ impl Rope {
pub fn push(&mut self, text: &str) {
let mut new_chunks = SmallVec::<[_; 16]>::new();
let mut new_chunk = ArrayString::new();
let mut chars = text.chars().peekable();
while let Some(mut ch) = chars.next() {
for ch in text.chars() {
if new_chunk.len() + ch.len_utf8() > 2 * CHUNK_BASE {
new_chunks.push(Chunk(new_chunk));
new_chunk = ArrayString::new();
}
if ch == '\r' {
ch = '\n';
if chars.peek().copied() == Some('\n') {
chars.next();
}
}
new_chunk.push(ch);
}
if !new_chunk.is_empty() {

View file

@ -43,6 +43,8 @@ fn test_random_edits(mut rng: StdRng) {
.take(reference_string_len)
.collect::<String>();
let mut buffer = Buffer::new(0, 0, reference_string.clone().into());
reference_string = reference_string.replace("\r", "");
buffer.history.group_interval = Duration::from_millis(rng.gen_range(0..=200));
let mut buffer_versions = Vec::new();
log::info!(
@ -56,6 +58,8 @@ fn test_random_edits(mut rng: StdRng) {
for (old_range, new_text) in edits.iter().rev() {
reference_string.replace_range(old_range.clone(), &new_text);
}
reference_string = reference_string.replace("\r", "");
assert_eq!(buffer.text(), reference_string);
log::info!(
"buffer text {:?}, version: {:?}",
@ -148,6 +152,20 @@ fn test_random_edits(mut rng: StdRng) {
}
}
#[test]
fn test_line_endings() {
let mut buffer = Buffer::new(0, 0, "one\r\ntwo".into());
assert_eq!(buffer.text(), "one\ntwo");
assert_eq!(buffer.line_ending(), LineEnding::Windows);
buffer.check_invariants();
buffer.edit([(buffer.len()..buffer.len(), "\r\nthree")]);
buffer.edit([(0..0, "zero\r\n")]);
assert_eq!(buffer.text(), "zero\none\ntwo\nthree");
assert_eq!(buffer.line_ending(), LineEnding::Windows);
buffer.check_invariants();
}
#[test]
fn test_line_len() {
let mut buffer = Buffer::new(0, 0, "".into());

View file

@ -63,6 +63,7 @@ pub struct BufferSnapshot {
remote_id: u64,
visible_text: Rope,
deleted_text: Rope,
line_ending: LineEnding,
undo_map: UndoMap,
fragments: SumTree<Fragment>,
insertions: SumTree<InsertionFragment>,
@ -86,6 +87,12 @@ pub struct Transaction {
pub ranges: Vec<Range<FullOffset>>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LineEnding {
Unix,
Windows,
}
impl HistoryEntry {
pub fn transaction_id(&self) -> TransactionId {
self.transaction.id
@ -539,7 +546,10 @@ pub struct UndoOperation {
}
impl Buffer {
pub fn new(replica_id: u16, remote_id: u64, base_text: String) -> Buffer {
pub fn new(replica_id: u16, remote_id: u64, mut base_text: String) -> Buffer {
let line_ending = LineEnding::detect(&base_text);
LineEnding::strip_carriage_returns(&mut base_text);
let history = History::new(base_text.into());
let mut fragments = SumTree::new();
let mut insertions = SumTree::new();
@ -547,6 +557,7 @@ impl Buffer {
let mut local_clock = clock::Local::new(replica_id);
let mut lamport_clock = clock::Lamport::new(replica_id);
let mut version = clock::Global::new();
let visible_text = Rope::from(history.base_text.as_ref());
if visible_text.len() > 0 {
let insertion_timestamp = InsertionTimestamp {
@ -577,6 +588,7 @@ impl Buffer {
remote_id,
visible_text,
deleted_text: Rope::new(),
line_ending,
fragments,
insertions,
version,
@ -659,7 +671,7 @@ impl Buffer {
let mut new_insertions = Vec::new();
let mut insertion_offset = 0;
let mut ranges = edits
let mut edits = edits
.map(|(range, new_text)| (range.to_offset(&*self), new_text))
.peekable();
@ -667,12 +679,12 @@ impl Buffer {
RopeBuilder::new(self.visible_text.cursor(0), self.deleted_text.cursor(0));
let mut old_fragments = self.fragments.cursor::<FragmentTextSummary>();
let mut new_fragments =
old_fragments.slice(&ranges.peek().unwrap().0.start, Bias::Right, &None);
old_fragments.slice(&edits.peek().unwrap().0.start, Bias::Right, &None);
new_ropes.push_tree(new_fragments.summary().text);
let mut fragment_start = old_fragments.start().visible;
for (range, new_text) in ranges {
let new_text = new_text.into();
for (range, new_text) in edits {
let new_text = LineEnding::strip_carriage_returns_from_arc(new_text.into());
let fragment_end = old_fragments.end(&None).visible;
// If the current fragment ends before this range, then jump ahead to the first fragment
@ -715,6 +727,7 @@ impl Buffer {
// Insert the new text before any existing fragments within the range.
if !new_text.is_empty() {
let new_start = new_fragments.summary().text.visible;
edits_patch.push(Edit {
old: fragment_start..fragment_start,
new: new_start..new_start + new_text.len(),
@ -806,6 +819,10 @@ impl Buffer {
edit_op
}
pub fn set_line_ending(&mut self, line_ending: LineEnding) {
self.snapshot.line_ending = line_ending;
}
pub fn apply_ops<I: IntoIterator<Item = Operation>>(&mut self, ops: I) -> Result<()> {
let mut deferred_ops = Vec::new();
for op in ops {
@ -1413,6 +1430,8 @@ impl Buffer {
fragment_summary.text.deleted,
self.snapshot.deleted_text.len()
);
assert!(!self.text().contains("\r\n"));
}
pub fn set_group_interval(&mut self, group_interval: Duration) {
@ -1550,6 +1569,10 @@ impl BufferSnapshot {
self.visible_text.to_string()
}
pub fn line_ending(&self) -> LineEnding {
self.line_ending
}
pub fn deleted_text(&self) -> String {
self.deleted_text.to_string()
}
@ -2311,6 +2334,50 @@ impl operation_queue::Operation for Operation {
}
}
impl Default for LineEnding {
fn default() -> Self {
#[cfg(unix)]
return Self::Unix;
#[cfg(not(unix))]
return Self::CRLF;
}
}
impl LineEnding {
pub fn as_str(&self) -> &'static str {
match self {
LineEnding::Unix => "\n",
LineEnding::Windows => "\r\n",
}
}
pub fn detect(text: &str) -> Self {
if let Some(ix) = text[..cmp::min(text.len(), 1000)].find(&['\n']) {
let text = text.as_bytes();
if ix > 0 && text[ix - 1] == b'\r' {
Self::Windows
} else {
Self::Unix
}
} else {
Self::default()
}
}
pub fn strip_carriage_returns(text: &mut String) {
text.retain(|c| c != '\r')
}
fn strip_carriage_returns_from_arc(text: Arc<str>) -> Arc<str> {
if text.contains('\r') {
text.replace('\r', "").into()
} else {
text
}
}
}
pub trait ToOffset {
fn to_offset<'a>(&self, snapshot: &BufferSnapshot) -> usize;
}