Basic side-by-side diff implementation (#43586)
Release Notes: - N/A --------- Co-authored-by: cameron <cameron.studdstreet@gmail.com> Co-authored-by: Cameron <cameron@zed.dev>
This commit is contained in:
co-authored by
cameron
Cameron
parent
ca6e64d451
commit
2e00f40c54
@@ -1056,6 +1056,7 @@ impl Iterator for WrapRows<'_> {
|
||||
RowInfo {
|
||||
buffer_id: None,
|
||||
buffer_row: None,
|
||||
base_text_row: None,
|
||||
multibuffer_row: None,
|
||||
diff_status,
|
||||
expand_info: None,
|
||||
|
||||
@@ -36,6 +36,7 @@ mod persistence;
|
||||
mod rust_analyzer_ext;
|
||||
pub mod scroll;
|
||||
mod selections_collection;
|
||||
mod split;
|
||||
pub mod tasks;
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -69,6 +70,7 @@ pub use multi_buffer::{
|
||||
MultiBufferOffset, MultiBufferOffsetUtf16, MultiBufferSnapshot, PathKey, RowInfo, ToOffset,
|
||||
ToPoint,
|
||||
};
|
||||
pub use split::SplittableEditor;
|
||||
pub use text::Bias;
|
||||
|
||||
use ::git::{
|
||||
@@ -1198,6 +1200,7 @@ pub struct Editor {
|
||||
applicable_language_settings: HashMap<Option<LanguageName>, LanguageSettings>,
|
||||
accent_overrides: Vec<SharedString>,
|
||||
fetched_tree_sitter_chunks: HashMap<ExcerptId, HashSet<Range<BufferRow>>>,
|
||||
use_base_text_line_numbers: bool,
|
||||
}
|
||||
|
||||
fn debounce_value(debounce_ms: u64) -> Option<Duration> {
|
||||
@@ -1637,7 +1640,7 @@ pub(crate) struct FocusedBlock {
|
||||
focus_handle: WeakFocusHandle,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
enum JumpData {
|
||||
MultiBufferRow {
|
||||
row: MultiBufferRow,
|
||||
@@ -2344,6 +2347,7 @@ impl Editor {
|
||||
applicable_language_settings: HashMap::default(),
|
||||
accent_overrides: Vec::new(),
|
||||
fetched_tree_sitter_chunks: HashMap::default(),
|
||||
use_base_text_line_numbers: false,
|
||||
};
|
||||
|
||||
if is_minimap {
|
||||
@@ -19204,6 +19208,10 @@ impl Editor {
|
||||
self.display_map.read(cx).fold_placeholder.clone()
|
||||
}
|
||||
|
||||
pub fn set_use_base_text_line_numbers(&mut self, show: bool, _cx: &mut Context<Self>) {
|
||||
self.use_base_text_line_numbers = show;
|
||||
}
|
||||
|
||||
pub fn set_expand_all_diff_hunks(&mut self, cx: &mut App) {
|
||||
self.buffer.update(cx, |buffer, cx| {
|
||||
buffer.set_all_diff_hunks_expanded(cx);
|
||||
|
||||
@@ -35,7 +35,9 @@ use language_settings::Formatter;
|
||||
use languages::markdown_lang;
|
||||
use languages::rust_lang;
|
||||
use lsp::CompletionParams;
|
||||
use multi_buffer::{IndentGuide, MultiBufferOffset, MultiBufferOffsetUtf16, PathKey};
|
||||
use multi_buffer::{
|
||||
IndentGuide, MultiBufferFilterMode, MultiBufferOffset, MultiBufferOffsetUtf16, PathKey,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use pretty_assertions::{assert_eq, assert_ne};
|
||||
use project::{
|
||||
@@ -28199,6 +28201,208 @@ async fn test_multibuffer_selections_with_folding(cx: &mut TestAppContext) {
|
||||
"});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_filtered_editor_pair(cx: &mut gpui::TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
let mut leader_cx = EditorTestContext::new(cx).await;
|
||||
|
||||
let diff_base = indoc!(
|
||||
r#"
|
||||
one
|
||||
two
|
||||
three
|
||||
four
|
||||
five
|
||||
six
|
||||
"#
|
||||
);
|
||||
|
||||
let initial_state = indoc!(
|
||||
r#"
|
||||
ˇone
|
||||
two
|
||||
THREE
|
||||
four
|
||||
five
|
||||
six
|
||||
"#
|
||||
);
|
||||
|
||||
leader_cx.set_state(initial_state);
|
||||
|
||||
leader_cx.set_head_text(&diff_base);
|
||||
leader_cx.run_until_parked();
|
||||
|
||||
let follower = leader_cx.update_multibuffer(|leader, cx| {
|
||||
leader.set_filter_mode(Some(MultiBufferFilterMode::KeepInsertions));
|
||||
leader.set_all_diff_hunks_expanded(cx);
|
||||
leader.get_or_create_follower(cx)
|
||||
});
|
||||
follower.update(cx, |follower, cx| {
|
||||
follower.set_filter_mode(Some(MultiBufferFilterMode::KeepDeletions));
|
||||
follower.set_all_diff_hunks_expanded(cx);
|
||||
});
|
||||
|
||||
let follower_editor =
|
||||
leader_cx.new_window_entity(|window, cx| build_editor(follower, window, cx));
|
||||
// leader_cx.window.focus(&follower_editor.focus_handle(cx));
|
||||
|
||||
let mut follower_cx = EditorTestContext::for_editor_in(follower_editor, &mut leader_cx).await;
|
||||
cx.run_until_parked();
|
||||
|
||||
leader_cx.assert_editor_state(initial_state);
|
||||
follower_cx.assert_editor_state(indoc! {
|
||||
r#"
|
||||
ˇone
|
||||
two
|
||||
three
|
||||
four
|
||||
five
|
||||
six
|
||||
"#
|
||||
});
|
||||
|
||||
follower_cx.editor(|editor, _window, cx| {
|
||||
assert!(editor.read_only(cx));
|
||||
});
|
||||
|
||||
leader_cx.update_editor(|editor, _window, cx| {
|
||||
editor.edit([(Point::new(4, 0)..Point::new(5, 0), "FIVE\n")], cx);
|
||||
});
|
||||
cx.run_until_parked();
|
||||
|
||||
leader_cx.assert_editor_state(indoc! {
|
||||
r#"
|
||||
ˇone
|
||||
two
|
||||
THREE
|
||||
four
|
||||
FIVE
|
||||
six
|
||||
"#
|
||||
});
|
||||
|
||||
follower_cx.assert_editor_state(indoc! {
|
||||
r#"
|
||||
ˇone
|
||||
two
|
||||
three
|
||||
four
|
||||
five
|
||||
six
|
||||
"#
|
||||
});
|
||||
|
||||
leader_cx.update_editor(|editor, _window, cx| {
|
||||
editor.edit([(Point::new(6, 0)..Point::new(6, 0), "SEVEN")], cx);
|
||||
});
|
||||
cx.run_until_parked();
|
||||
|
||||
leader_cx.assert_editor_state(indoc! {
|
||||
r#"
|
||||
ˇone
|
||||
two
|
||||
THREE
|
||||
four
|
||||
FIVE
|
||||
six
|
||||
SEVEN"#
|
||||
});
|
||||
|
||||
follower_cx.assert_editor_state(indoc! {
|
||||
r#"
|
||||
ˇone
|
||||
two
|
||||
three
|
||||
four
|
||||
five
|
||||
six
|
||||
"#
|
||||
});
|
||||
|
||||
leader_cx.update_editor(|editor, window, cx| {
|
||||
editor.move_down(&MoveDown, window, cx);
|
||||
editor.refresh_selected_text_highlights(true, window, cx);
|
||||
});
|
||||
leader_cx.run_until_parked();
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_filtered_editor_pair_complex(cx: &mut gpui::TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
let base_text = "base\n";
|
||||
let buffer_text = "buffer\n";
|
||||
|
||||
let buffer1 = cx.new(|cx| Buffer::local(buffer_text, cx));
|
||||
let diff1 = cx.new(|cx| BufferDiff::new_with_base_text(base_text, &buffer1, cx));
|
||||
|
||||
let extra_buffer_1 = cx.new(|cx| Buffer::local("dummy text 1\n", cx));
|
||||
let extra_diff_1 = cx.new(|cx| BufferDiff::new_with_base_text("", &extra_buffer_1, cx));
|
||||
let extra_buffer_2 = cx.new(|cx| Buffer::local("dummy text 2\n", cx));
|
||||
let extra_diff_2 = cx.new(|cx| BufferDiff::new_with_base_text("", &extra_buffer_2, cx));
|
||||
|
||||
let leader = cx.new(|cx| {
|
||||
let mut leader = MultiBuffer::new(Capability::ReadWrite);
|
||||
leader.set_all_diff_hunks_expanded(cx);
|
||||
leader.set_filter_mode(Some(MultiBufferFilterMode::KeepInsertions));
|
||||
leader
|
||||
});
|
||||
let follower = leader.update(cx, |leader, cx| leader.get_or_create_follower(cx));
|
||||
follower.update(cx, |follower, _| {
|
||||
follower.set_filter_mode(Some(MultiBufferFilterMode::KeepDeletions));
|
||||
});
|
||||
|
||||
leader.update(cx, |leader, cx| {
|
||||
leader.insert_excerpts_after(
|
||||
ExcerptId::min(),
|
||||
extra_buffer_2.clone(),
|
||||
vec![ExcerptRange::new(text::Anchor::MIN..text::Anchor::MAX)],
|
||||
cx,
|
||||
);
|
||||
leader.add_diff(extra_diff_2.clone(), cx);
|
||||
|
||||
leader.insert_excerpts_after(
|
||||
ExcerptId::min(),
|
||||
extra_buffer_1.clone(),
|
||||
vec![ExcerptRange::new(text::Anchor::MIN..text::Anchor::MAX)],
|
||||
cx,
|
||||
);
|
||||
leader.add_diff(extra_diff_1.clone(), cx);
|
||||
|
||||
leader.insert_excerpts_after(
|
||||
ExcerptId::min(),
|
||||
buffer1.clone(),
|
||||
vec![ExcerptRange::new(text::Anchor::MIN..text::Anchor::MAX)],
|
||||
cx,
|
||||
);
|
||||
leader.add_diff(diff1.clone(), cx);
|
||||
});
|
||||
|
||||
cx.run_until_parked();
|
||||
let mut cx = cx.add_empty_window();
|
||||
|
||||
let leader_editor = cx
|
||||
.new_window_entity(|window, cx| Editor::for_multibuffer(leader.clone(), None, window, cx));
|
||||
let follower_editor = cx.new_window_entity(|window, cx| {
|
||||
Editor::for_multibuffer(follower.clone(), None, window, cx)
|
||||
});
|
||||
|
||||
let mut leader_cx = EditorTestContext::for_editor_in(leader_editor.clone(), &mut cx).await;
|
||||
leader_cx.assert_editor_state(indoc! {"
|
||||
ˇbuffer
|
||||
|
||||
dummy text 1
|
||||
|
||||
dummy text 2
|
||||
"});
|
||||
let mut follower_cx = EditorTestContext::for_editor_in(follower_editor.clone(), &mut cx).await;
|
||||
follower_cx.assert_editor_state(indoc! {"
|
||||
ˇbase
|
||||
|
||||
|
||||
"});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_multibuffer_scroll_cursor_top_margin(cx: &mut TestAppContext) {
|
||||
init_test(cx, |_| {});
|
||||
|
||||
@@ -3274,6 +3274,8 @@ impl EditorElement {
|
||||
line_number.clear();
|
||||
let non_relative_number = if relative.wrapped() {
|
||||
row_info.buffer_row.or(row_info.wrapped_buffer_row)? + 1
|
||||
} else if self.editor.read(cx).use_base_text_line_numbers {
|
||||
row_info.base_text_row?.0 + 1
|
||||
} else {
|
||||
row_info.buffer_row? + 1
|
||||
};
|
||||
@@ -3282,6 +3284,7 @@ impl EditorElement {
|
||||
&& row_info
|
||||
.diff_status
|
||||
.is_some_and(|status| status.is_deleted())
|
||||
&& !self.editor.read(cx).use_base_text_line_numbers
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -929,7 +929,11 @@ impl Item for Editor {
|
||||
})
|
||||
}
|
||||
|
||||
fn as_searchable(&self, handle: &Entity<Self>) -> Option<Box<dyn SearchableItemHandle>> {
|
||||
fn as_searchable(
|
||||
&self,
|
||||
handle: &Entity<Self>,
|
||||
_: &App,
|
||||
) -> Option<Box<dyn SearchableItemHandle>> {
|
||||
Some(Box::new(handle.clone()))
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
use feature_flags::{FeatureFlag, FeatureFlagAppExt as _};
|
||||
use gpui::{
|
||||
Action, AppContext as _, Entity, EventEmitter, Focusable, NoAction, Subscription, WeakEntity,
|
||||
};
|
||||
use multi_buffer::{MultiBuffer, MultiBufferFilterMode};
|
||||
use project::Project;
|
||||
use ui::{
|
||||
App, Context, InteractiveElement as _, IntoElement as _, ParentElement as _, Render,
|
||||
Styled as _, Window, div,
|
||||
};
|
||||
use workspace::{
|
||||
ActivePaneDecorator, Item, ItemHandle, Pane, PaneGroup, SplitDirection, Workspace,
|
||||
};
|
||||
|
||||
use crate::{Editor, EditorEvent};
|
||||
|
||||
struct SplitDiffFeatureFlag;
|
||||
|
||||
impl FeatureFlag for SplitDiffFeatureFlag {
|
||||
const NAME: &'static str = "split-diff";
|
||||
|
||||
fn enabled_for_staff() -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Action, Default)]
|
||||
#[action(namespace = editor)]
|
||||
struct SplitDiff;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Action, Default)]
|
||||
#[action(namespace = editor)]
|
||||
struct UnsplitDiff;
|
||||
|
||||
pub struct SplittableEditor {
|
||||
primary_editor: Entity<Editor>,
|
||||
secondary: Option<SecondaryEditor>,
|
||||
panes: PaneGroup,
|
||||
workspace: WeakEntity<Workspace>,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
struct SecondaryEditor {
|
||||
editor: Entity<Editor>,
|
||||
pane: Entity<Pane>,
|
||||
has_latest_selection: bool,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
impl SplittableEditor {
|
||||
pub fn primary_editor(&self) -> &Entity<Editor> {
|
||||
&self.primary_editor
|
||||
}
|
||||
|
||||
pub fn last_selected_editor(&self) -> &Entity<Editor> {
|
||||
if let Some(secondary) = &self.secondary
|
||||
&& secondary.has_latest_selection
|
||||
{
|
||||
&secondary.editor
|
||||
} else {
|
||||
&self.primary_editor
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_unsplit(
|
||||
buffer: Entity<MultiBuffer>,
|
||||
project: Entity<Project>,
|
||||
workspace: Entity<Workspace>,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
let primary_editor =
|
||||
cx.new(|cx| Editor::for_multibuffer(buffer, Some(project.clone()), window, cx));
|
||||
let pane = cx.new(|cx| {
|
||||
let mut pane = Pane::new(
|
||||
workspace.downgrade(),
|
||||
project,
|
||||
Default::default(),
|
||||
None,
|
||||
NoAction.boxed_clone(),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
pane.set_should_display_tab_bar(|_, _| false);
|
||||
pane.add_item(primary_editor.boxed_clone(), true, true, None, window, cx);
|
||||
pane
|
||||
});
|
||||
let panes = PaneGroup::new(pane);
|
||||
// TODO(split-diff) we might want to tag editor events with whether they came from primary/secondary
|
||||
let subscriptions =
|
||||
vec![
|
||||
cx.subscribe(&primary_editor, |this, _, event: &EditorEvent, cx| {
|
||||
if let EditorEvent::SelectionsChanged { .. } = event
|
||||
&& let Some(secondary) = &mut this.secondary
|
||||
{
|
||||
secondary.has_latest_selection = false;
|
||||
}
|
||||
cx.emit(event.clone())
|
||||
}),
|
||||
];
|
||||
|
||||
window.defer(cx, {
|
||||
let workspace = workspace.downgrade();
|
||||
let primary_editor = primary_editor.downgrade();
|
||||
move |window, cx| {
|
||||
workspace
|
||||
.update(cx, |workspace, cx| {
|
||||
primary_editor.update(cx, |editor, cx| {
|
||||
editor.added_to_workspace(workspace, window, cx);
|
||||
})
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
});
|
||||
Self {
|
||||
primary_editor,
|
||||
secondary: None,
|
||||
panes,
|
||||
workspace: workspace.downgrade(),
|
||||
_subscriptions: subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
fn split(&mut self, _: &SplitDiff, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if !cx.has_flag::<SplitDiffFeatureFlag>() {
|
||||
return;
|
||||
}
|
||||
if self.secondary.is_some() {
|
||||
return;
|
||||
}
|
||||
let Some(workspace) = self.workspace.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let project = workspace.read(cx).project().clone();
|
||||
let follower = self.primary_editor.update(cx, |primary, cx| {
|
||||
primary.buffer().update(cx, |buffer, cx| {
|
||||
let follower = buffer.get_or_create_follower(cx);
|
||||
buffer.set_filter_mode(Some(MultiBufferFilterMode::KeepInsertions));
|
||||
follower
|
||||
})
|
||||
});
|
||||
follower.update(cx, |follower, _| {
|
||||
follower.set_filter_mode(Some(MultiBufferFilterMode::KeepDeletions));
|
||||
});
|
||||
let secondary_editor = workspace.update(cx, |workspace, cx| {
|
||||
cx.new(|cx| {
|
||||
let mut editor = Editor::for_multibuffer(follower, Some(project), window, cx);
|
||||
// TODO(split-diff) this should be at the multibuffer level
|
||||
editor.set_use_base_text_line_numbers(true, cx);
|
||||
editor.added_to_workspace(workspace, window, cx);
|
||||
editor
|
||||
})
|
||||
});
|
||||
let secondary_pane = cx.new(|cx| {
|
||||
let mut pane = Pane::new(
|
||||
workspace.downgrade(),
|
||||
workspace.read(cx).project().clone(),
|
||||
Default::default(),
|
||||
None,
|
||||
NoAction.boxed_clone(),
|
||||
true,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
pane.set_should_display_tab_bar(|_, _| false);
|
||||
pane.add_item(
|
||||
ItemHandle::boxed_clone(&secondary_editor),
|
||||
false,
|
||||
false,
|
||||
None,
|
||||
window,
|
||||
cx,
|
||||
);
|
||||
pane
|
||||
});
|
||||
|
||||
let subscriptions =
|
||||
vec![
|
||||
cx.subscribe(&secondary_editor, |this, _, event: &EditorEvent, cx| {
|
||||
if let EditorEvent::SelectionsChanged { .. } = event
|
||||
&& let Some(secondary) = &mut this.secondary
|
||||
{
|
||||
secondary.has_latest_selection = true;
|
||||
}
|
||||
cx.emit(event.clone())
|
||||
}),
|
||||
];
|
||||
self.secondary = Some(SecondaryEditor {
|
||||
editor: secondary_editor,
|
||||
pane: secondary_pane.clone(),
|
||||
has_latest_selection: false,
|
||||
_subscriptions: subscriptions,
|
||||
});
|
||||
let primary_pane = self.panes.first_pane();
|
||||
self.panes
|
||||
.split(&primary_pane, &secondary_pane, SplitDirection::Left)
|
||||
.unwrap();
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn unsplit(&mut self, _: &UnsplitDiff, _: &mut Window, cx: &mut Context<Self>) {
|
||||
let Some(secondary) = self.secondary.take() else {
|
||||
return;
|
||||
};
|
||||
self.panes.remove(&secondary.pane).unwrap();
|
||||
self.primary_editor.update(cx, |primary, cx| {
|
||||
primary.buffer().update(cx, |buffer, _| {
|
||||
buffer.set_filter_mode(None);
|
||||
});
|
||||
});
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn added_to_workspace(
|
||||
&mut self,
|
||||
workspace: &mut Workspace,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.workspace = workspace.weak_handle();
|
||||
self.primary_editor.update(cx, |primary_editor, cx| {
|
||||
primary_editor.added_to_workspace(workspace, window, cx);
|
||||
});
|
||||
if let Some(secondary) = &self.secondary {
|
||||
secondary.editor.update(cx, |secondary_editor, cx| {
|
||||
secondary_editor.added_to_workspace(workspace, window, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EventEmitter<EditorEvent> for SplittableEditor {}
|
||||
impl Focusable for SplittableEditor {
|
||||
fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
|
||||
self.primary_editor.read(cx).focus_handle(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for SplittableEditor {
|
||||
fn render(
|
||||
&mut self,
|
||||
window: &mut ui::Window,
|
||||
cx: &mut ui::Context<Self>,
|
||||
) -> impl ui::IntoElement {
|
||||
let Some(active) = self.panes.panes().into_iter().next() else {
|
||||
return div().into_any_element();
|
||||
};
|
||||
div()
|
||||
.id("splittable-editor")
|
||||
.on_action(cx.listener(Self::split))
|
||||
.on_action(cx.listener(Self::unsplit))
|
||||
.size_full()
|
||||
.child(self.panes.render(
|
||||
None,
|
||||
&ActivePaneDecorator::new(active, &self.workspace),
|
||||
window,
|
||||
cx,
|
||||
))
|
||||
.into_any_element()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user