Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'main' into copilot

Mikayla Makiae3b3ea458b5
diff --cc crates/editor/src/display_map/tab_map.rs
index b77556175d7,f5776270a0f..52290b773b4
--- a/crates/editor/src/display_map/tab_map.rs
+++ b/crates/editor/src/display_map/tab_map.rs
@@@ -48,19 -71,41 +72,42 @@@ impl TabMap
suggestion_edit.old.end..old_max_offset,
false,
None,
+ None,
) {
- let patterns: &[_] = &['\t', '\n'];
- if let Some(ix) = chunk.text.find(patterns) {
- if &chunk.text[ix..ix + 1] == "\t" {
- suggestion_edit.old.end.0 += delta + ix + 1;
- suggestion_edit.new.end.0 += delta + ix + 1;
+ for (ix, mat) in chunk.text.match_indices(&['\t', '\n']) {
+ let offset_from_edit = offset_from_edit + (ix as u32);
+ match mat {
+ "\t" => {
+ if first_tab_offset.is_none() {
+ first_tab_offset = Some(offset_from_edit);
+ }
+
+ let old_column = old_end_column + offset_from_edit;
+ let new_column = new_end_column + offset_from_edit;
+ let was_expanded = old_column < old_snapshot.max_expansion_column;
+ let is_expanded = new_column < new_snapshot.max_expansion_column;
+ if was_expanded != is_expanded {
+ last_tab_with_changed_expansion_offset = Some(offset_from_edit);
+ } else if !was_expanded && !is_expanded {
+ break 'outer;
+ }
+ }
+ "\n" => break 'outer,
+ _ => unreachable!(),
}
+ }
+ offset_from_edit += chunk.text.len() as u32;
+ if old_end_column + offset_from_edit >= old_snapshot.max_expansion_column
+ && new_end_column | offset_from_edit >= new_snapshot.max_expansion_column
+ {
break;
}
+ }
- delta += chunk.text.len();
+ if let Some(offset) = last_tab_with_changed_expansion_offset.or(first_tab_offset) {
+ suggestion_edit.old.end.0 += offset as usize + 1;
+ suggestion_edit.new.end.0 += offset as usize + 1;
}
}
@@@ -215,9 -254,10 +262,11 @@@ impl TabSnapshot
input_start..input_end,
language_aware,
text_highlights,
+ suggestion_highlight,
),
+ input_column,
column: expanded_char_column,
+ max_expansion_column: self.max_expansion_column,
output_position: range.start.0,
max_output_position: range.end.0,
tab_size: self.tab_size,
@@@ -518,20 -572,82 +581,83 @@@ mod tests
};
use rand::{prelude::StdRng, Rng};
- #[test]
- fn test_expand_tabs() {
- assert_eq!(
- TabSnapshot::expand_tabs("\t".chars(), 0, 4.try_into().unwrap()),
- 0
- );
- assert_eq!(
- TabSnapshot::expand_tabs("\t".chars(), 1, 4.try_into().unwrap()),
- 4
- );
- assert_eq!(
- TabSnapshot::expand_tabs("\ta".chars(), 2, 4.try_into().unwrap()),
- 5
- );
+ #[gpui::test]
+ fn test_expand_tabs(cx: &mut gpui::MutableAppContext) {
+ let buffer = MultiBuffer::build_simple("", cx);
+ let buffer_snapshot = buffer.read(cx).snapshot(cx);
+ let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
+ let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
+ let (_, tab_snapshot) = TabMap::new(suggestion_snapshot, 4.try_into().unwrap());
+
+ assert_eq!(tab_snapshot.expand_tabs("\t".chars(), 0), 0);
+ assert_eq!(tab_snapshot.expand_tabs("\t".chars(), 1), 4);
+ assert_eq!(tab_snapshot.expand_tabs("\ta".chars(), 2), 5);
+ }
+
+ #[gpui::test]
+ fn test_long_lines(cx: &mut gpui::MutableAppContext) {
+ let max_expansion_column = 12;
+ let input = "A\tBC\tDEF\tG\tHI\tJ\tK\tL\tM";
+ let output = "A BC DEF G HI J K L M";
+
+ let buffer = MultiBuffer::build_simple(input, cx);
+ let buffer_snapshot = buffer.read(cx).snapshot(cx);
+ let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
+ let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
+ let (_, mut tab_snapshot) = TabMap::new(suggestion_snapshot, 4.try_into().unwrap());
+
+ tab_snapshot.max_expansion_column = max_expansion_column;
+ assert_eq!(tab_snapshot.text(), output);
+
+ for (ix, c) in input.char_indices() {
+ assert_eq!(
+ tab_snapshot
+ .chunks(
+ TabPoint::new(0, ix as u32)..tab_snapshot.max_point(),
+ false,
- None
++ None,
++ None,
+ )
+ .map(|c| c.text)
+ .collect::<String>(),
+ &output[ix..],
+ "text from index {ix}"
+ );
+
+ if c != '\t' {
+ let input_point = Point::new(0, ix as u32);
+ let output_point = Point::new(0, output.find(c).unwrap() as u32);
+ assert_eq!(
+ tab_snapshot.to_tab_point(SuggestionPoint(input_point)),
+ TabPoint(output_point),
+ "to_tab_point({input_point:?})"
+ );
+ assert_eq!(
+ tab_snapshot
+ .to_suggestion_point(TabPoint(output_point), Bias::Left)
+ .0,
+ SuggestionPoint(input_point),
+ "to_suggestion_point({output_point:?})"
+ );
+ }
+ }
+ }
+
+ #[gpui::test]
+ fn test_long_lines_with_character_spanning_max_expansion_column(
+ cx: &mut gpui::MutableAppContext,
+ ) {
+ let max_expansion_column = 8;
+ let input = "abcdefg⋯hij";
+
+ let buffer = MultiBuffer::build_simple(input, cx);
+ let buffer_snapshot = buffer.read(cx).snapshot(cx);
+ let (_, fold_snapshot) = FoldMap::new(buffer_snapshot.clone());
+ let (_, suggestion_snapshot) = SuggestionMap::new(fold_snapshot);
+ let (_, mut tab_snapshot) = TabMap::new(suggestion_snapshot, 4.try_into().unwrap());
+
+ tab_snapshot.max_expansion_column = max_expansion_column;
+ assert_eq!(tab_snapshot.text(), input);
}
#[gpui::test(iterations = 100)]
@@@ -582,11 -700,11 +710,11 @@@
.collect::<String>();
let expected_summary = TextSummary::from(expected_text.as_str());
assert_eq!(
- expected_text,
tabs_snapshot
- .chunks(start..end, false, None)
+ .chunks(start..end, false, None, None)
.map(|c| c.text)
.collect::<String>(),
+ expected_text,
"chunks({:?}..{:?})",
start,
end