on master help sign in
Fix sorting issues of the editor wide diagnostics and apply diagnostics related review suggestions

Co-authored-by: Pascal Kuthe <pascal.kuthe@semimod.de>

commitf9b08656f41cbb9573ffb144f5dc2e24ea764ac9 authorPhilipp Mildenberger <philipp@mildenberger.me> dateFri 17 Mar 2023, 15:30 (4 years ago) parent4da6d8c
4 files changed+23−19
helix-term/src/application.rs+11−10 helix-term/src/commands/lsp.rs+10−7 helix-term/src/ui/statusline.rs+1−1 helix-view/src/editor.rs+1−1
helix-term/src/application.rs+11−10
@@ -721,7 +721,7 @@ impl Application {
721721 ));
722722 }
723723 }
724− Notification::PublishDiagnostics(mut params) => {
724+ Notification::PublishDiagnostics(params) => {
725725 let path = match params.uri.to_file_path() {
726726 Ok(path) => path,
727727 Err(_) => {
@@ -841,15 +841,10 @@ impl Application {
841841 doc.replace_diagnostics(diagnostics, server_id);
842842 }
843843
844− // Sort diagnostics first by severity and then by line numbers.
845− // Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
846− params
847− .diagnostics
848− .sort_unstable_by_key(|d| (d.severity, d.range.start));
849− let diagnostics = params
844+ let mut diagnostics = params
850845 .diagnostics
851846 .into_iter()
852− .map(|d| (d, server_id, offset_encoding))
847+ .map(|d| (d, server_id))
853848 .collect();
854849
855850 // Insert the original lsp::Diagnostics here because we may have no open document
@@ -859,10 +854,16 @@ impl Application {
859854 Entry::Occupied(o) => {
860855 let current_diagnostics = o.into_mut();
861856 // there may entries of other language servers, which is why we can't overwrite the whole entry
862− current_diagnostics.retain(|(_, lsp_id, _)| *lsp_id != server_id);
863− current_diagnostics.extend(diagnostics);
857+ current_diagnostics.retain(|(_, lsp_id)| *lsp_id != server_id);
858+ current_diagnostics.append(&mut diagnostics);
859+ // Sort diagnostics first by severity and then by line numbers.
860+ // Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
861+ current_diagnostics
862+ .sort_unstable_by_key(|(d, _)| (d.severity, d.range.start));
864863 }
865864 Entry::Vacant(v) => {
865+ diagnostics
866+ .sort_unstable_by_key(|(d, _)| (d.severity, d.range.start));
866867 v.insert(diagnostics);
867868 }
868869 };
helix-term/src/commands/lsp.rs+10−7
@@ -262,7 +262,7 @@ enum DiagnosticsFormat {
262262
263263fn diag_picker(
264264 cx: &Context,
265− diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize, OffsetEncoding)>>,
265+ diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize)>>,
266266 current_path: Option<lsp::Url>,
267267 format: DiagnosticsFormat,
268268) -> FilePicker<PickerDiagnostic> {
@@ -272,12 +272,15 @@ fn diag_picker(
272272 let mut flat_diag = Vec::new();
273273 for (url, diags) in diagnostics {
274274 flat_diag.reserve(diags.len());
275− for (diag, _, offset_encoding) in diags {
276− flat_diag.push(PickerDiagnostic {
277− url: url.clone(),
278− diag,
279− offset_encoding,
280− });
275+
276+ for (diag, ls) in diags {
277+ if let Some(ls) = cx.editor.language_servers.get_by_id(ls) {
278+ flat_diag.push(PickerDiagnostic {
279+ url: url.clone(),
280+ diag,
281+ offset_encoding: ls.offset_encoding(),
282+ });
283+ }
281284 }
282285 }
283286
helix-term/src/ui/statusline.rs+1−1
@@ -266,7 +266,7 @@ where
266266 .diagnostics
267267 .values()
268268 .flatten()
269− .fold((0, 0), |mut counts, (diag, _, _)| {
269+ .fold((0, 0), |mut counts, (diag, _)| {
270270 match diag.severity {
271271 Some(DiagnosticSeverity::WARNING) => counts.0 += 1,
272272 Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1,
helix-view/src/editor.rs+1−1
@@ -818,7 +818,7 @@ pub struct Editor {
818818 pub macro_recording: Option<(char, Vec<KeyEvent>)>,
819819 pub macro_replaying: Vec<char>,
820820 pub language_servers: helix_lsp::Registry,
821− pub diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize, OffsetEncoding)>>,
821+ pub diagnostics: BTreeMap<lsp::Url, Vec<(lsp::Diagnostic, usize)>>,
822822 pub diff_providers: DiffProviderRegistry,
823823
824824 pub debugger: Option<dap::Client>,