editor: Prevent panic in `lsp_symbols_at_cursor` with diff hunks handling (#51077) (cherry-pick to preview) (#51079)
zed-zippy[bot]
and
Lukas Wirth
created 1 month ago
Cherry-pick of #51077 to preview
----
Fixes ZED-5M9
No test as I couldn't quite reproduce this, as the cause is mostly a
guess
Release Notes:
- Fixed a panic in `lsp_symbols_at_cursor` when dealing with diff hunks
Co-authored-by: Lukas Wirth <lukas@zed.dev>
Change summary
crates/auto_update/src/auto_update.rs | 16 ++++------------
crates/editor/src/document_symbols.rs | 3 +++
crates/rope/src/rope.rs | 18 ++++++++++++++----
3 files changed, 21 insertions(+), 16 deletions(-)
Detailed changes
@@ -212,18 +212,10 @@ pub fn init(client: Arc<Client>, cx: &mut App) {
}
pub fn check(_: &Check, window: &mut Window, cx: &mut App) {
- if let Some(message) = option_env!("ZED_UPDATE_EXPLANATION") {
- drop(window.prompt(
- gpui::PromptLevel::Info,
- "Zed was installed via a package manager.",
- Some(message),
- &["Ok"],
- cx,
- ));
- return;
- }
-
- if let Ok(message) = env::var("ZED_UPDATE_EXPLANATION") {
+ if let Some(message) = option_env!("ZED_UPDATE_EXPLANATION")
+ .map(ToOwned::to_owned)
+ .or_else(|| env::var("ZED_UPDATE_EXPLANATION").ok())
+ {
drop(window.prompt(
gpui::PromptLevel::Info,
"Zed was installed via a package manager.",
@@ -77,6 +77,9 @@ impl Editor {
let excerpt = multi_buffer_snapshot.excerpt_containing(cursor..cursor)?;
let excerpt_id = excerpt.id();
let buffer_id = excerpt.buffer_id();
+ if Some(buffer_id) != cursor.text_anchor.buffer_id {
+ return None;
+ }
let buffer = self.buffer.read(cx).buffer(buffer_id)?;
let buffer_snapshot = buffer.read(cx).snapshot();
let cursor_text_anchor = cursor.text_anchor;
@@ -693,16 +693,21 @@ impl<'a> Cursor<'a> {
}
pub fn seek_forward(&mut self, end_offset: usize) {
- debug_assert!(end_offset >= self.offset);
+ assert!(
+ end_offset >= self.offset,
+ "cannot seek backward from {} to {}",
+ self.offset,
+ end_offset
+ );
self.chunks.seek_forward(&end_offset, Bias::Right);
self.offset = end_offset;
}
pub fn slice(&mut self, end_offset: usize) -> Rope {
- debug_assert!(
+ assert!(
end_offset >= self.offset,
- "cannot slice backwards from {} to {}",
+ "cannot slice backward from {} to {}",
self.offset,
end_offset
);
@@ -730,7 +735,12 @@ impl<'a> Cursor<'a> {
}
pub fn summary<D: TextDimension>(&mut self, end_offset: usize) -> D {
- debug_assert!(end_offset >= self.offset);
+ assert!(
+ end_offset >= self.offset,
+ "cannot summarize backward from {} to {}",
+ self.offset,
+ end_offset
+ );
let mut summary = D::zero(());
if let Some(start_chunk) = self.chunks.item() {