From 169a298af1f5e55f33963743027e6c543ce2e89b Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 28 Jul 2021 13:29:56 +0200 Subject: [PATCH] Expand edits correctly when there are folds or multi-byte chars Previously, we were mistakenly adding `tab_size` to the extent produced by the fold edits but that could cause the edit to land on a multi-byte character (like a fold or an emoji). In practice, we only need to expand the edit's extent by 1 because we are operating in the fold coordinate space and all we need to convey is that we want to encapsulate the first tab next to whatever edit has just occurred in the `FoldMap`. --- zed/src/editor/display_map/tab_map.rs | 10 +++------- zed/src/editor/display_map/wrap_map.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/zed/src/editor/display_map/tab_map.rs b/zed/src/editor/display_map/tab_map.rs index 75049e40ca2dbbe0ad8adfa29d5f5ceb464d8b71..fb11e08024f2eae31d0afd47b1ea5a3bf79bf038 100644 --- a/zed/src/editor/display_map/tab_map.rs +++ b/zed/src/editor/display_map/tab_map.rs @@ -2,7 +2,7 @@ use parking_lot::Mutex; use super::fold_map::{self, FoldEdit, FoldPoint, Snapshot as FoldSnapshot}; use crate::{editor::rope, settings::StyleId, util::Bias}; -use std::{cmp, mem, ops::Range}; +use std::{mem, ops::Range}; pub struct TabMap(Mutex); @@ -36,8 +36,8 @@ impl TabMap { let patterns: &[_] = &['\t', '\n']; if let Some(ix) = chunk.find(patterns) { if &chunk[ix..ix + 1] == "\t" { - fold_edit.old_bytes.end.0 += delta + ix + old_snapshot.tab_size; - fold_edit.new_bytes.end.0 += delta + ix + new_snapshot.tab_size; + fold_edit.old_bytes.end.0 += delta + ix + 1; + fold_edit.new_bytes.end.0 += delta + ix + 1; } break; @@ -45,10 +45,6 @@ impl TabMap { delta += chunk.len(); } - fold_edit.old_bytes.end = - cmp::min(fold_edit.old_bytes.end, old_snapshot.fold_snapshot.len()); - fold_edit.new_bytes.end = - cmp::min(fold_edit.new_bytes.end, new_snapshot.fold_snapshot.len()); } let mut ix = 1; diff --git a/zed/src/editor/display_map/wrap_map.rs b/zed/src/editor/display_map/wrap_map.rs index 68a649ff0dae1599f9738bce562e3108d1011c01..d2f38971a81b3497ee418896932c9111617c496e 100644 --- a/zed/src/editor/display_map/wrap_map.rs +++ b/zed/src/editor/display_map/wrap_map.rs @@ -902,6 +902,10 @@ mod tests { }); let (mut fold_map, folds_snapshot) = cx.read(|cx| FoldMap::new(buffer.clone(), cx)); let (tab_map, tabs_snapshot) = TabMap::new(folds_snapshot.clone(), settings.tab_size); + log::info!( + "Unwrapped text (no folds): {:?}", + buffer.read_with(&cx, |buf, _| buf.text()) + ); log::info!( "Unwrapped text (unexpanded tabs): {:?}", folds_snapshot.text() @@ -957,6 +961,10 @@ mod tests { } } + log::info!( + "Unwrapped text (no folds): {:?}", + buffer.read_with(&cx, |buf, _| buf.text()) + ); let (folds_snapshot, edits) = cx.read(|cx| fold_map.read(cx)); log::info!( "Unwrapped text (unexpanded tabs): {:?}",