Fix fold tests with new representation

Switch UI code from using display rows to using buffer rows
Make folds only show up on lines with line layouts

co-authored-by: Max <max@zed.dev>
This commit is contained in:
Mikayla Maki 2023-03-17 16:00:22 -07:00
parent c427a8c584
commit eba119b914
4 changed files with 93 additions and 117 deletions

View file

@ -40,7 +40,6 @@ use gpui::{
keymap_matcher::KeymapContext,
platform::CursorStyle,
serde_json::json,
text_layout::Line,
AnyViewHandle, AppContext, AsyncAppContext, ClipboardItem, Element, ElementBox, Entity,
ModelHandle, MouseButton, MutableAppContext, RenderContext, Subscription, Task, View,
ViewContext, ViewHandle, WeakViewHandle,
@ -164,12 +163,12 @@ pub struct ToggleComments {
#[derive(Clone, Default, Deserialize, PartialEq)]
pub struct FoldAt {
pub display_row: u32,
pub buffer_row: u32,
}
#[derive(Clone, Default, Deserialize, PartialEq)]
pub struct UnfoldAt {
pub display_row: u32,
pub buffer_row: u32,
}
#[derive(Clone, Default, Deserialize, PartialEq)]
@ -2706,34 +2705,26 @@ impl Editor {
pub fn render_fold_indicators(
&self,
fold_data: Option<Vec<(u32, FoldStatus)>>,
active_rows: &BTreeMap<u32, bool>,
line_layouts: &Vec<Option<Line>>,
fold_data: Vec<Option<(FoldStatus, u32, bool)>>,
style: &EditorStyle,
gutter_hovered: bool,
line_height: f32,
gutter_margin: f32,
cx: &mut RenderContext<Self>,
) -> Option<Vec<(u32, ElementBox)>> {
) -> Vec<Option<ElementBox>> {
enum FoldIndicators {}
let style = style.folds.clone();
fold_data.map(|fold_data| {
fold_data
.iter()
.copied()
.filter_map(|(fold_location, fold_status)| {
let has_line_number = line_layouts[fold_location as usize].is_some();
(has_line_number
&& (gutter_hovered
|| fold_status == FoldStatus::Folded
|| !*active_rows.get(&fold_location).unwrap_or(&true)))
.then(|| {
(
fold_location,
fold_data
.iter()
.enumerate()
.map(|(ix, fold_data)| {
fold_data
.map(|(fold_status, buffer_row, active)| {
(active || gutter_hovered || fold_status == FoldStatus::Folded).then(|| {
MouseEventHandler::<FoldIndicators>::new(
fold_location as usize,
ix as usize,
cx,
|mouse_state, _| -> ElementBox {
Svg::new(match fold_status {
@ -2764,21 +2755,17 @@ impl Editor {
.on_click(MouseButton::Left, {
move |_, cx| {
cx.dispatch_any_action(match fold_status {
FoldStatus::Folded => Box::new(UnfoldAt {
display_row: fold_location,
}),
FoldStatus::Foldable => Box::new(FoldAt {
display_row: fold_location,
}),
FoldStatus::Folded => Box::new(UnfoldAt { buffer_row }),
FoldStatus::Foldable => Box::new(FoldAt { buffer_row }),
});
}
})
.boxed(),
)
.boxed()
})
})
})
.collect()
})
.flatten()
})
.collect()
}
pub fn context_menu_visible(&self) -> bool {
@ -5762,24 +5749,14 @@ impl Editor {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let selections = self.selections.all::<Point>(cx);
dbg!(&selections);
for selection in selections {
let range = selection.range().sorted();
let buffer_start_row = range.start.row;
for row in (0..=range.end.row).rev() {
dbg!(row);
let fold_range = dbg!(display_map.foldable_range(row));
let fold_range = display_map.foldable_range(row);
if let Some(fold_range) = fold_range {
let display_point = fold_range.end.to_display_point(&display_map);
let line = display_map
.chars_at(DisplayPoint::new(display_point.row(), 0))
.map(|(ccharr, _)| ccharr)
.take_while(|charr| charr != &'\n')
.collect::<String>();
dbg!(line);
if fold_range.end.row >= buffer_start_row {
fold_ranges.push(fold_range);
if row <= range.start.row {
@ -5794,11 +5771,11 @@ impl Editor {
}
pub fn fold_at(&mut self, fold_at: &FoldAt, cx: &mut ViewContext<Self>) {
let display_row = fold_at.display_row;
let buffer_row = fold_at.buffer_row;
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
if let Some(fold_range) = display_map.foldable_range(display_row) {
if let Some(fold_range) = display_map.foldable_range(buffer_row) {
let autoscroll = self
.selections
.all::<Point>(cx)
@ -5831,25 +5808,19 @@ impl Editor {
pub fn unfold_at(&mut self, unfold_at: &UnfoldAt, cx: &mut ViewContext<Self>) {
let display_map = self.display_map.update(cx, |map, cx| map.snapshot(cx));
let intersection_range = DisplayPoint::new(unfold_at.display_row, 0)
..DisplayPoint::new(
unfold_at.display_row,
display_map.line_len(unfold_at.display_row),
let intersection_range = Point::new(unfold_at.buffer_row, 0)
..Point::new(
unfold_at.buffer_row,
display_map.buffer_snapshot.line_len(unfold_at.buffer_row),
);
let autoscroll =
self.selections.all::<Point>(cx).iter().any(|selection| {
intersection_range.overlaps(&selection.display_range(&display_map))
});
let autoscroll = self
.selections
.all::<Point>(cx)
.iter()
.any(|selection| selection.range().overlaps(&intersection_range));
let display_point = DisplayPoint::new(unfold_at.display_row, 0).to_point(&display_map);
let mut point_range = display_point..display_point;
point_range.start.column = 0;
point_range.end.column = display_map.buffer_snapshot.line_len(point_range.end.row);
self.unfold_ranges(std::iter::once(point_range), true, autoscroll, cx)
self.unfold_ranges(std::iter::once(intersection_range), true, autoscroll, cx)
}
pub fn fold_selected_ranges(&mut self, _: &FoldSelectedRanges, cx: &mut ViewContext<Self>) {