Fix crash when restarting a language server after it reports an unknown buffer version

Co-authored-by: Antonio Scandurra <antonio@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-01-10 16:27:13 -08:00
parent a222821dfa
commit 41ff42ddec
2 changed files with 64 additions and 18 deletions

View file

@ -2081,6 +2081,7 @@ impl Project {
.buffer_snapshots
.entry(buffer.remote_id())
.or_insert_with(|| vec![(0, buffer.text_snapshot())]);
let (version, initial_snapshot) = versions.last().unwrap();
let uri = lsp::Url::from_file_path(file.abs_path(cx)).unwrap();
language_server
@ -2617,6 +2618,7 @@ impl Project {
worktree_id: worktree.read(cx).id(),
path: relative_path.into(),
};
if let Some(buffer) = self.get_open_buffer(&project_path, cx) {
self.update_buffer_diagnostics(&buffer, diagnostics.clone(), version, cx)?;
}
@ -6124,25 +6126,20 @@ impl Project {
.buffer_snapshots
.get_mut(&buffer_id)
.ok_or_else(|| anyhow!("no snapshot found for buffer {}", buffer_id))?;
let mut found_snapshot = None;
snapshots.retain(|(snapshot_version, snapshot)| {
if snapshot_version + OLD_VERSIONS_TO_RETAIN < version {
false
} else {
if *snapshot_version == version {
found_snapshot = Some(snapshot.clone());
}
true
}
let found_snapshot = snapshots
.binary_search_by_key(&version, |e| e.0)
.map(|ix| snapshots[ix].1.clone())
.map_err(|_| {
anyhow!(
"snapshot not found for buffer {} at version {}",
buffer_id,
version
)
})?;
snapshots.retain(|(snapshot_version, _)| {
snapshot_version + OLD_VERSIONS_TO_RETAIN >= version
});
found_snapshot.ok_or_else(|| {
anyhow!(
"snapshot not found for buffer {} at version {}",
buffer_id,
version
)
})
Ok(found_snapshot)
} else {
Ok((buffer.read(cx)).text_snapshot())
}