text: Improve panic messages with more information (#42072)

Lukas Wirth created

Release Notes:

- N/A *or* Added/Fixed/Improved ...

Change summary

crates/clock/src/clock.rs      |  2 +-
crates/repl/src/repl_editor.rs | 11 ++++++-----
crates/text/src/text.rs        | 12 ++++++++++--
3 files changed, 17 insertions(+), 8 deletions(-)

Detailed changes

crates/clock/src/clock.rs 🔗

@@ -260,7 +260,7 @@ impl fmt::Debug for Lamport {
 impl fmt::Debug for Global {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "Global {{")?;
-        for timestamp in self.iter() {
+        for timestamp in self.iter().filter(|t| t.value > 0) {
             if timestamp.replica_id.0 > 0 {
                 write!(f, ", ")?;
             }

crates/repl/src/repl_editor.rs 🔗

@@ -477,11 +477,12 @@ fn language_supported(language: &Arc<Language>, cx: &mut App) -> bool {
 fn get_language(editor: WeakEntity<Editor>, cx: &mut App) -> Option<Arc<Language>> {
     editor
         .update(cx, |editor, cx| {
-            let selection = editor
-                .selections
-                .newest::<usize>(&editor.display_snapshot(cx));
-            let buffer = editor.buffer().read(cx).snapshot(cx);
-            buffer.language_at(selection.head()).cloned()
+            let display_snapshot = editor.display_snapshot(cx);
+            let selection = editor.selections.newest::<usize>(&display_snapshot);
+            display_snapshot
+                .buffer_snapshot()
+                .language_at(selection.head())
+                .cloned()
         })
         .ok()
         .flatten()

crates/text/src/text.rs 🔗

@@ -2281,12 +2281,20 @@ impl BufferSnapshot {
             } else {
                 insertion_cursor.prev();
             }
-            let insertion = insertion_cursor.item().expect("invalid insertion");
+            let Some(insertion) = insertion_cursor.item() else {
+                panic!(
+                    "invalid insertion for buffer {}@{:?} with anchor {:?}",
+                    self.remote_id(),
+                    self.version,
+                    anchor
+                );
+            };
             assert_eq!(
                 insertion.timestamp,
                 anchor.timestamp,
-                "invalid insertion for buffer {} with anchor {:?}",
+                "invalid insertion for buffer {}@{:?} and anchor {:?}",
                 self.remote_id(),
+                self.version,
                 anchor
             );