Skip to content

Commit

Permalink
editor: Use proper rows for fold indicators in the gutter (#11741)
Browse files Browse the repository at this point in the history
Follow-up to #11656

Release Notes:

- N/A

---------

Co-authored-by: Kirill <[email protected]>
  • Loading branch information
osiewicz and SomeoneToIgnore authored May 13, 2024
1 parent 1afcd12 commit c90263d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
20 changes: 7 additions & 13 deletions crates/editor/src/display_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,6 @@ pub trait ToDisplayPoint {
type TextHighlights = TreeMap<Option<TypeId>, Arc<(HighlightStyle, Vec<Range<Anchor>>)>>;
type InlayHighlights = TreeMap<TypeId, TreeMap<InlayId, (HighlightStyle, InlayHighlight)>>;

#[derive(Clone)]
pub struct DisplayBufferRows<'a>(BlockBufferRows<'a>);

impl<'a> Iterator for DisplayBufferRows<'a> {
type Item = Option<DisplayRow>;

fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|row| row.map(|r| DisplayRow(r.0)))
}
}

/// Decides how text in a [`MultiBuffer`] should be displayed in a buffer, handling inlay hints,
/// folding, hard tabs, soft wrapping, custom blocks (like diagnostics), and highlighting.
///
Expand Down Expand Up @@ -398,8 +387,13 @@ impl DisplaySnapshot {
self.buffer_snapshot.len() == 0
}

pub fn display_rows(&self, start_row: DisplayRow) -> DisplayBufferRows {
DisplayBufferRows(self.block_snapshot.buffer_rows(BlockRow(start_row.0)))
pub fn buffer_rows(
&self,
start_row: DisplayRow,
) -> impl Iterator<Item = Option<MultiBufferRow>> + '_ {
self.block_snapshot
.buffer_rows(BlockRow(start_row.0))
.map(|row| row.map(|row| MultiBufferRow(row.0)))
}

pub fn max_buffer_row(&self) -> MultiBufferRow {
Expand Down
26 changes: 10 additions & 16 deletions crates/editor/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ impl EditorElement {
let end = rows.end.max(relative_to);

let buffer_rows = snapshot
.display_rows(start)
.buffer_rows(start)
.take(1 + end.minus(start) as usize)
.collect::<Vec<_>>();

Expand Down Expand Up @@ -1554,7 +1554,7 @@ impl EditorElement {
fn layout_line_numbers(
&self,
rows: Range<DisplayRow>,
buffer_rows: impl Iterator<Item = Option<DisplayRow>>,
buffer_rows: impl Iterator<Item = Option<MultiBufferRow>>,
active_rows: &BTreeMap<DisplayRow, bool>,
newest_selection_head: Option<DisplayPoint>,
snapshot: &EditorSnapshot,
Expand Down Expand Up @@ -1602,10 +1602,10 @@ impl EditorElement {
} else {
(false, cx.theme().colors().editor_line_number)
};
if let Some(display_row) = row {
if let Some(multibuffer_row) = row {
if include_line_numbers {
line_number.clear();
let default_number = display_row.0 + 1;
let default_number = multibuffer_row.0 + 1;
let number = relative_rows
.get(&DisplayRow(ix as u32 + rows.start.0))
.unwrap_or(&default_number);
Expand All @@ -1628,9 +1628,6 @@ impl EditorElement {
fold_statuses.push(
is_singleton
.then(|| {
let multibuffer_point =
DisplayPoint::new(display_row, 0).to_point(snapshot);
let multibuffer_row = MultiBufferRow(multibuffer_point.row);
snapshot
.fold_for_line(multibuffer_row)
.map(|fold_status| (fold_status, multibuffer_row, active))
Expand Down Expand Up @@ -3861,8 +3858,9 @@ impl Element for EditorElement {
let end_row = DisplayRow(end_row);

let buffer_rows = snapshot
.display_rows(start_row)
.take((start_row..end_row).len());
.buffer_rows(start_row)
.take((start_row..end_row).len())
.collect::<Vec<_>>();

let start_anchor = if start_row == Default::default() {
Anchor::min()
Expand Down Expand Up @@ -3905,7 +3903,7 @@ impl Element for EditorElement {

let (line_numbers, fold_statuses) = self.layout_line_numbers(
start_row..end_row,
buffer_rows.clone(),
buffer_rows.clone().into_iter(),
&active_rows,
newest_selection_head,
&snapshot,
Expand Down Expand Up @@ -3975,11 +3973,7 @@ impl Element for EditorElement {
}

let blamed_display_rows = self.layout_blame_entries(
buffer_rows.map(|display_row| {
display_row.map(|row| {
MultiBufferRow(DisplayPoint::new(row, 0).to_point(&snapshot).row)
})
}),
buffer_rows.into_iter(),
em_width,
scroll_position,
line_height,
Expand Down Expand Up @@ -4884,7 +4878,7 @@ mod tests {
element
.layout_line_numbers(
DisplayRow(0)..DisplayRow(6),
(0..6).map(DisplayRow).map(Some),
(0..6).map(MultiBufferRow).map(Some),
&Default::default(),
Some(DisplayPoint::new(DisplayRow(0), 0)),
&snapshot,
Expand Down

0 comments on commit c90263d

Please sign in to comment.