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:
parent
7a6fe73440
commit
865cd1960f
1 changed files with 26 additions and 39 deletions
|
@ -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(
|
||||||
.edits_since::<PointUtf16>(buffer.read(cx).saved_version())
|
snapshot
|
||||||
.peekable();
|
.edits_since::<PointUtf16>(buffer.read(cx).saved_version())
|
||||||
let mut last_edit_old_end = PointUtf16::zero();
|
.collect(),
|
||||||
let mut last_edit_new_end = PointUtf16::zero();
|
);
|
||||||
'outer: for entry in diagnostics {
|
for entry in diagnostics {
|
||||||
let mut start = entry.range.start;
|
let start;
|
||||||
let mut end = entry.range.end;
|
let end;
|
||||||
|
|
||||||
// Some diagnostics are based on files on disk instead of buffers'
|
|
||||||
// current contents. Adjust these diagnostics' ranges to reflect
|
|
||||||
// any unsaved edits.
|
|
||||||
if entry.diagnostic.is_disk_based {
|
if entry.diagnostic.is_disk_based {
|
||||||
while let Some(edit) = edits_since_save.peek() {
|
// Some diagnostics are based on files on disk instead of buffers'
|
||||||
if edit.old.end <= start {
|
// current contents. Adjust these diagnostics' ranges to reflect
|
||||||
last_edit_old_end = edit.old.end;
|
// any unsaved edits.
|
||||||
last_edit_new_end = edit.new.end;
|
start = edits_since_save.old_to_new(entry.range.start);
|
||||||
edits_since_save.next();
|
end = edits_since_save.old_to_new(entry.range.end);
|
||||||
} else if edit.old.start <= end && edit.old.end >= start {
|
} else {
|
||||||
continue 'outer;
|
start = entry.range.start;
|
||||||
} else {
|
end = entry.range.end;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue