Rework diff rendering to allow putting the cursor into deleted text, soft-wrapping and scrolling deleted text correctly (#22994)
Closes #12553 * [x] Fix `diff_hunk_before` * [x] Fix failure to show deleted text when expanding hunk w/ cursor on second line of the hunk * [x] Failure to expand diff hunk below the cursor. * [x] Delete the whole file, and expand the diff. Backspace over the deleted hunk, panic! * [x] Go-to-line now counts the diff hunks, but it should not * [x] backspace at the beginning of a deleted hunk deletes too much text * [x] Indent guides are rendered incorrectly * [ ] Fix randomized multi buffer tests Maybe: * [ ] Buffer search should include deleted text (in vim mode it turns out I use `/x` all the time to jump to the next x I can see). * [ ] vim: should refuse to switch into insert mode if selection is fully within a diff. * [ ] vim `o` command when cursor is on last line of deleted hunk. * [ ] vim `shift-o` on first line of deleted hunk moves cursor but doesn't insert line * [x] `enter` at end of diff hunk inserts a new line but doesn't move cursor * [x] (`shift-enter` at start of diff hunk does nothing) * [ ] Inserting a line just before an expanded hunk collapses it Release Notes: - Improved diff rendering, allowing you to navigate with your cursor inside of deleted text in diff hunks. --------- Co-authored-by: Conrad <conrad@zed.dev> Co-authored-by: Cole <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com> Co-authored-by: Michael <michael@zed.dev> Co-authored-by: Agus <agus@zed.dev> Co-authored-by: João <joao@zed.dev>
This commit is contained in:
parent
1fdae4bae0
commit
d2c55cbe3d
64 changed files with 7653 additions and 5495 deletions
|
@ -138,22 +138,27 @@ pub fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
|
|||
Vim::action(editor, cx, |vim, action: &GoToLine, cx| {
|
||||
vim.switch_mode(Mode::Normal, false, cx);
|
||||
let result = vim.update_editor(cx, |vim, editor, cx| {
|
||||
action.range.head().buffer_row(vim, editor, cx)
|
||||
let snapshot = editor.snapshot(cx);
|
||||
let buffer_row = action.range.head().buffer_row(vim, editor, cx)?;
|
||||
let current = editor.selections.newest::<Point>(cx);
|
||||
let target = snapshot
|
||||
.buffer_snapshot
|
||||
.clip_point(Point::new(buffer_row.0, current.head().column), Bias::Left);
|
||||
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
|
||||
s.select_ranges([target..target]);
|
||||
});
|
||||
|
||||
anyhow::Ok(())
|
||||
});
|
||||
let buffer_row = match result {
|
||||
None => return,
|
||||
Some(e @ Err(_)) => {
|
||||
let Some(workspace) = vim.workspace(cx) else {
|
||||
return;
|
||||
};
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
e.notify_err(workspace, cx);
|
||||
});
|
||||
if let Some(e @ Err(_)) = result {
|
||||
let Some(workspace) = vim.workspace(cx) else {
|
||||
return;
|
||||
}
|
||||
Some(Ok(result)) => result,
|
||||
};
|
||||
vim.move_cursor(Motion::StartOfDocument, Some(buffer_row.0 as usize + 1), cx);
|
||||
};
|
||||
workspace.update(cx, |workspace, cx| {
|
||||
e.notify_err(workspace, cx);
|
||||
});
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
Vim::action(editor, cx, |vim, action: &YankCommand, cx| {
|
||||
|
@ -462,7 +467,22 @@ impl Position {
|
|||
) -> Result<MultiBufferRow> {
|
||||
let snapshot = editor.snapshot(cx);
|
||||
let target = match self {
|
||||
Position::Line { row, offset } => row.saturating_add_signed(offset.saturating_sub(1)),
|
||||
Position::Line { row, offset } => {
|
||||
if let Some(anchor) = editor.active_excerpt(cx).and_then(|(_, buffer, _)| {
|
||||
editor.buffer().read(cx).buffer_point_to_anchor(
|
||||
&buffer,
|
||||
Point::new(row.saturating_sub(1), 0),
|
||||
cx,
|
||||
)
|
||||
}) {
|
||||
anchor
|
||||
.to_point(&snapshot.buffer_snapshot)
|
||||
.row
|
||||
.saturating_add_signed(*offset)
|
||||
} else {
|
||||
row.saturating_add_signed(offset.saturating_sub(1))
|
||||
}
|
||||
}
|
||||
Position::Mark { name, offset } => {
|
||||
let Some(mark) = vim.marks.get(&name.to_string()).and_then(|vec| vec.last()) else {
|
||||
return Err(anyhow!("mark {} not set", name));
|
||||
|
@ -697,7 +717,8 @@ fn generate_commands(_: &AppContext) -> Vec<VimCommand> {
|
|||
VimCommand::new(("foldc", "lose"), editor::actions::Fold)
|
||||
.bang(editor::actions::FoldRecursive)
|
||||
.range(act_on_range),
|
||||
VimCommand::new(("dif", "fupdate"), editor::actions::ToggleHunkDiff).range(act_on_range),
|
||||
VimCommand::new(("dif", "fupdate"), editor::actions::ToggleSelectedDiffHunks)
|
||||
.range(act_on_range),
|
||||
VimCommand::new(("rev", "ert"), editor::actions::RevertSelectedHunks).range(act_on_range),
|
||||
VimCommand::new(("d", "elete"), VisualDeleteLine).range(select_range),
|
||||
VimCommand::new(("y", "ank"), gpui::NoAction).range(|_, range| {
|
||||
|
|
|
@ -4,7 +4,7 @@ use editor::{
|
|||
self, find_boundary, find_preceding_boundary_display_point, FindRange, TextLayoutDetails,
|
||||
},
|
||||
scroll::Autoscroll,
|
||||
Anchor, Bias, DisplayPoint, Editor, RowExt, ToOffset,
|
||||
Anchor, Bias, DisplayPoint, Editor, RowExt, ToOffset, ToPoint,
|
||||
};
|
||||
use gpui::{actions, impl_actions, px, ViewContext};
|
||||
use language::{CharKind, Point, Selection, SelectionGoal};
|
||||
|
@ -847,7 +847,10 @@ impl Motion {
|
|||
SelectionGoal::None,
|
||||
),
|
||||
CurrentLine => (next_line_end(map, point, times), SelectionGoal::None),
|
||||
StartOfDocument => (start_of_document(map, point, times), SelectionGoal::None),
|
||||
StartOfDocument => (
|
||||
start_of_document(map, point, maybe_times),
|
||||
SelectionGoal::None,
|
||||
),
|
||||
EndOfDocument => (
|
||||
end_of_document(map, point, maybe_times),
|
||||
SelectionGoal::None,
|
||||
|
@ -1956,25 +1959,96 @@ fn start_of_next_sentence(map: &DisplaySnapshot, end_of_sentence: usize) -> Opti
|
|||
Some(map.buffer_snapshot.len())
|
||||
}
|
||||
|
||||
fn start_of_document(map: &DisplaySnapshot, point: DisplayPoint, line: usize) -> DisplayPoint {
|
||||
let mut new_point = Point::new((line - 1) as u32, 0).to_display_point(map);
|
||||
*new_point.column_mut() = point.column();
|
||||
map.clip_point(new_point, Bias::Left)
|
||||
fn go_to_line(map: &DisplaySnapshot, display_point: DisplayPoint, line: usize) -> DisplayPoint {
|
||||
let point = map.display_point_to_point(display_point, Bias::Left);
|
||||
let Some(mut excerpt) = map.buffer_snapshot.excerpt_containing(point..point) else {
|
||||
return display_point;
|
||||
};
|
||||
let offset = excerpt.buffer().point_to_offset(
|
||||
excerpt
|
||||
.buffer()
|
||||
.clip_point(Point::new((line - 1) as u32, point.column), Bias::Left),
|
||||
);
|
||||
let buffer_range = excerpt.buffer_range();
|
||||
if offset >= buffer_range.start && offset <= buffer_range.end {
|
||||
let point = map
|
||||
.buffer_snapshot
|
||||
.offset_to_point(excerpt.map_offset_from_buffer(offset));
|
||||
return map.clip_point(map.point_to_display_point(point, Bias::Left), Bias::Left);
|
||||
}
|
||||
let mut last_position = None;
|
||||
for (excerpt, buffer, range) in map.buffer_snapshot.excerpts() {
|
||||
let excerpt_range = language::ToOffset::to_offset(&range.context.start, &buffer)
|
||||
..language::ToOffset::to_offset(&range.context.end, &buffer);
|
||||
if offset >= excerpt_range.start && offset <= excerpt_range.end {
|
||||
let text_anchor = buffer.anchor_after(offset);
|
||||
let anchor = Anchor::in_buffer(excerpt, buffer.remote_id(), text_anchor);
|
||||
return anchor.to_display_point(map);
|
||||
} else if offset <= excerpt_range.start {
|
||||
let anchor = Anchor::in_buffer(excerpt, buffer.remote_id(), range.context.start);
|
||||
return anchor.to_display_point(map);
|
||||
} else {
|
||||
last_position = Some(Anchor::in_buffer(
|
||||
excerpt,
|
||||
buffer.remote_id(),
|
||||
range.context.end,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let mut last_point = last_position.unwrap().to_point(&map.buffer_snapshot);
|
||||
last_point.column = point.column;
|
||||
|
||||
map.clip_point(
|
||||
map.point_to_display_point(
|
||||
map.buffer_snapshot.clip_point(point, Bias::Left),
|
||||
Bias::Left,
|
||||
),
|
||||
Bias::Left,
|
||||
)
|
||||
}
|
||||
|
||||
fn start_of_document(
|
||||
map: &DisplaySnapshot,
|
||||
display_point: DisplayPoint,
|
||||
maybe_times: Option<usize>,
|
||||
) -> DisplayPoint {
|
||||
if let Some(times) = maybe_times {
|
||||
return go_to_line(map, display_point, times);
|
||||
}
|
||||
|
||||
let point = map.display_point_to_point(display_point, Bias::Left);
|
||||
let mut first_point = Point::zero();
|
||||
first_point.column = point.column;
|
||||
|
||||
map.clip_point(
|
||||
map.point_to_display_point(
|
||||
map.buffer_snapshot.clip_point(first_point, Bias::Left),
|
||||
Bias::Left,
|
||||
),
|
||||
Bias::Left,
|
||||
)
|
||||
}
|
||||
|
||||
fn end_of_document(
|
||||
map: &DisplaySnapshot,
|
||||
point: DisplayPoint,
|
||||
line: Option<usize>,
|
||||
display_point: DisplayPoint,
|
||||
maybe_times: Option<usize>,
|
||||
) -> DisplayPoint {
|
||||
let new_row = if let Some(line) = line {
|
||||
(line - 1) as u32
|
||||
} else {
|
||||
map.buffer_snapshot.max_row().0
|
||||
if let Some(times) = maybe_times {
|
||||
return go_to_line(map, display_point, times);
|
||||
};
|
||||
let point = map.display_point_to_point(display_point, Bias::Left);
|
||||
let mut last_point = map.buffer_snapshot.max_point();
|
||||
last_point.column = point.column;
|
||||
|
||||
let new_point = Point::new(new_row, point.column());
|
||||
map.clip_point(new_point.to_display_point(map), Bias::Left)
|
||||
map.clip_point(
|
||||
map.point_to_display_point(
|
||||
map.buffer_snapshot.clip_point(last_point, Bias::Left),
|
||||
Bias::Left,
|
||||
),
|
||||
Bias::Left,
|
||||
)
|
||||
}
|
||||
|
||||
fn matching_tag(map: &DisplaySnapshot, head: DisplayPoint) -> Option<DisplayPoint> {
|
||||
|
@ -2545,7 +2619,7 @@ fn section_motion(
|
|||
direction: Direction,
|
||||
is_start: bool,
|
||||
) -> DisplayPoint {
|
||||
if let Some((_, _, buffer)) = map.buffer_snapshot.as_singleton() {
|
||||
if map.buffer_snapshot.as_singleton().is_some() {
|
||||
for _ in 0..times {
|
||||
let offset = map
|
||||
.display_point_to_point(display_point, Bias::Left)
|
||||
|
@ -2553,13 +2627,14 @@ fn section_motion(
|
|||
let range = if direction == Direction::Prev {
|
||||
0..offset
|
||||
} else {
|
||||
offset..buffer.len()
|
||||
offset..map.buffer_snapshot.len()
|
||||
};
|
||||
|
||||
// we set a max start depth here because we want a section to only be "top level"
|
||||
// similar to vim's default of '{' in the first column.
|
||||
// (and without it, ]] at the start of editor.rs is -very- slow)
|
||||
let mut possibilities = buffer
|
||||
let mut possibilities = map
|
||||
.buffer_snapshot
|
||||
.text_object_ranges(range, language::TreeSitterOptions::max_start_depth(3))
|
||||
.filter(|(_, object)| {
|
||||
matches!(
|
||||
|
@ -2591,7 +2666,7 @@ fn section_motion(
|
|||
let offset = if direction == Direction::Prev {
|
||||
possibilities.max().unwrap_or(0)
|
||||
} else {
|
||||
possibilities.min().unwrap_or(buffer.len())
|
||||
possibilities.min().unwrap_or(map.buffer_snapshot.len())
|
||||
};
|
||||
|
||||
let new_point = map.clip_point(offset.to_display_point(&map), Bias::Left);
|
||||
|
|
|
@ -494,7 +494,7 @@ pub fn surrounding_html_tag(
|
|||
|
||||
let snapshot = &map.buffer_snapshot;
|
||||
let offset = head.to_offset(map, Bias::Left);
|
||||
let excerpt = snapshot.excerpt_containing(offset..offset)?;
|
||||
let mut excerpt = snapshot.excerpt_containing(offset..offset)?;
|
||||
let buffer = excerpt.buffer();
|
||||
let offset = excerpt.map_offset_to_buffer(offset);
|
||||
|
||||
|
@ -664,7 +664,7 @@ fn text_object(
|
|||
let snapshot = &map.buffer_snapshot;
|
||||
let offset = relative_to.to_offset(map, Bias::Left);
|
||||
|
||||
let excerpt = snapshot.excerpt_containing(offset..offset)?;
|
||||
let mut excerpt = snapshot.excerpt_containing(offset..offset)?;
|
||||
let buffer = excerpt.buffer();
|
||||
let offset = excerpt.map_offset_to_buffer(offset);
|
||||
|
||||
|
@ -710,7 +710,7 @@ fn argument(
|
|||
let offset = relative_to.to_offset(map, Bias::Left);
|
||||
|
||||
// The `argument` vim text object uses the syntax tree, so we operate at the buffer level and map back to the display level
|
||||
let excerpt = snapshot.excerpt_containing(offset..offset)?;
|
||||
let mut excerpt = snapshot.excerpt_containing(offset..offset)?;
|
||||
let buffer = excerpt.buffer();
|
||||
|
||||
fn comma_delimited_range_at(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue