Introduce DisplayRow, MultiBufferRow newtypes and BufferRow type alias (#11656)

Part of https://github.com/zed-industries/zed/issues/8081

To avoid confusion and bugs when converting between various row `u32`'s,
use different types for each.
Further PRs should split `Point` into buffer and multi buffer variants
and make the code more readable.

Release Notes:

- N/A

---------

Co-authored-by: Piotr <piotr@zed.dev>
This commit is contained in:
Kirill Bulatov 2024-05-11 00:06:51 +03:00 committed by GitHub
parent 38f110852f
commit df41435d1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 1726 additions and 1220 deletions

View file

@ -26,6 +26,7 @@ gpui.workspace = true
itertools.workspace = true
language.workspace = true
log.workspace = true
multi_buffer.workspace = true
nvim-rs = { git = "https://github.com/KillTheMule/nvim-rs", branch = "master", features = [
"use_tokio",
], optional = true }

View file

@ -1,13 +1,14 @@
use editor::{
display_map::{DisplaySnapshot, FoldPoint, ToDisplayPoint},
display_map::{DisplayRow, DisplaySnapshot, FoldPoint, ToDisplayPoint},
movement::{
self, find_boundary, find_preceding_boundary_display_point, FindRange, TextLayoutDetails,
},
scroll::Autoscroll,
Anchor, Bias, DisplayPoint, ToOffset,
Anchor, Bias, DisplayPoint, RowExt, ToOffset,
};
use gpui::{actions, impl_actions, px, ViewContext, WindowContext};
use language::{char_kind, CharKind, Point, Selection, SelectionGoal};
use multi_buffer::MultiBufferRow;
use serde::Deserialize;
use std::ops::Range;
use workspace::Workspace;
@ -843,7 +844,7 @@ impl Motion {
selection.end = map.clip_point(selection.end, Bias::Right);
// Don't reset the end here
return Some(selection.start..selection.end);
} else if selection.start.row() > 0 {
} else if selection.start.row().0 > 0 {
*selection.start.row_mut() -= 1;
*selection.start.column_mut() = map.line_len(selection.start.row());
selection.start = map.clip_point(selection.start, Bias::Left);
@ -860,10 +861,10 @@ impl Motion {
ignore_punctuation: _,
} = self
{
let start_row = selection.start.to_point(&map).row;
if selection.end.to_point(&map).row > start_row {
let start_row = MultiBufferRow(selection.start.to_point(&map).row);
if selection.end.to_point(&map).row > start_row.0 {
selection.end =
Point::new(start_row, map.buffer_snapshot.line_len(start_row))
Point::new(start_row.0, map.buffer_snapshot.line_len(start_row))
.to_display_point(&map)
}
}
@ -997,7 +998,7 @@ fn up_down_buffer_rows(
map.fold_snapshot
.clip_point(FoldPoint::new(start.row(), 0), Bias::Left),
);
let select_nth_wrapped_row = point.row() - begin_folded_line.row();
let select_nth_wrapped_row = point.row().0 - begin_folded_line.row().0;
let (goal_wrap, goal_x) = match goal {
SelectionGoal::WrappedHorizontalPosition((row, x)) => (row, x),
@ -1020,7 +1021,7 @@ fn up_down_buffer_rows(
let mut i = 0;
while i < goal_wrap && begin_folded_line.row() < map.max_point().row() {
let next_folded_line = DisplayPoint::new(begin_folded_line.row() + 1, 0);
let next_folded_line = DisplayPoint::new(begin_folded_line.row().next_row(), 0);
if map
.display_point_to_fold_point(next_folded_line, Bias::Right)
.row()
@ -1215,7 +1216,7 @@ fn previous_word_end(
let scope = map.buffer_snapshot.language_scope_at(point.to_point(map));
let mut point = point.to_point(map);
if point.column < map.buffer_snapshot.line_len(point.row) {
if point.column < map.buffer_snapshot.line_len(MultiBufferRow(point.row)) {
point.column += 1;
}
for _ in 0..times {
@ -1375,7 +1376,7 @@ fn previous_subword_end(
let scope = map.buffer_snapshot.language_scope_at(point.to_point(map));
let mut point = point.to_point(map);
if point.column < map.buffer_snapshot.line_len(point.row) {
if point.column < map.buffer_snapshot.line_len(MultiBufferRow(point.row)) {
point.column += 1;
}
for _ in 0..times {
@ -1497,7 +1498,7 @@ fn end_of_document(
let new_row = if let Some(line) = line {
(line - 1) as u32
} else {
map.max_buffer_row()
map.max_buffer_row().0
};
let new_point = Point::new(new_row, point.column());
@ -1672,22 +1673,24 @@ fn window_top(
.anchor
.to_display_point(map);
if first_visible_line.row() != 0 && text_layout_details.vertical_scroll_margin as usize > times
if first_visible_line.row() != DisplayRow(0)
&& text_layout_details.vertical_scroll_margin as usize > times
{
times = text_layout_details.vertical_scroll_margin.ceil() as usize;
}
if let Some(visible_rows) = text_layout_details.visible_rows {
let bottom_row = first_visible_line.row() + visible_rows as u32;
let new_row = (first_visible_line.row() + (times as u32))
let bottom_row = first_visible_line.row().0 + visible_rows as u32;
let new_row = (first_visible_line.row().0 + (times as u32))
.min(bottom_row)
.min(map.max_point().row());
.min(map.max_point().row().0);
let new_col = point.column().min(map.line_len(first_visible_line.row()));
let new_point = DisplayPoint::new(new_row, new_col);
let new_point = DisplayPoint::new(DisplayRow(new_row), new_col);
(map.clip_point(new_point, Bias::Left), SelectionGoal::None)
} else {
let new_row = (first_visible_line.row() + (times as u32)).min(map.max_point().row());
let new_row =
DisplayRow((first_visible_line.row().0 + (times as u32)).min(map.max_point().row().0));
let new_col = point.column().min(map.line_len(first_visible_line.row()));
let new_point = DisplayPoint::new(new_row, new_col);
@ -1707,10 +1710,11 @@ fn window_middle(
.to_display_point(map);
let max_visible_rows =
(visible_rows as u32).min(map.max_point().row() - first_visible_line.row());
(visible_rows as u32).min(map.max_point().row().0 - first_visible_line.row().0);
let new_row =
(first_visible_line.row() + (max_visible_rows / 2)).min(map.max_point().row());
(first_visible_line.row().0 + (max_visible_rows / 2)).min(map.max_point().row().0);
let new_row = DisplayRow(new_row);
let new_col = point.column().min(map.line_len(new_row));
let new_point = DisplayPoint::new(new_row, new_col);
(map.clip_point(new_point, Bias::Left), SelectionGoal::None)
@ -1730,18 +1734,19 @@ fn window_bottom(
.scroll_anchor
.anchor
.to_display_point(map);
let bottom_row = first_visible_line.row()
let bottom_row = first_visible_line.row().0
+ (visible_rows + text_layout_details.scroll_anchor.offset.y - 1.).floor() as u32;
if bottom_row < map.max_point().row()
if bottom_row < map.max_point().row().0
&& text_layout_details.vertical_scroll_margin as usize > times
{
times = text_layout_details.vertical_scroll_margin.ceil() as usize;
}
let bottom_row_capped = bottom_row.min(map.max_point().row());
let new_row = if bottom_row_capped.saturating_sub(times as u32) < first_visible_line.row() {
let bottom_row_capped = bottom_row.min(map.max_point().row().0);
let new_row = if bottom_row_capped.saturating_sub(times as u32) < first_visible_line.row().0
{
first_visible_line.row()
} else {
bottom_row_capped.saturating_sub(times as u32)
DisplayRow(bottom_row_capped.saturating_sub(times as u32))
};
let new_col = point.column().min(map.line_len(new_row));
let new_point = DisplayPoint::new(new_row, new_col);

View file

@ -25,6 +25,7 @@ use editor::Bias;
use gpui::{actions, ViewContext, WindowContext};
use language::{Point, SelectionGoal};
use log::error;
use multi_buffer::MultiBufferRow;
use workspace::Workspace;
use self::{
@ -314,7 +315,7 @@ fn insert_line_above(_: &mut Workspace, _: &InsertLineAbove, cx: &mut ViewContex
.collect();
let edits = selection_start_rows.into_iter().map(|row| {
let indent = snapshot
.indent_size_for_line(row)
.indent_size_for_line(MultiBufferRow(row))
.chars()
.collect::<String>();
let start_of_line = Point::new(row, 0);
@ -349,10 +350,10 @@ fn insert_line_below(_: &mut Workspace, _: &InsertLineBelow, cx: &mut ViewContex
.collect();
let edits = selection_end_rows.into_iter().map(|row| {
let indent = snapshot
.indent_size_for_line(row)
.indent_size_for_line(MultiBufferRow(row))
.chars()
.collect::<String>();
let end_of_line = Point::new(row, snapshot.line_len(row));
let end_of_line = Point::new(row, snapshot.line_len(MultiBufferRow(row)));
(end_of_line..end_of_line, "\n".to_string() + &indent)
});
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {

View file

@ -1,6 +1,7 @@
use editor::scroll::Autoscroll;
use gpui::ViewContext;
use language::{Bias, Point};
use multi_buffer::MultiBufferRow;
use workspace::Workspace;
use crate::{
@ -48,8 +49,10 @@ where
match vim.state().mode {
Mode::VisualLine => {
let start = Point::new(selection.start.row, 0);
let end =
Point::new(selection.end.row, snapshot.line_len(selection.end.row));
let end = Point::new(
selection.end.row,
snapshot.line_len(MultiBufferRow(selection.end.row)),
);
ranges.push(start..end);
cursor_positions.push(start..start);
}
@ -71,7 +74,7 @@ where
}
ranges.push(start..end);
if end.column == snapshot.line_len(end.row) {
if end.column == snapshot.line_len(MultiBufferRow(end.row)) {
end = snapshot.clip_point(end - Point::new(0, 1), Bias::Left);
}
cursor_positions.push(end..end)

View file

@ -7,6 +7,7 @@ use editor::{
};
use gpui::WindowContext;
use language::{Point, Selection};
use multi_buffer::MultiBufferRow;
pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &mut WindowContext) {
vim.stop_recording();
@ -29,7 +30,7 @@ pub fn delete_motion(vim: &mut Vim, motion: Motion, times: Option<usize>, cx: &m
if selection.is_empty()
&& map
.buffer_snapshot
.line_len(selection.start.to_point(&map).row)
.line_len(MultiBufferRow(selection.start.to_point(&map).row))
== 0
{
selection.end = map
@ -79,7 +80,7 @@ pub fn delete_object(vim: &mut Vim, object: Object, around: bool, cx: &mut Windo
let mut move_selection_start_to_previous_line =
|map: &DisplaySnapshot, selection: &mut Selection<DisplayPoint>| {
let start = selection.start.to_offset(map, Bias::Left);
if selection.start.row() > 0 {
if selection.start.row().0 > 0 {
should_move_to_start.insert(selection.id);
selection.start = (start - '\n'.len_utf8()).to_display_point(map);
}

View file

@ -2,6 +2,7 @@ use std::cmp;
use editor::{
display_map::ToDisplayPoint, movement, scroll::Autoscroll, ClipboardSelection, DisplayPoint,
RowExt,
};
use gpui::{impl_actions, AppContext, ViewContext};
use language::{Bias, SelectionGoal};
@ -99,13 +100,13 @@ fn paste(_: &mut Workspace, action: &Paste, cx: &mut ViewContext<Workspace>) {
.map(|selection| cmp::min(selection.start.column(), selection.end.column()))
.min()
.unwrap();
let mut row = current_selections.last().unwrap().end.row() + 1;
let mut row = current_selections.last().unwrap().end.row().next_row();
while i < clipboard_selections.len() {
let cursor =
display_map.clip_point(DisplayPoint::new(row, left), Bias::Left);
selections_to_process.push((cursor..cursor, false));
i += 1;
row += 1;
row.0 += 1;
}
}

View file

@ -1,6 +1,8 @@
use crate::Vim;
use editor::{
display_map::ToDisplayPoint, scroll::ScrollAmount, DisplayPoint, Editor, EditorSettings,
display_map::{DisplayRow, ToDisplayPoint},
scroll::ScrollAmount,
DisplayPoint, Editor, EditorSettings,
};
use gpui::{actions, ViewContext};
use language::Bias;
@ -85,11 +87,13 @@ fn scroll_editor(
if preserve_cursor_position {
let old_top = old_top_anchor.to_display_point(map);
let new_row = top.row() + selection.head().row() - old_top.row();
let new_row =
DisplayRow(top.row().0 + selection.head().row().0 - old_top.row().0);
head = map.clip_point(DisplayPoint::new(new_row, head.column()), Bias::Left)
}
let min_row = top.row() + vertical_scroll_margin as u32;
let max_row = top.row() + visible_rows - vertical_scroll_margin as u32 - 1;
let min_row = DisplayRow(top.row().0 + vertical_scroll_margin as u32);
let max_row =
DisplayRow(top.row().0 + visible_rows - vertical_scroll_margin as u32 - 1);
let new_head = if head.row() < min_row {
map.clip_point(DisplayPoint::new(min_row, head.column()), Bias::Left)

View file

@ -509,7 +509,7 @@ fn parse_replace_all(query: &str) -> Replacement {
#[cfg(test)]
mod test {
use editor::DisplayPoint;
use editor::{display_map::DisplayRow, DisplayPoint};
use indoc::indoc;
use search::BufferSearchBar;
@ -582,7 +582,7 @@ mod test {
let highlights = editor.all_text_background_highlights(cx);
assert_eq!(3, highlights.len());
assert_eq!(
DisplayPoint::new(2, 0)..DisplayPoint::new(2, 2),
DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2),
highlights[0].0
)
});

View file

@ -14,6 +14,7 @@ use itertools::Itertools;
use gpui::{actions, impl_actions, ViewContext, WindowContext};
use language::{char_kind, BufferSnapshot, CharKind, Point, Selection};
use multi_buffer::MultiBufferRow;
use serde::Deserialize;
use workspace::Workspace;
@ -724,7 +725,7 @@ fn paragraph(
let paragraph_end_row = paragraph_end.row();
let paragraph_ends_with_eof = paragraph_end_row == map.max_point().row();
let point = relative_to.to_point(map);
let current_line_is_empty = map.buffer_snapshot.is_line_blank(point.row);
let current_line_is_empty = map.buffer_snapshot.is_line_blank(MultiBufferRow(point.row));
if around {
if paragraph_ends_with_eof {
@ -733,13 +734,13 @@ fn paragraph(
}
let paragraph_start_row = paragraph_start.row();
if paragraph_start_row != 0 {
if paragraph_start_row.0 != 0 {
let previous_paragraph_last_line_start =
Point::new(paragraph_start_row - 1, 0).to_display_point(map);
Point::new(paragraph_start_row.0 - 1, 0).to_display_point(map);
paragraph_start = start_of_paragraph(map, previous_paragraph_last_line_start);
}
} else {
let next_paragraph_start = Point::new(paragraph_end_row + 1, 0).to_display_point(map);
let next_paragraph_start = Point::new(paragraph_end_row.0 + 1, 0).to_display_point(map);
paragraph_end = end_of_paragraph(map, next_paragraph_start);
}
}
@ -756,10 +757,10 @@ pub fn start_of_paragraph(map: &DisplaySnapshot, display_point: DisplayPoint) ->
return DisplayPoint::zero();
}
let is_current_line_blank = map.buffer_snapshot.is_line_blank(point.row);
let is_current_line_blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(point.row));
for row in (0..point.row).rev() {
let blank = map.buffer_snapshot.is_line_blank(row);
let blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(row));
if blank != is_current_line_blank {
return Point::new(row + 1, 0).to_display_point(map);
}
@ -773,18 +774,21 @@ pub fn start_of_paragraph(map: &DisplaySnapshot, display_point: DisplayPoint) ->
/// The trailing newline is excluded from the paragraph.
pub fn end_of_paragraph(map: &DisplaySnapshot, display_point: DisplayPoint) -> DisplayPoint {
let point = display_point.to_point(map);
if point.row == map.max_buffer_row() {
if point.row == map.max_buffer_row().0 {
return map.max_point();
}
let is_current_line_blank = map.buffer_snapshot.is_line_blank(point.row);
let is_current_line_blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(point.row));
for row in point.row + 1..map.max_buffer_row() + 1 {
let blank = map.buffer_snapshot.is_line_blank(row);
for row in point.row + 1..map.max_buffer_row().0 + 1 {
let blank = map.buffer_snapshot.is_line_blank(MultiBufferRow(row));
if blank != is_current_line_blank {
let previous_row = row - 1;
return Point::new(previous_row, map.buffer_snapshot.line_len(previous_row))
.to_display_point(map);
return Point::new(
previous_row,
map.buffer_snapshot.line_len(MultiBufferRow(previous_row)),
)
.to_display_point(map);
}
}

View file

@ -6,7 +6,7 @@ mod vim_test_context;
use std::time::Duration;
use command_palette::CommandPalette;
use editor::DisplayPoint;
use editor::{display_map::DisplayRow, DisplayPoint};
use futures::StreamExt;
use gpui::{KeyBinding, Modifiers, MouseButton, TestAppContext};
pub use neovim_backed_binding_test_context::*;
@ -235,7 +235,7 @@ async fn test_selection_on_search(cx: &mut gpui::TestAppContext) {
let highlights = editor.all_text_background_highlights(cx);
assert_eq!(3, highlights.len());
assert_eq!(
DisplayPoint::new(2, 0)..DisplayPoint::new(2, 2),
DisplayPoint::new(DisplayRow(2), 0)..DisplayPoint::new(DisplayRow(2), 2),
highlights[0].0
)
});

View file

@ -3,6 +3,7 @@ use std::time::Duration;
use editor::{ClipboardSelection, Editor};
use gpui::{ClipboardItem, ViewContext};
use language::{CharKind, Point};
use multi_buffer::MultiBufferRow;
use settings::Settings;
use crate::{state::Mode, UseSystemClipboard, Vim, VimSettings};
@ -74,10 +75,10 @@ fn copy_selections_content_internal(
// contains a newline (so that delete works as expected). We undo that change
// here.
let is_last_line = linewise
&& end.row == buffer.max_buffer_row()
&& end.row == buffer.max_buffer_row().0
&& buffer.max_point().column > 0
&& start.row < buffer.max_buffer_row()
&& start == Point::new(start.row, buffer.line_len(start.row));
&& start.row < buffer.max_buffer_row().0
&& start == Point::new(start.row, buffer.line_len(MultiBufferRow(start.row)));
if is_last_line {
start = Point::new(start.row + 1, 0);
@ -96,7 +97,7 @@ fn copy_selections_content_internal(
clipboard_selections.push(ClipboardSelection {
len: text.len() - initial_len,
is_entire_line: linewise,
first_line_indent: buffer.indent_size_for_line(start.row).len,
first_line_indent: buffer.indent_size_for_line(MultiBufferRow(start.row)).len,
});
}
}

View file

@ -9,6 +9,7 @@ use editor::{
};
use gpui::{actions, ViewContext, WindowContext};
use language::{Point, Selection, SelectionGoal};
use multi_buffer::MultiBufferRow;
use search::BufferSearchBar;
use util::ResultExt;
use workspace::{searchable::Direction, Workspace};
@ -251,9 +252,9 @@ pub fn visual_block_motion(
break;
}
if tail.row() > head.row() {
row -= 1
row.0 -= 1
} else {
row += 1
row.0 += 1
}
}
@ -313,13 +314,15 @@ pub fn visual_object(object: Object, cx: &mut WindowContext) {
// trailing newline is included in its selection from the beginning.
if object == Object::Paragraph && range.start != range.end {
let row_of_selection_end_line = selection.end.to_point(map).row;
let new_selection_end =
if map.buffer_snapshot.line_len(row_of_selection_end_line) == 0
{
Point::new(row_of_selection_end_line + 1, 0)
} else {
Point::new(row_of_selection_end_line, 1)
};
let new_selection_end = if map
.buffer_snapshot
.line_len(MultiBufferRow(row_of_selection_end_line))
== 0
{
Point::new(row_of_selection_end_line + 1, 0)
} else {
Point::new(row_of_selection_end_line, 1)
};
selection.end = new_selection_end.to_display_point(map);
}
}