Fix sorting issues of the editor wide diagnostics and apply diagnostics related review suggestions

Co-authored-by: Pascal Kuthe <pascal.kuthe@semimod.de>
pull/2507/head
Philipp Mildenberger 1 year ago
parent 4da6d8ccc7
commit f9b08656f4

@ -721,7 +721,7 @@ impl Application {
)); ));
} }
} }
Notification::PublishDiagnostics(mut params) => { Notification::PublishDiagnostics(params) => {
let path = match params.uri.to_file_path() { let path = match params.uri.to_file_path() {
Ok(path) => path, Ok(path) => path,
Err(_) => { Err(_) => {
@ -841,15 +841,10 @@ impl Application {
doc.replace_diagnostics(diagnostics, server_id); doc.replace_diagnostics(diagnostics, server_id);
} }
// Sort diagnostics first by severity and then by line numbers. let mut diagnostics = params
// Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
params
.diagnostics
.sort_unstable_by_key(|d| (d.severity, d.range.start));
let diagnostics = params
.diagnostics .diagnostics
.into_iter() .into_iter()
.map(|d| (d, server_id, offset_encoding)) .map(|d| (d, server_id))
.collect(); .collect();
// Insert the original lsp::Diagnostics here because we may have no open document // Insert the original lsp::Diagnostics here because we may have no open document
@ -859,10 +854,16 @@ impl Application {
Entry::Occupied(o) => { Entry::Occupied(o) => {
let current_diagnostics = o.into_mut(); let current_diagnostics = o.into_mut();
// there may entries of other language servers, which is why we can't overwrite the whole entry // there may entries of other language servers, which is why we can't overwrite the whole entry
current_diagnostics.retain(|(_, lsp_id, _)| *lsp_id != server_id); current_diagnostics.retain(|(_, lsp_id)| *lsp_id != server_id);
current_diagnostics.extend(diagnostics); current_diagnostics.append(&mut diagnostics);
// Sort diagnostics first by severity and then by line numbers.
// Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
current_diagnostics
.sort_unstable_by_key(|(d, _)| (d.severity, d.range.start));
} }
Entry::Vacant(v) => { Entry::Vacant(v) => {
diagnostics
.sort_unstable_by_key(|(d, _)| (d.severity, d.range.start));
v.insert(diagnostics); v.insert(diagnostics);
} }
}; };

@ -262,7 +262,7 @@ enum DiagnosticsFormat {
fn diag_picker( fn diag_picker(
cx: &Context, cx: &Context,
diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize, OffsetEncoding)>>, diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize)>>,
current_path: Option<lsp::Url>, current_path: Option<lsp::Url>,
format: DiagnosticsFormat, format: DiagnosticsFormat,
) -> FilePicker<PickerDiagnostic> { ) -> FilePicker<PickerDiagnostic> {
@ -272,12 +272,15 @@ fn diag_picker(
let mut flat_diag = Vec::new(); let mut flat_diag = Vec::new();
for (url, diags) in diagnostics { for (url, diags) in diagnostics {
flat_diag.reserve(diags.len()); flat_diag.reserve(diags.len());
for (diag, _, offset_encoding) in diags {
flat_diag.push(PickerDiagnostic { for (diag, ls) in diags {
url: url.clone(), if let Some(ls) = cx.editor.language_servers.get_by_id(ls) {
diag, flat_diag.push(PickerDiagnostic {
offset_encoding, url: url.clone(),
}); diag,
offset_encoding: ls.offset_encoding(),
});
}
} }
} }

@ -266,7 +266,7 @@ where
.diagnostics .diagnostics
.values() .values()
.flatten() .flatten()
.fold((0, 0), |mut counts, (diag, _, _)| { .fold((0, 0), |mut counts, (diag, _)| {
match diag.severity { match diag.severity {
Some(DiagnosticSeverity::WARNING) => counts.0 += 1, Some(DiagnosticSeverity::WARNING) => counts.0 += 1,
Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1, Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1,

@ -818,7 +818,7 @@ pub struct Editor {
pub macro_recording: Option<(char, Vec<KeyEvent>)>, pub macro_recording: Option<(char, Vec<KeyEvent>)>,
pub macro_replaying: Vec<char>, pub macro_replaying: Vec<char>,
pub language_servers: helix_lsp::Registry, pub language_servers: helix_lsp::Registry,
pub diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize, OffsetEncoding)>>, pub diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize)>>,
pub diff_providers: DiffProviderRegistry, pub diff_providers: DiffProviderRegistry,
pub debugger: Option<dap::Client>, pub debugger: Option<dap::Client>,

Loading…
Cancel
Save