From a3ea6a34d91953dcbb76a5ab4043cd984a1938c3 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 14 Mar 2022 16:17:14 +0100 Subject: [PATCH 1/2] Ensure there's at least one selection in `Editor::set_selections` This commit introduces an assertion that will cause Zed to panic as soon as the invariant gets violated. This will be useful to investigate issue #503. Co-Authored-By: Nathan Sobo --- crates/editor/src/editor.rs | 5 +++++ crates/editor/src/multi_buffer.rs | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 1a1bf7cc66..3ca5798af4 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4992,6 +4992,11 @@ impl Editor { pending_selection: Option, cx: &mut ViewContext, ) { + assert!( + !selections.is_empty() || pending_selection.is_some(), + "must have at least one selection" + ); + let old_cursor_position = self.newest_anchor_selection().head(); self.selections = selections; diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index fa35ad206b..c267ea775f 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -3357,7 +3357,7 @@ mod tests { } 40..=44 if !anchors.is_empty() => { let multibuffer = multibuffer.read(cx).read(cx); - + let prev_len = anchors.len(); anchors = multibuffer .refresh_anchors(&anchors) .into_iter() @@ -3366,6 +3366,7 @@ mod tests { // Ensure the newly-refreshed anchors point to a valid excerpt and don't // overshoot its boundaries. + assert_eq!(anchors.len(), prev_len); let mut cursor = multibuffer.excerpts.cursor::>(); for anchor in &anchors { if anchor.excerpt_id == ExcerptId::min() From 21eebede37284a7ab88fe87140469f02cb4bcf96 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Mon, 14 Mar 2022 16:53:39 +0100 Subject: [PATCH 2/2] Add more assertions to investigate #503 in the future --- crates/editor/src/editor.rs | 3 +++ crates/editor/src/multi_buffer.rs | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 3ca5798af4..df7a6c7eba 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4952,6 +4952,9 @@ impl Editor { ); let offsets = snapshot.summaries_for_anchors::(anchors_with_status.iter().map(|a| &a.1)); + assert_eq!(anchors_with_status.len(), 2 * self.selections.len()); + assert_eq!(offsets.len(), anchors_with_status.len()); + let offsets = offsets.chunks(2); let statuses = anchors_with_status .chunks(2) diff --git a/crates/editor/src/multi_buffer.rs b/crates/editor/src/multi_buffer.rs index c267ea775f..4a6169e5d3 100644 --- a/crates/editor/src/multi_buffer.rs +++ b/crates/editor/src/multi_buffer.rs @@ -3664,10 +3664,9 @@ mod tests { } // Anchor resolution - for (anchor, resolved_offset) in anchors - .iter() - .zip(snapshot.summaries_for_anchors::(&anchors)) - { + let summaries = snapshot.summaries_for_anchors::(&anchors); + assert_eq!(anchors.len(), summaries.len()); + for (anchor, resolved_offset) in anchors.iter().zip(summaries) { assert!(resolved_offset <= snapshot.len()); assert_eq!( snapshot.summary_for_anchor::(anchor),