Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve diagnostics picker formatting #2984

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,11 @@ impl Application {
doc.set_diagnostics(diagnostics);
}

// Sort diagnostics first by URL and then by severity.
// Sort diagnostics first by severity and then by line numbers.
// Note: The `lsp::DiagnosticSeverity` enum is already defined in decreasing order
params.diagnostics.sort_unstable_by(|a, b| {
if let (Some(a), Some(b)) = (a.severity, b.severity) {
a.partial_cmp(&b).unwrap()
} else {
std::cmp::Ordering::Equal
}
});
params
.diagnostics
.sort_unstable_by_key(|d| (d.severity, d.range.start));

// Insert the original lsp::Diagnostics here because we may have no open document
// for diagnosic message and so we can't calculate the exact position.
Expand Down
51 changes: 31 additions & 20 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use tui::text::{Span, Spans};
use super::{align_view, push_jump, Align, Context, Editor};

use helix_core::{path, Selection};
use helix_view::{
editor::Action,
theme::{Modifier, Style},
};
use helix_view::{editor::Action, theme::Style};

use crate::{
compositor::{self, Compositor},
Expand Down Expand Up @@ -99,9 +96,9 @@ struct PickerDiagnostic {
}

impl ui::menu::Item for PickerDiagnostic {
type Data = DiagnosticStyles;
type Data = (DiagnosticStyles, DiagnosticsFormat);

fn label(&self, styles: &Self::Data) -> Spans {
fn label(&self, (styles, format): &Self::Data) -> Spans {
let mut style = self
.diag
.severity
Expand All @@ -125,23 +122,23 @@ impl ui::menu::Item for PickerDiagnostic {
NumberOrString::Number(n) => n.to_string(),
NumberOrString::String(s) => s.to_string(),
})
.map(|code| format!(" ({})", code))
.unwrap_or_default();

let truncated_path = path::get_truncated_path(self.url.path())
.to_string_lossy()
.into_owned();
let path = match format {
DiagnosticsFormat::HideSourcePath => String::new(),
DiagnosticsFormat::ShowSourcePath => {
let path = path::get_truncated_path(self.url.path())
.to_string_lossy()
.into_owned();
format!("{}: ", path)
}
};

Spans::from(vec![
Span::styled(
self.diag.source.clone().unwrap_or_default(),
style.add_modifier(Modifier::BOLD),
),
Span::raw(": "),
Span::styled(truncated_path, style),
Span::raw(" - "),
Span::styled(code, style.add_modifier(Modifier::BOLD)),
Span::raw(": "),
Span::raw(path),
Span::styled(&self.diag.message, style),
Span::styled(code, style),
])
}
}
Expand Down Expand Up @@ -242,10 +239,17 @@ fn sym_picker(
.truncate_start(false)
}

#[derive(Copy, Clone, PartialEq)]
enum DiagnosticsFormat {
ShowSourcePath,
HideSourcePath,
}

fn diag_picker(
cx: &Context,
diagnostics: BTreeMap<lsp::Url, Vec<lsp::Diagnostic>>,
current_path: Option<lsp::Url>,
format: DiagnosticsFormat,
offset_encoding: OffsetEncoding,
) -> FilePicker<PickerDiagnostic> {
// TODO: drop current_path comparison and instead use workspace: bool flag?
Expand All @@ -271,7 +275,7 @@ fn diag_picker(

FilePicker::new(
flat_diag,
styles,
(styles, format),
move |cx, PickerDiagnostic { url, diag }, action| {
if current_path.as_ref() == Some(url) {
let (view, doc) = current!(cx.editor);
Expand Down Expand Up @@ -383,6 +387,7 @@ pub fn diagnostics_picker(cx: &mut Context) {
cx,
[(current_url.clone(), diagnostics)].into(),
Some(current_url),
DiagnosticsFormat::HideSourcePath,
offset_encoding,
);
cx.push_layer(Box::new(overlayed(picker)));
Expand All @@ -395,7 +400,13 @@ pub fn workspace_diagnostics_picker(cx: &mut Context) {
let current_url = doc.url();
let offset_encoding = language_server.offset_encoding();
let diagnostics = cx.editor.diagnostics.clone();
let picker = diag_picker(cx, diagnostics, current_url, offset_encoding);
let picker = diag_picker(
cx,
diagnostics,
current_url,
DiagnosticsFormat::ShowSourcePath,
offset_encoding,
);
cx.push_layer(Box::new(overlayed(picker)));
}

Expand Down