Compare commits

...

8 Commits

Author SHA1 Message Date
Richard Feldman
87057cce46 remove crease 2024-10-31 02:08:52 -04:00
Richard Feldman
c76d5526a6 Remove snapshot.intersects_fold logic 2024-10-31 02:08:52 -04:00
Richard Feldman
933595c0cf Change BlockStyle 2024-10-31 02:08:52 -04:00
Richard Feldman
80cff6ec51 Change styling 2024-10-31 02:02:25 -04:00
Richard Feldman
1e4ec0b9a9 switch to Replace 2024-10-31 02:02:25 -04:00
Richard Feldman
2cf7eee271 rename patch_footer 2024-10-31 01:39:17 -04:00
Antonio Scandurra
8854ecd797 Introduce a new selected field to BlockContext 2024-10-30 17:38:50 +01:00
Antonio Scandurra
26ad0f78d7 Start on expanding selection when it lands inside of a fold or block
Co-Authored-By: Richard <richard@zed.dev>
2024-10-30 16:45:09 +01:00
8 changed files with 166 additions and 106 deletions

View File

@@ -1447,8 +1447,8 @@ struct ScrollPosition {
}
struct PatchViewState {
footer_block_id: CustomBlockId,
crease_id: CreaseId,
block_id: CustomBlockId,
// crease_id: CreaseId,
editor: Option<PatchEditorState>,
update_task: Option<Task<()>>,
}
@@ -2170,8 +2170,8 @@ impl ContextEditor {
for range in removed {
if let Some(state) = self.patches.remove(range) {
editors_to_close.extend(state.editor.and_then(|state| state.editor.upgrade()));
removed_block_ids.insert(state.footer_block_id);
removed_crease_ids.push(state.crease_id);
removed_block_ids.insert(state.block_id);
// removed_crease_ids.push(state.crease_id);
}
}
@@ -2201,7 +2201,7 @@ impl ContextEditor {
let gutter_width = cx.gutter_dimensions.full_width();
let block_id = cx.block_id;
this.update(&mut **cx, |this, cx| {
this.render_patch_footer(
this.render_patch(
patch_range.clone(),
max_width,
gutter_width,
@@ -2215,26 +2215,9 @@ impl ContextEditor {
}
});
let header_placeholder = FoldPlaceholder {
render: {
let this = this.clone();
let patch_range = range.clone();
Arc::new(move |fold_id, _range, cx| {
this.update(cx, |this, cx| {
this.render_patch_header(patch_range.clone(), fold_id, cx)
})
.ok()
.flatten()
.unwrap_or_else(|| Empty.into_any())
})
},
constrain_width: false,
merge_adjacent: false,
};
let should_refold;
if let Some(state) = self.patches.get_mut(&range) {
replaced_blocks.insert(state.footer_block_id, render_block);
replaced_blocks.insert(state.block_id, render_block);
if let Some(editor_state) = &state.editor {
if editor_state.opened_patch != patch {
state.update_task = Some({
@@ -2254,30 +2237,30 @@ impl ContextEditor {
let block_ids = editor.insert_blocks(
[BlockProperties {
height: path_count as u32 + 1,
style: BlockStyle::Flex,
style: BlockStyle::Fixed,
render: render_block,
placement: BlockPlacement::Below(patch_start),
placement: BlockPlacement::Replace(patch_start..patch_end),
priority: 0,
}],
None,
cx,
);
let new_crease_ids = editor.insert_creases(
[Crease::new(
patch_start..patch_end,
header_placeholder.clone(),
fold_toggle("patch-header"),
|_, _, _| Empty.into_any_element(),
)],
cx,
);
// let new_crease_ids = editor.insert_creases(
// [Crease::new(
// patch_start..patch_end,
// header_placeholder.clone(),
// fold_toggle("patch-header"),
// |_, _, _| Empty.into_any_element(),
// )],
// cx,
// );
self.patches.insert(
range.clone(),
PatchViewState {
footer_block_id: block_ids[0],
crease_id: new_crease_ids[0],
block_id: block_ids[0],
// crease_id: new_crease_ids[0],
editor: None,
update_task: None,
},
@@ -2287,8 +2270,8 @@ impl ContextEditor {
}
if should_refold {
editor.unfold_ranges([patch_start..patch_end], true, false, cx);
editor.fold_ranges([(patch_start..patch_end, header_placeholder)], false, cx);
// editor.unfold_ranges([patch_start..patch_end], true, false, cx);
// editor.fold_ranges([(patch_start..patch_end, header_placeholder)], false, cx);
}
}
@@ -3446,28 +3429,7 @@ impl ContextEditor {
.unwrap_or_else(|| Cow::Borrowed(DEFAULT_TAB_TITLE))
}
fn render_patch_header(
&self,
range: Range<text::Anchor>,
_id: FoldId,
cx: &mut ViewContext<Self>,
) -> Option<AnyElement> {
let patch = self.context.read(cx).patch_for_range(&range, cx)?;
let theme = cx.theme().clone();
Some(
h_flex()
.px_1()
.py_0p5()
.border_b_1()
.border_color(theme.status().info_border)
.gap_1()
.child(Icon::new(IconName::Diff).size(IconSize::Small))
.child(Label::new(patch.title.clone()).size(LabelSize::Small))
.into_any(),
)
}
fn render_patch_footer(
fn render_patch(
&mut self,
range: Range<text::Anchor>,
max_width: Pixels,
@@ -3483,10 +3445,6 @@ impl ContextEditor {
.anchor_in_excerpt(excerpt_id, range.start)
.unwrap();
if !snapshot.intersects_fold(anchor) {
return None;
}
let patch = self.context.read(cx).patch_for_range(&range, cx)?;
let paths = patch
.paths()
@@ -3495,10 +3453,13 @@ impl ContextEditor {
Some(
v_flex()
.border_1()
.border_color(cx.theme().colors().border)
.id(id)
.pl(gutter_width)
.w(max_width)
.py_2()
.ml(gutter_width)
.p_2()
.rounded_md()
.min_h(cx.line_height() * 3.)
.cursor(CursorStyle::PointingHand)
.on_click(cx.listener(move |this, _, cx| {
this.editor.update(cx, |editor, cx| {
@@ -3508,6 +3469,7 @@ impl ContextEditor {
});
this.focus_active_patch(cx);
}))
.child(Label::new(patch.title.clone()))
.children(paths.into_iter().map(|path| {
h_flex()
.pl_1()

View File

@@ -986,6 +986,7 @@ fn editor_blocks(
em_width: px(0.),
max_width: px(0.),
block_id,
selected: false,
editor_style: &editor::EditorStyle::default(),
});
let element = element.downcast_mut::<Stateful<Div>>().unwrap();

View File

@@ -660,7 +660,7 @@ impl DisplaySnapshot {
new_start..new_end
}
fn point_to_display_point(&self, point: MultiBufferPoint, bias: Bias) -> DisplayPoint {
pub fn point_to_display_point(&self, point: MultiBufferPoint, bias: Bias) -> DisplayPoint {
let inlay_point = self.inlay_snapshot.to_inlay_point(point);
let fold_point = self.fold_snapshot.to_fold_point(inlay_point, bias);
let tab_point = self.tab_snapshot.to_tab_point(fold_point);
@@ -669,7 +669,7 @@ impl DisplaySnapshot {
DisplayPoint(block_point)
}
fn display_point_to_point(&self, point: DisplayPoint, bias: Bias) -> Point {
pub fn display_point_to_point(&self, point: DisplayPoint, bias: Bias) -> Point {
self.inlay_snapshot
.to_buffer_point(self.display_point_to_inlay_point(point, bias))
}

View File

@@ -265,6 +265,7 @@ pub struct BlockContext<'a, 'b> {
pub em_width: Pixels,
pub line_height: Pixels,
pub block_id: BlockId,
pub selected: bool,
pub editor_style: &'b EditorStyle,
}

View File

@@ -3471,8 +3471,8 @@ impl Editor {
}
let new_anchor_selections = new_selections.iter().map(|e| &e.0);
let new_selection_deltas = new_selections.iter().map(|e| e.1);
let snapshot = this.buffer.read(cx).read(cx);
let new_selections = resolve_multiple::<usize, _>(new_anchor_selections, &snapshot)
let map = this.display_map.update(cx, |map, cx| map.snapshot(cx));
let new_selections = resolve_multiple::<usize, _>(new_anchor_selections, &map)
.zip(new_selection_deltas)
.map(|(selection, delta)| Selection {
id: selection.id,
@@ -3485,18 +3485,20 @@ impl Editor {
let mut i = 0;
for (position, delta, selection_id, pair) in new_autoclose_regions {
let position = position.to_offset(&snapshot) + delta;
let start = snapshot.anchor_before(position);
let end = snapshot.anchor_after(position);
let position = position.to_offset(&map.buffer_snapshot) + delta;
let start = map.buffer_snapshot.anchor_before(position);
let end = map.buffer_snapshot.anchor_after(position);
while let Some(existing_state) = this.autoclose_regions.get(i) {
match existing_state.range.start.cmp(&start, &snapshot) {
match existing_state.range.start.cmp(&start, &map.buffer_snapshot) {
Ordering::Less => i += 1,
Ordering::Greater => break,
Ordering::Equal => match end.cmp(&existing_state.range.end, &snapshot) {
Ordering::Less => i += 1,
Ordering::Equal => break,
Ordering::Greater => break,
},
Ordering::Equal => {
match end.cmp(&existing_state.range.end, &map.buffer_snapshot) {
Ordering::Less => i += 1,
Ordering::Equal => break,
Ordering::Greater => break,
}
}
}
}
this.autoclose_regions.insert(
@@ -3509,7 +3511,6 @@ impl Editor {
);
}
drop(snapshot);
let had_active_inline_completion = this.has_active_inline_completion(cx);
this.change_selections_inner(Some(Autoscroll::fit()), false, cx, |s| {
s.select(new_selections)

View File

@@ -2067,6 +2067,7 @@ impl EditorElement {
editor_width: Pixels,
scroll_width: &mut Pixels,
resized_blocks: &mut HashMap<CustomBlockId, u32>,
active_rows: &BTreeMap<DisplayRow, bool>,
cx: &mut WindowContext,
) -> (AnyElement, Size<Pixels>) {
let mut element = match block {
@@ -2093,6 +2094,7 @@ impl EditorElement {
line_height,
em_width,
block_id,
selected: active_rows.contains_key(&block_row_start),
max_width: text_hitbox.size.width.max(*scroll_width),
editor_style: &self.style,
}))
@@ -2430,6 +2432,7 @@ impl EditorElement {
text_x: Pixels,
line_height: Pixels,
line_layouts: &[LineWithInvisibles],
active_rows: &BTreeMap<DisplayRow, bool>,
cx: &mut WindowContext,
) -> Result<Vec<BlockLayout>, HashMap<CustomBlockId, u32>> {
let (fixed_blocks, non_fixed_blocks) = snapshot
@@ -2466,6 +2469,7 @@ impl EditorElement {
editor_width,
scroll_width,
&mut resized_blocks,
active_rows,
cx,
);
fixed_block_max_width = fixed_block_max_width.max(element_size.width + em_width);
@@ -2510,6 +2514,7 @@ impl EditorElement {
editor_width,
scroll_width,
&mut resized_blocks,
active_rows,
cx,
);
@@ -2555,6 +2560,7 @@ impl EditorElement {
editor_width,
scroll_width,
&mut resized_blocks,
active_rows,
cx,
);
@@ -5175,6 +5181,7 @@ impl Element for EditorElement {
gutter_dimensions.full_width(),
line_height,
&line_layouts,
&active_rows,
cx,
)
});

View File

@@ -1,6 +1,6 @@
use std::{
cell::Ref,
iter, mem,
cmp, iter, mem,
ops::{Deref, DerefMut, Range, Sub},
sync::Arc,
};
@@ -111,9 +111,9 @@ impl SelectionsCollection {
where
D: 'a + TextDimension + Ord + Sub<D, Output = D>,
{
let map = self.display_map(cx);
let disjoint_anchors = &self.disjoint;
let mut disjoint =
resolve_multiple::<D, _>(disjoint_anchors.iter(), &self.buffer(cx)).peekable();
let mut disjoint = resolve_multiple::<D, _>(disjoint_anchors.iter(), &map).peekable();
let mut pending_opt = self.pending::<D>(cx);
@@ -199,21 +199,21 @@ impl SelectionsCollection {
where
D: 'a + TextDimension + Ord + Sub<D, Output = D> + std::fmt::Debug,
{
let buffer = self.buffer(cx);
let map = self.display_map(cx);
let start_ix = match self
.disjoint
.binary_search_by(|probe| probe.end.cmp(&range.start, &buffer))
.binary_search_by(|probe| probe.end.cmp(&range.start, &map.buffer_snapshot))
{
Ok(ix) | Err(ix) => ix,
};
let end_ix = match self
.disjoint
.binary_search_by(|probe| probe.start.cmp(&range.end, &buffer))
.binary_search_by(|probe| probe.start.cmp(&range.end, &map.buffer_snapshot))
{
Ok(ix) => ix + 1,
Err(ix) => ix,
};
resolve_multiple(&self.disjoint[start_ix..end_ix], &buffer).collect()
resolve_multiple(&self.disjoint[start_ix..end_ix], &map).collect()
}
pub fn all_display(
@@ -538,9 +538,9 @@ impl<'a> MutableSelectionsCollection<'a> {
}
pub fn select_anchors(&mut self, selections: Vec<Selection<Anchor>>) {
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
let map = self.display_map();
let resolved_selections =
resolve_multiple::<usize, _>(&selections, &buffer).collect::<Vec<_>>();
resolve_multiple::<usize, _>(&selections, &map).collect::<Vec<_>>();
self.select(resolved_selections);
}
@@ -804,8 +804,8 @@ impl<'a> MutableSelectionsCollection<'a> {
.collect();
if !adjusted_disjoint.is_empty() {
let resolved_selections =
resolve_multiple(adjusted_disjoint.iter(), &self.buffer()).collect();
let map = self.display_map();
let resolved_selections = resolve_multiple(adjusted_disjoint.iter(), &map).collect();
self.select::<usize>(resolved_selections);
}
@@ -851,25 +851,55 @@ impl<'a> DerefMut for MutableSelectionsCollection<'a> {
// Panics if passed selections are not in order
pub(crate) fn resolve_multiple<'a, D, I>(
selections: I,
snapshot: &MultiBufferSnapshot,
map: &'a DisplaySnapshot,
) -> impl 'a + Iterator<Item = Selection<D>>
where
D: TextDimension + Ord + Sub<D, Output = D>,
D: TextDimension + Clone + Ord + Sub<D, Output = D>,
I: 'a + IntoIterator<Item = &'a Selection<Anchor>>,
{
let (to_summarize, selections) = selections.into_iter().tee();
let mut summaries = snapshot
.summaries_for_anchors::<D, _>(
to_summarize
.flat_map(|s| [&s.start, &s.end])
.collect::<Vec<_>>(),
)
let mut summaries = map
.buffer_snapshot
.summaries_for_anchors::<Point, _>(to_summarize.flat_map(|s| [&s.start, &s.end]))
.into_iter();
selections.map(move |s| Selection {
id: s.id,
start: summaries.next().unwrap(),
end: summaries.next().unwrap(),
reversed: s.reversed,
goal: s.goal,
let mut selection_endpoints = map.buffer_snapshot.dimensions_from_points::<D>(
iter::from_fn(move || {
let start = map.display_point_to_point(
map.point_to_display_point(summaries.next().unwrap(), Bias::Left),
Bias::Left,
);
let end = map.display_point_to_point(
map.point_to_display_point(summaries.next().unwrap(), Bias::Right),
Bias::Right,
);
Some([start, end])
})
.flatten(),
);
let mut selections = selections
.map(move |s| {
let start = selection_endpoints.next().unwrap();
let end = selection_endpoints.next().unwrap();
Selection {
id: s.id,
start,
end,
reversed: s.reversed,
goal: s.goal,
}
})
.peekable();
iter::from_fn(move || {
let mut selection = selections.next()?;
while let Some(next_selection) = selections.peek() {
if selection.end >= next_selection.start {
selection.end = cmp::max(selection.end, next_selection.end.clone());
selections.next();
} else {
break;
}
}
Some(selection)
})
}

View File

@@ -3083,6 +3083,58 @@ impl MultiBufferSnapshot {
summaries
}
pub fn dimensions_from_points<'a, D>(
&'a self,
points: impl 'a + IntoIterator<Item = Point>,
) -> impl 'a + Iterator<Item = D>
where
D: TextDimension,
{
let mut cursor = self.excerpts.cursor::<TextSummary>(&());
let mut memoized_source_start: Option<Point> = None;
let mut points = points.into_iter();
std::iter::from_fn(move || {
let point = points.next()?;
// Clear the memoized source start if the point is in a different excerpt than previous.
if memoized_source_start.map_or(false, |_| point >= cursor.end(&()).lines) {
memoized_source_start = None;
}
// Now determine where the excerpt containing the point starts in its source buffer.
// We'll use this value to calculate overshoot next.
let source_start = if let Some(source_start) = memoized_source_start {
source_start
} else {
cursor.seek_forward(&point, Bias::Right, &());
if let Some(excerpt) = cursor.item() {
let source_start = excerpt.range.context.start.to_point(&excerpt.buffer);
memoized_source_start = Some(source_start);
source_start
} else {
return Some(D::from_text_summary(cursor.start()));
}
};
// First, assume the output dimension is at least the start of the excerpt containing the point
let mut output = D::from_text_summary(cursor.start());
// If the point lands within its excerpt, calculate and add the overshoot in dimension D.
if let Some(excerpt) = cursor.item() {
let overshoot = point - cursor.start().lines;
if !overshoot.is_zero() {
let end_in_excerpt = source_start + overshoot;
output.add_assign(
&excerpt
.buffer
.text_summary_for_range::<D, _>(source_start..end_in_excerpt),
);
}
}
Some(output)
})
}
pub fn refresh_anchors<'a, I>(&'a self, anchors: I) -> Vec<(usize, Anchor, bool)>
where
I: 'a + IntoIterator<Item = &'a Anchor>,
@@ -4706,6 +4758,12 @@ impl<'a> sum_tree::SeekTarget<'a, ExcerptSummary, ExcerptSummary> for usize {
}
}
impl<'a> sum_tree::SeekTarget<'a, ExcerptSummary, TextSummary> for Point {
fn cmp(&self, cursor_location: &TextSummary, _: &()) -> cmp::Ordering {
Ord::cmp(self, &cursor_location.lines)
}
}
impl<'a> sum_tree::SeekTarget<'a, ExcerptSummary, Option<&'a Locator>> for Locator {
fn cmp(&self, cursor_location: &Option<&'a Locator>, _: &()) -> cmp::Ordering {
Ord::cmp(&Some(self), cursor_location)