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

displaying diagnostic count on all open buffers in bufferline and for all buffers with diagnostics in the picker in addition with a red highlight #5536

59 changes: 54 additions & 5 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use typed::*;

use helix_core::{
char_idx_at_visual_offset, comment,
diagnostic::{Diagnostic, Severity::*},
doc_formatter::TextFormat,
encoding, find_first_non_whitespace_char, find_root, graphemes,
history::UndoKind,
Expand Down Expand Up @@ -2440,12 +2441,13 @@ fn buffer_picker(cx: &mut Context) {
path: Option<PathBuf>,
is_modified: bool,
is_current: bool,
diagnostics: Vec<Diagnostic>,
}

impl ui::menu::Item for BufferMeta {
type Data = ();
type Data = (helix_view::graphics::Style, helix_view::graphics::Style);

fn format(&self, _data: &Self::Data) -> Row {
fn format(&self, data: &Self::Data) -> Row {
let path = self
.path
.as_deref()
Expand All @@ -2455,15 +2457,61 @@ fn buffer_picker(cx: &mut Context) {
None => SCRATCH_BUFFER_NAME,
};

let mut flags = String::new();
let mut flags = Vec::new();

let diagnostics = &self.diagnostics;

let (hint, info, warning, error) =
diagnostics
.iter()
.filter_map(|d| d.severity)
.fold((0, 0, 0, 0), |mut acc, s| {
match s {
Hint => acc.0 += 1,
Info => acc.1 += 1,
Warning => acc.2 += 1,
Error => acc.3 += 1,
}

acc
});

if !diagnostics.is_empty() {
flags.push("!");
}
if self.is_modified {
flags.push('+');
}
if self.is_current {
flags.push('*');
}

Row::new([self.id.to_string(), flags, path.to_string()])
let flag = if flags.is_empty() {
"".into()
} else {
format!(" ({})", flags.join(""))
};

let (diag, text) = data;

Row::new(vec![
self.id.to_string(),
path.to_string(),
flag,
if !diagnostics.is_empty() {
format!(
" | {} error(s), {} warning(s), {} info, {} hint(s)",
error, warning, info, hint
)
} else {
String::new()
},
])
.style(if !diagnostics.is_empty() {
*diag
} else {
*text
})
}
}

Expand All @@ -2472,6 +2520,7 @@ fn buffer_picker(cx: &mut Context) {
path: doc.path().cloned(),
is_modified: doc.is_modified(),
is_current: doc.id() == current,
diagnostics: doc.diagnostics().to_vec(),
};

let picker = FilePicker::new(
Expand All @@ -2480,7 +2529,7 @@ fn buffer_picker(cx: &mut Context) {
.values()
.map(|doc| new_meta(doc))
.collect(),
(),
(cx.editor.theme.get("error"), cx.editor.theme.get("text")),
|cx, meta, action| {
cx.editor.switch(meta.id, action);
},
Expand Down
38 changes: 34 additions & 4 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,43 @@ impl EditorView {
bufferline_inactive
};

let text = format!(" {}{} ", fname, if doc.is_modified() { "[+]" } else { "" });
let diagnostic_count = doc.diagnostics().len();

let text = format!(
" {}{}{} ",
fname,
if diagnostic_count > 9 {
String::from(" 9+")
} else if diagnostic_count > 0 {
format!(" {}", diagnostic_count)
} else {
String::new()
},
if doc.is_modified() && diagnostic_count > 0 {
" [+]"
} else if doc.is_modified() {
"[+]"
} else {
""
}
);

let used_width = viewport.x.saturating_sub(x);
let rem_width = surface.area.width.saturating_sub(used_width);

x = surface
.set_stringn(x, viewport.y, text, rem_width as usize, style)
.0;
x = if diagnostic_count > 0 {
let background_style = editor.theme.get("ui.background");
let error_style = editor.theme.get("error");
let diagnostic_style = Style::reset().patch(background_style).patch(error_style);

surface
.set_stringn(x, viewport.y, text, rem_width as usize, diagnostic_style)
.0
} else {
surface
.set_stringn(x, viewport.y, text, rem_width as usize, style)
.0
};

if x >= surface.area.right() {
break;
Expand Down