Clear diagnostics from buffer when unregistering it from language server

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-04-05 16:01:33 +02:00
parent 7e3cc67e0a
commit cc45658b2d

View file

@ -1177,7 +1177,9 @@ impl Project {
old_path: PathBuf,
cx: &mut ModelContext<Self>,
) {
let buffer = &buffer.read(cx);
buffer.update(cx, |buffer, cx| {
buffer.update_diagnostics(Default::default(), cx);
self.buffer_snapshots.remove(&buffer.remote_id());
if let Some((_, language_server)) = self.language_server_for_buffer(buffer, cx) {
language_server
.notify::<lsp::notification::DidCloseTextDocument>(
@ -1189,7 +1191,7 @@ impl Project {
)
.log_err();
}
self.buffer_snapshots.remove(&buffer.remote_id());
});
}
fn on_buffer_event(
@ -5189,6 +5191,26 @@ mod tests {
},
);
rust_buffer2.update(cx, |buffer, cx| {
buffer.update_diagnostics(
DiagnosticSet::from_sorted_entries(
vec![DiagnosticEntry {
diagnostic: Default::default(),
range: Anchor::MIN..Anchor::MAX,
}],
&buffer.snapshot(),
),
cx,
);
assert_eq!(
buffer
.snapshot()
.diagnostics_in_range::<_, usize>(0..buffer.len(), false)
.count(),
1
);
});
// When the rename changes the extension of the file, the buffer gets closed on the old
// language server and gets opened on the new one.
fs.rename(
@ -5219,6 +5241,16 @@ mod tests {
language_id: Default::default()
},
);
// We clear the diagnostics, since the language has changed.
rust_buffer2.read_with(cx, |buffer, _| {
assert_eq!(
buffer
.snapshot()
.diagnostics_in_range::<_, usize>(0..buffer.len(), false)
.count(),
0
);
});
// The renamed file's version resets after changing language server.
rust_buffer2.update(cx, |buffer, cx| buffer.edit([0..0], "// ", cx));