Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix stale breakpoints when re-opening buffers for a saved file (#55610)

Lukas Wirthfd3055cf562b
diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rs
index 05fb9425c6a..03f332821a8 100644
--- a/crates/editor/src/editor_tests.rs
+++ b/crates/editor/src/editor_tests.rs
@@ -41,7 +41,7 @@ use multi_buffer::{IndentGuide, MultiBuffer, MultiBufferOffset, MultiBufferOffse
use parking_lot::Mutex;
use pretty_assertions::{assert_eq, assert_ne};
use project::{
- FakeFs, Project,
+ FakeFs, Project, ProjectPath,
bookmark_store::SerializedBookmark,
debugger::breakpoint_store::{BreakpointState, SourceBreakpoint},
project_settings::LspSettings,
@@ -27994,6 +27994,109 @@ async fn test_breakpoint_toggling(cx: &mut TestAppContext) {
assert_breakpoint(&breakpoints, &abs_path, vec![]);
}
+#[gpui::test]
+async fn test_breakpoint_after_save_as_existing_path(cx: &mut TestAppContext) {
+ init_test(cx, |_| {});
+
+ let fs = FakeFs::new(cx.executor());
+ fs.insert_tree(
+ path!("/a"),
+ json!({
+ "main.rs": "First line\nSecond line\nThird line\nFourth line",
+ }),
+ )
+ .await;
+ let project = Project::test(fs, [path!("/a").as_ref()], cx).await;
+ let (multi_workspace, cx) =
+ cx.add_window_view(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
+ let workspace =
+ multi_workspace.read_with(cx, |multi_workspace, _| multi_workspace.workspace().clone());
+
+ let worktree_id = workspace.update(cx, |workspace, cx| {
+ workspace.project().update(cx, |project, cx| {
+ project.worktrees(cx).next().unwrap().read(cx).id()
+ })
+ });
+
+ let first_buffer = project
+ .update(cx, |project, cx| {
+ project.open_buffer((worktree_id, rel_path("main.rs")), cx)
+ })
+ .await
+ .unwrap();
+
+ let (first_editor, cx) = cx.add_window_view(|window, cx| {
+ Editor::new(
+ EditorMode::full(),
+ MultiBuffer::build_from_buffer(first_buffer, cx),
+ Some(project.clone()),
+ window,
+ cx,
+ )
+ });
+
+ first_editor.update_in(cx, |editor, window, cx| {
+ editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
+ });
+
+ let replacement_buffer = project.update(cx, |project, cx| {
+ project.create_local_buffer("Alpha\nBeta\nGamma", None, true, cx)
+ });
+ project
+ .update(cx, |project, cx| {
+ project.save_buffer_as(
+ replacement_buffer.clone(),
+ ProjectPath {
+ worktree_id,
+ path: rel_path("main.rs").into(),
+ },
+ cx,
+ )
+ })
+ .await
+ .unwrap();
+
+ let (replacement_editor, cx) = cx.add_window_view(|window, cx| {
+ Editor::new(
+ EditorMode::full(),
+ MultiBuffer::build_from_buffer(replacement_buffer, cx),
+ Some(project.clone()),
+ window,
+ cx,
+ )
+ });
+
+ replacement_editor.update_in(cx, |editor, window, cx| {
+ editor.move_down(&MoveDown, window, cx);
+ editor.toggle_breakpoint(&actions::ToggleBreakpoint, window, cx);
+ });
+
+ let project_path = first_editor.update(cx, |editor, cx| editor.project_path(cx).unwrap());
+ let abs_path = project.read_with(cx, |project, cx| {
+ project
+ .absolute_path(&project_path, cx)
+ .map(Arc::from)
+ .unwrap()
+ });
+
+ let breakpoints = first_editor.update(cx, |editor, cx| {
+ editor
+ .breakpoint_store()
+ .as_ref()
+ .unwrap()
+ .read(cx)
+ .source_breakpoints_from_path(&abs_path, cx)
+ });
+
+ assert_eq!(
+ vec![0, 1],
+ breakpoints
+ .into_iter()
+ .map(|breakpoint| breakpoint.row)
+ .collect::<Vec<_>>()
+ );
+}
+
#[gpui::test]
async fn test_log_breakpoint_editing(cx: &mut TestAppContext) {
init_test(cx, |_| {});
diff --git a/crates/project/src/debugger/breakpoint_store.rs b/crates/project/src/debugger/breakpoint_store.rs
index 50df9ae3125..f307afe888a 100644
--- a/crates/project/src/debugger/breakpoint_store.rs
+++ b/crates/project/src/debugger/breakpoint_store.rs
@@ -16,7 +16,7 @@ use rpc::{
proto::{self},
};
use std::{hash::Hash, ops::Range, path::Path, sync::Arc, u32};
-use text::{Point, PointUtf16};
+use text::{Bias, Point, PointUtf16, Unclipped};
use util::maybe;
use crate::{ProjectPath, buffer_store::BufferStore, worktree_store::WorktreeStore};
@@ -415,7 +415,28 @@ impl BreakpointStore {
let breakpoint_set = self
.breakpoints
.entry(abs_path.clone())
- .or_insert_with(|| BreakpointsInFile::new(buffer, cx));
+ .or_insert_with(|| BreakpointsInFile::new(buffer.clone(), cx));
+
+ // Buffers changed for the file, migrate breakpoints to the new buffer
+ if breakpoint_set.buffer != buffer {
+ let old_snapshot = breakpoint_set.buffer.read(cx).snapshot();
+ let new_snapshot = buffer.read(cx).snapshot();
+ let breakpoints = breakpoint_set
+ .breakpoints
+ .drain(..)
+ .map(|mut breakpoint| {
+ let old_position =
+ old_snapshot.summary_for_anchor::<PointUtf16>(breakpoint.position());
+ let new_position = PointUtf16::new(old_position.row, 0);
+ let new_position =
+ new_snapshot.clip_point_utf16(Unclipped(new_position), Bias::Left);
+ breakpoint.bp.position = new_snapshot.anchor_after(new_position);
+ breakpoint
+ })
+ .collect();
+ *breakpoint_set = BreakpointsInFile::new(buffer, cx);
+ breakpoint_set.breakpoints = breakpoints;
+ }
match edit_action {
BreakpointEditAction::Toggle => {