Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to toggle git hunk diffs #11080

Merged
merged 58 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
f8c9c6a
Draft showing git diff hunks in the editor
SomeoneToIgnore Mar 3, 2024
6eab37d
For deletion hunk displays, try the BlockMap approach instead
SomeoneToIgnore Mar 29, 2024
d2865df
Remove git inlays
SomeoneToIgnore Apr 1, 2024
7c5d4c6
Add a noop scroll manager
SomeoneToIgnore Apr 16, 2024
e343c5c
Remove line numbers for deleted excerpts
SomeoneToIgnore Apr 16, 2024
5f88240
Deduplicate
SomeoneToIgnore Apr 16, 2024
de7af16
Use better background colors
SomeoneToIgnore Apr 16, 2024
0d9a804
Properly display deleted content
SomeoneToIgnore Apr 16, 2024
92fb290
Revert "Add a noop scroll manager"
SomeoneToIgnore Apr 17, 2024
ddf8543
Properly disable vertical scrolling for removed hunk blocks
SomeoneToIgnore Apr 17, 2024
3d9f017
Attempts to dispay a long deleted diff hunk line without wrapping
SomeoneToIgnore Apr 17, 2024
a1622d4
Wrap fix prototype
SomeoneToIgnore Apr 18, 2024
5392cf6
Avoid wrapping long lines in deleted git hunk editors
SomeoneToIgnore Apr 19, 2024
dd9b005
Use display row difference as a baseline for deleted block height
SomeoneToIgnore Apr 19, 2024
71b7e8c
Avoid synthetic drag scroll inside deleted hunk blocks
SomeoneToIgnore Apr 19, 2024
db2b9df
Exclude expanded hunks from the gutter (start)
SomeoneToIgnore Apr 19, 2024
9d1c8e2
Avoid highlighting extra row for git add hunks
SomeoneToIgnore Apr 21, 2024
2f452c8
Hide already expanded git gutter hunks
SomeoneToIgnore Apr 21, 2024
fb6c476
Avoid autoscroll for git hunk expansions
SomeoneToIgnore Apr 21, 2024
cd9ed92
Slightly increase the size of the deleted hunk marks
SomeoneToIgnore Apr 21, 2024
493aa59
Track buffer git diff base version
SomeoneToIgnore Apr 22, 2024
31ba345
Support dynamic added hunk highlight updates
SomeoneToIgnore Apr 22, 2024
28e752a
Add actions to toggle diff hunks
SomeoneToIgnore Apr 22, 2024
b81ee64
Draft hunk-related actions
SomeoneToIgnore Apr 22, 2024
8d57e95
Ensure addition hunk highlights are correct
SomeoneToIgnore Apr 23, 2024
111f2cd
Properly highlight git hunk rows
SomeoneToIgnore Apr 23, 2024
3979533
Properly use associated_range for hunks
SomeoneToIgnore Apr 24, 2024
f2eefae
Add a diff base change test
SomeoneToIgnore Apr 24, 2024
ec62e1a
Properly use hunk buffer coordinates when expanding in bulk
SomeoneToIgnore Apr 24, 2024
308b52f
Fix another deleted editor block scrolling issue
SomeoneToIgnore Apr 24, 2024
7812928
Properly handle diff_base_version
SomeoneToIgnore Apr 25, 2024
27dc6c0
Properly handle git row highlight ranges
SomeoneToIgnore Apr 25, 2024
4376a34
Fix the 2nd test
SomeoneToIgnore Apr 25, 2024
ba12579
Post-rebase fixes
SomeoneToIgnore Apr 25, 2024
c865f82
Test edits near expanded addition hunk
SomeoneToIgnore Apr 25, 2024
3a22321
Do not accept empty row highlight ranges
SomeoneToIgnore Apr 25, 2024
967fdab
Simplify expanded hunk invalidation, fix edits around addition hunk test
SomeoneToIgnore Apr 26, 2024
77c765e
Add edits around expanded removed hunk tests
SomeoneToIgnore Apr 26, 2024
cf4d03f
Move hunk expansion logic to editor
SomeoneToIgnore Apr 26, 2024
2551589
Fix incorrect text range in gutter diff hunk calculations
SomeoneToIgnore Apr 26, 2024
05d4b49
Remove redundant hovered hunks code
SomeoneToIgnore Apr 26, 2024
9ba56e4
Compare hunk display ranges when updating
SomeoneToIgnore Apr 26, 2024
c5d3a9b
Hide expanded hunks' gutter marks and their hitboxes
SomeoneToIgnore Apr 26, 2024
8191b48
Simplify gutter hunk bounds calculation
SomeoneToIgnore Apr 26, 2024
a974e84
Introduce new keybindigns for git hunk toggling
SomeoneToIgnore Apr 26, 2024
2fb9c61
Hide expanded hunks that became folded
SomeoneToIgnore Apr 26, 2024
69a50ea
Test expanded hunk [un]folding
SomeoneToIgnore Apr 29, 2024
31c9489
Add a multibuffer test
SomeoneToIgnore Apr 29, 2024
f6d2930
Add a collab hunk diff test
SomeoneToIgnore Apr 29, 2024
9b4bf55
Revert "Do not accept empty row highlight ranges"
SomeoneToIgnore Apr 29, 2024
83e7726
Fix the existing tests
SomeoneToIgnore Apr 29, 2024
e6e51f0
Restore expanded git diff hunks on unfold
SomeoneToIgnore Apr 29, 2024
f122898
Move all hunk diff code away into a separate module
SomeoneToIgnore Apr 29, 2024
ecf3c7a
Name all related code less generic
SomeoneToIgnore Apr 30, 2024
115d207
Update hunks asynchronously, with deboucing after each edit
SomeoneToIgnore Apr 30, 2024
807f9ea
Abandon the debounce idea, fix the tests
SomeoneToIgnore Apr 30, 2024
09ef08a
Use multi buffer for showing the removed lines diff
SomeoneToIgnore Apr 30, 2024
b6d3e92
Revert useless changes
SomeoneToIgnore May 1, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions assets/keymaps/default-linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@
"ctrl-alt-space": "editor::ShowCharacterPalette",
"ctrl-;": "editor::ToggleLineNumbers",
"ctrl-k ctrl-r": "editor::RevertSelectedHunks",
"ctrl-'": "editor::ToggleHunkDiff",
"ctrl-\"": "editor::ExpandAllHunkDiffs",
"ctrl-alt-g b": "editor::ToggleGitBlame"
}
},
Expand Down
2 changes: 2 additions & 0 deletions assets/keymaps/default-macos.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@
"ctrl-cmd-space": "editor::ShowCharacterPalette",
"cmd-;": "editor::ToggleLineNumbers",
"cmd-alt-z": "editor::RevertSelectedHunks",
"cmd-'": "editor::ToggleHunkDiff",
"cmd-\"": "editor::ExpandAllHunkDiffs",
"cmd-alt-g b": "editor::ToggleGitBlame"
}
},
Expand Down
15 changes: 12 additions & 3 deletions assets/settings/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@
// The list of language servers to use (or disable) for all languages.
//
// This is typically customized on a per-language basis.
"language_servers": ["..."],
"language_servers": [
"..."
],
// When to automatically save edited buffers. This setting can
// take four values.
//
Expand Down Expand Up @@ -428,7 +430,9 @@
"copilot": {
// The set of glob patterns for which copilot should be disabled
// in any matching file.
"disabled_globs": [".env"]
"disabled_globs": [
".env"
]
},
// Settings specific to journaling
"journal": {
Expand Down Expand Up @@ -539,7 +543,12 @@
// Default directories to search for virtual environments, relative
// to the current working directory. We recommend overriding this
// in your project's settings, rather than globally.
"directories": [".env", "env", ".venv", "venv"],
"directories": [
".env",
"env",
".venv",
"venv"
],
// Can also be 'csh', 'fish', and `nushell`
"activate_script": "default"
}
Expand Down
108 changes: 104 additions & 4 deletions crates/collab/src/tests/editor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ use editor::{
ConfirmCodeAction, ConfirmCompletion, ConfirmRename, Redo, Rename, RevertSelectedHunks,
ToggleCodeActions, Undo,
},
test::editor_test_context::{AssertionContextManager, EditorTestContext},
test::{
editor_hunks,
editor_test_context::{AssertionContextManager, EditorTestContext},
expanded_hunks, expanded_hunks_background_highlights,
},
Editor,
};
use futures::StreamExt;
use git::diff::DiffHunkStatus;
use gpui::{BorrowAppContext, TestAppContext, VisualContext, VisualTestContext};
use indoc::indoc;
use language::{
Expand Down Expand Up @@ -1875,7 +1880,7 @@ async fn test_inlay_hint_refresh_is_forwarded(
}

#[gpui::test]
async fn test_multiple_types_reverts(cx_a: &mut TestAppContext, cx_b: &mut TestAppContext) {
async fn test_multiple_hunk_types_revert(cx_a: &mut TestAppContext, cx_b: &mut TestAppContext) {
let mut server = TestServer::start(cx_a.executor()).await;
let client_a = server.create_client(cx_a, "user_a").await;
let client_b = server.create_client(cx_b, "user_b").await;
Expand Down Expand Up @@ -1997,8 +2002,8 @@ struct Row10;"#};
cx_a.executor().run_until_parked();
cx_b.executor().run_until_parked();

// client, selects a range in the updated buffer, and reverts it
// both host and the client observe the reverted state (with one hunk left, not covered by client's selection)
// the client selects a range in the updated buffer, expands it to see the diff for each hunk in the selection
// the host does not see the diffs toggled
editor_cx_b.set_selections_state(indoc! {r#"«ˇstruct Row;
struct Row0.1;
struct Row0.2;
Expand All @@ -2010,11 +2015,106 @@ struct Row10;"#};

struct R»ow9;
struct Row1220;"#});
editor_cx_b
.update_editor(|editor, cx| editor.toggle_hunk_diff(&editor::actions::ToggleHunkDiff, cx));
cx_a.executor().run_until_parked();
cx_b.executor().run_until_parked();
editor_cx_a.update_editor(|editor, cx| {
let snapshot = editor.snapshot(cx);
let all_hunks = editor_hunks(editor, &snapshot, cx);
let all_expanded_hunks = expanded_hunks(&editor, &snapshot, cx);
assert_eq!(
expanded_hunks_background_highlights(editor, &snapshot),
Vec::new(),
);
assert_eq!(
all_hunks,
vec![
("".to_string(), DiffHunkStatus::Added, 1..3),
("struct Row2;\n".to_string(), DiffHunkStatus::Removed, 4..4),
("struct Row5;\n".to_string(), DiffHunkStatus::Modified, 6..7),
("struct Row8;\n".to_string(), DiffHunkStatus::Removed, 9..9),
(
"struct Row10;".to_string(),
DiffHunkStatus::Modified,
10..10,
),
]
);
assert_eq!(all_expanded_hunks, Vec::new());
});
editor_cx_b.update_editor(|editor, cx| {
let snapshot = editor.snapshot(cx);
let all_hunks = editor_hunks(editor, &snapshot, cx);
let all_expanded_hunks = expanded_hunks(&editor, &snapshot, cx);
assert_eq!(
expanded_hunks_background_highlights(editor, &snapshot),
vec![1..3, 8..9],
);
assert_eq!(
all_hunks,
vec![
("".to_string(), DiffHunkStatus::Added, 1..3),
("struct Row2;\n".to_string(), DiffHunkStatus::Removed, 5..5),
("struct Row5;\n".to_string(), DiffHunkStatus::Modified, 8..9),
(
"struct Row8;\n".to_string(),
DiffHunkStatus::Removed,
12..12
),
(
"struct Row10;".to_string(),
DiffHunkStatus::Modified,
13..13,
),
]
);
assert_eq!(all_expanded_hunks, &all_hunks[..all_hunks.len() - 1]);
});

// the client reverts the hunks, removing the expanded diffs too
// both host and the client observe the reverted state (with one hunk left, not covered by client's selection)
editor_cx_b.update_editor(|editor, cx| {
editor.revert_selected_hunks(&RevertSelectedHunks, cx);
});
cx_a.executor().run_until_parked();
cx_b.executor().run_until_parked();
editor_cx_a.update_editor(|editor, cx| {
let snapshot = editor.snapshot(cx);
let all_hunks = editor_hunks(editor, &snapshot, cx);
let all_expanded_hunks = expanded_hunks(&editor, &snapshot, cx);
assert_eq!(
expanded_hunks_background_highlights(editor, &snapshot),
Vec::new(),
);
assert_eq!(
all_hunks,
vec![(
"struct Row10;".to_string(),
DiffHunkStatus::Modified,
10..10,
)]
);
assert_eq!(all_expanded_hunks, Vec::new());
});
editor_cx_b.update_editor(|editor, cx| {
let snapshot = editor.snapshot(cx);
let all_hunks = editor_hunks(editor, &snapshot, cx);
let all_expanded_hunks = expanded_hunks(&editor, &snapshot, cx);
assert_eq!(
expanded_hunks_background_highlights(editor, &snapshot),
Vec::new(),
);
assert_eq!(
all_hunks,
vec![(
"struct Row10;".to_string(),
DiffHunkStatus::Modified,
10..10,
)]
);
assert_eq!(all_expanded_hunks, Vec::new());
});
editor_cx_a.assert_editor_state(indoc! {r#"struct Row;
struct Row1;
struct Row2;
Expand Down
2 changes: 2 additions & 0 deletions crates/editor/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ gpui::actions!(
TabPrev,
ToggleGitBlame,
ToggleGitBlameInline,
ToggleHunkDiff,
ExpandAllHunkDiffs,
ToggleInlayHints,
ToggleLineNumbers,
ToggleSoftWrap,
Expand Down
47 changes: 26 additions & 21 deletions crates/editor/src/display_map/block_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,28 +364,33 @@ impl BlockMap {
(position.row(), TransformBlock::Custom(block.clone()))
}),
);
blocks_in_edit.extend(
buffer
.excerpt_boundaries_in_range((start_bound, end_bound))
.map(|excerpt_boundary| {
(
wrap_snapshot
.make_wrap_point(Point::new(excerpt_boundary.row, 0), Bias::Left)
.row(),
TransformBlock::ExcerptHeader {
id: excerpt_boundary.id,
buffer: excerpt_boundary.buffer,
range: excerpt_boundary.range,
height: if excerpt_boundary.starts_new_buffer {
self.buffer_header_height
} else {
self.excerpt_header_height
if buffer.show_headers() {
blocks_in_edit.extend(
buffer
.excerpt_boundaries_in_range((start_bound, end_bound))
.map(|excerpt_boundary| {
(
wrap_snapshot
.make_wrap_point(
Point::new(excerpt_boundary.row, 0),
Bias::Left,
)
.row(),
TransformBlock::ExcerptHeader {
id: excerpt_boundary.id,
buffer: excerpt_boundary.buffer,
range: excerpt_boundary.range,
height: if excerpt_boundary.starts_new_buffer {
self.buffer_header_height
} else {
self.excerpt_header_height
},
starts_new_buffer: excerpt_boundary.starts_new_buffer,
},
starts_new_buffer: excerpt_boundary.starts_new_buffer,
},
)
}),
);
)
}),
);
}

// Place excerpt headers above custom blocks on the same row.
blocks_in_edit.sort_unstable_by(|(row_a, block_a), (row_b, block_b)| {
Expand Down