Preserve disk-based diagnostics whose ranges intersect with an edit since save

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2022-03-25 15:35:31 +01:00
parent 7a6fe73440
commit 865cd1960f

View file

@ -18,8 +18,8 @@ use language::{
proto::{deserialize_anchor, deserialize_version, serialize_anchor, serialize_version}, proto::{deserialize_anchor, deserialize_version, serialize_anchor, serialize_version},
range_from_lsp, Anchor, Bias, Buffer, CodeAction, CodeLabel, Completion, Diagnostic, range_from_lsp, Anchor, Bias, Buffer, CodeAction, CodeLabel, Completion, Diagnostic,
DiagnosticEntry, DiagnosticSet, Event as BufferEvent, File as _, Language, LanguageRegistry, DiagnosticEntry, DiagnosticSet, Event as BufferEvent, File as _, Language, LanguageRegistry,
LocalFile, OffsetRangeExt, Operation, PointUtf16, TextBufferSnapshot, ToLspPosition, ToOffset, LocalFile, OffsetRangeExt, Operation, Patch, PointUtf16, TextBufferSnapshot, ToLspPosition,
ToPointUtf16, Transaction, ToOffset, ToPointUtf16, Transaction,
}; };
use lsp::{DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer}; use lsp::{DiagnosticSeverity, DiagnosticTag, DocumentHighlightKind, LanguageServer};
use lsp_command::*; use lsp_command::*;
@ -1866,38 +1866,23 @@ impl Project {
}); });
let mut sanitized_diagnostics = Vec::new(); let mut sanitized_diagnostics = Vec::new();
let mut edits_since_save = snapshot let edits_since_save = Patch::new(
snapshot
.edits_since::<PointUtf16>(buffer.read(cx).saved_version()) .edits_since::<PointUtf16>(buffer.read(cx).saved_version())
.peekable(); .collect(),
let mut last_edit_old_end = PointUtf16::zero(); );
let mut last_edit_new_end = PointUtf16::zero(); for entry in diagnostics {
'outer: for entry in diagnostics { let start;
let mut start = entry.range.start; let end;
let mut end = entry.range.end; if entry.diagnostic.is_disk_based {
// Some diagnostics are based on files on disk instead of buffers' // Some diagnostics are based on files on disk instead of buffers'
// current contents. Adjust these diagnostics' ranges to reflect // current contents. Adjust these diagnostics' ranges to reflect
// any unsaved edits. // any unsaved edits.
if entry.diagnostic.is_disk_based { start = edits_since_save.old_to_new(entry.range.start);
while let Some(edit) = edits_since_save.peek() { end = edits_since_save.old_to_new(entry.range.end);
if edit.old.end <= start {
last_edit_old_end = edit.old.end;
last_edit_new_end = edit.new.end;
edits_since_save.next();
} else if edit.old.start <= end && edit.old.end >= start {
continue 'outer;
} else { } else {
break; start = entry.range.start;
} end = entry.range.end;
}
let start_overshoot = start - last_edit_old_end;
start = last_edit_new_end;
start += start_overshoot;
let end_overshoot = end - last_edit_old_end;
end = last_edit_new_end;
end += end_overshoot;
} }
let mut range = snapshot.clip_point_utf16(start, Bias::Left) let mut range = snapshot.clip_point_utf16(start, Bias::Left)
@ -5018,7 +5003,7 @@ mod tests {
} }
#[gpui::test] #[gpui::test]
async fn test_transforming_disk_based_diagnostics(cx: &mut gpui::TestAppContext) { async fn test_transforming_diagnostics(cx: &mut gpui::TestAppContext) {
cx.foreground().forbid_parking(); cx.foreground().forbid_parking();
let (mut lsp_config, mut fake_servers) = LanguageServerConfig::fake(); let (mut lsp_config, mut fake_servers) = LanguageServerConfig::fake();
@ -5243,11 +5228,13 @@ mod tests {
buffer.update(cx, |buffer, cx| { buffer.update(cx, |buffer, cx| {
buffer.edit(Some(Point::new(2, 0)..Point::new(2, 0)), " ", cx); buffer.edit(Some(Point::new(2, 0)..Point::new(2, 0)), " ", cx);
buffer.edit(Some(Point::new(2, 8)..Point::new(2, 10)), "(x: usize)", cx); buffer.edit(Some(Point::new(2, 8)..Point::new(2, 10)), "(x: usize)", cx);
buffer.edit(Some(Point::new(3, 10)..Point::new(3, 10)), "xxx", cx);
}); });
let change_notification_2 = let change_notification_2 = fake_server
fake_server.receive_notification::<lsp::notification::DidChangeTextDocument>(); .receive_notification::<lsp::notification::DidChangeTextDocument>()
.await;
assert!( assert!(
change_notification_2.await.text_document.version change_notification_2.text_document.version
> change_notification_1.text_document.version > change_notification_1.text_document.version
); );
@ -5255,7 +5242,7 @@ mod tests {
fake_server.notify::<lsp::notification::PublishDiagnostics>( fake_server.notify::<lsp::notification::PublishDiagnostics>(
lsp::PublishDiagnosticsParams { lsp::PublishDiagnosticsParams {
uri: lsp::Url::from_file_path("/dir/a.rs").unwrap(), uri: lsp::Url::from_file_path("/dir/a.rs").unwrap(),
version: Some(open_notification.text_document.version), version: Some(change_notification_2.text_document.version),
diagnostics: vec![ diagnostics: vec![
lsp::Diagnostic { lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(1, 9), lsp::Position::new(1, 11)), range: lsp::Range::new(lsp::Position::new(1, 9), lsp::Position::new(1, 11)),
@ -5295,7 +5282,7 @@ mod tests {
} }
}, },
DiagnosticEntry { DiagnosticEntry {
range: Point::new(3, 9)..Point::new(3, 11), range: Point::new(3, 9)..Point::new(3, 14),
diagnostic: Diagnostic { diagnostic: Diagnostic {
severity: DiagnosticSeverity::ERROR, severity: DiagnosticSeverity::ERROR,
message: "undefined variable 'BB'".to_string(), message: "undefined variable 'BB'".to_string(),