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

Add global error/warning count statusline element #4569

Merged
merged 10 commits into from
Nov 15, 2022
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ The following statusline elements can be configured:
| `total-line-numbers` | The total line numbers of the opened file |
| `file-type` | The type of the opened file |
| `diagnostics` | The number of warnings and/or errors |
| `workspace-diagnostics` | The number of warnings and/or errors on file and workspace (ex: file-error-count/workspace-error-count) |
| `selections` | The number of active selections |
| `position` | The cursor position |
| `position-percentage` | The cursor position as a percentage of the total number of lines |
Expand Down
42 changes: 29 additions & 13 deletions helix-term/src/ui/statusline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ where
helix_view::editor::StatusLineElement::FileLineEnding => render_file_line_ending,
helix_view::editor::StatusLineElement::FileType => render_file_type,
helix_view::editor::StatusLineElement::Diagnostics => render_diagnostics,
helix_view::editor::StatusLineElement::WorkspaceDiagnostics => render_workspace_diagnostics,
helix_view::editor::StatusLineElement::Selections => render_selections,
helix_view::editor::StatusLineElement::Position => render_position,
helix_view::editor::StatusLineElement::PositionPercentage => render_position_percentage,
Expand Down Expand Up @@ -209,19 +210,7 @@ fn render_diagnostics<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let (warnings, errors) = context
.doc
.diagnostics()
.iter()
.fold((0, 0), |mut counts, diag| {
use helix_core::diagnostic::Severity;
match diag.severity {
Some(Severity::Warning) => counts.0 += 1,
Some(Severity::Error) | None => counts.1 += 1,
_ => {}
}
counts
});
let (warnings, errors) = context.doc.diagnostics_count();

if warnings > 0 {
write(
Expand All @@ -242,6 +231,33 @@ where
}
}

fn render_workspace_diagnostics<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
let (warnings, errors) = context.doc.diagnostics_count();

let (w_warnings, w_errors) = context.editor.workspace_diagnostic_count();

if warnings > 0 || w_warnings > 0 {
write(
context,
"●".to_string(),
Some(context.editor.theme.get("warning")),
);
write(context, format!(" {}/{} ", warnings, w_warnings), None);
}

if errors > 0 || w_errors > 0 {
write(
context,
"●".to_string(),
Some(context.editor.theme.get("error")),
);
write(context, format!(" {}/{} ", errors, w_errors), None);
}
}

fn render_selections<F>(context: &mut RenderContext, write: F)
where
F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
Expand Down
13 changes: 13 additions & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,19 @@ impl Document {
.sort_unstable_by_key(|diagnostic| diagnostic.range);
}

/// Gets the count pair (warnings, error) of warnings, error on document.
pub fn diagnostics_count(&self) -> (i32, i32) {
self.diagnostics().iter().fold((0, 0), |mut counts, diag| {
use helix_core::diagnostic::Severity;
match diag.severity {
Some(Severity::Warning) => counts.0 += 1,
Some(Severity::Error) | None => counts.1 += 1,
_ => {}
}
counts
})
}

/// Get the document's auto pairs. If the document has a recognized
/// language config with auto pairs configured, returns that;
/// otherwise, falls back to the global auto pairs config. If the global
Expand Down
20 changes: 19 additions & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{

use futures_util::stream::select_all::SelectAll;
use futures_util::{future, StreamExt};
use helix_lsp::Call;
use helix_lsp::{lsp::DiagnosticSeverity, Call};
use tokio_stream::wrappers::UnboundedReceiverStream;

use std::{
Expand Down Expand Up @@ -325,6 +325,9 @@ pub enum StatusLineElement {
/// A summary of the number of errors and warnings
Diagnostics,

/// A summary of the number of errors and warnings on file and workspace
WorkspaceDiagnostics,

/// The number of selections (cursors)
Selections,

Expand Down Expand Up @@ -1259,6 +1262,21 @@ impl Editor {
self.tree.is_empty()
}

/// Gets the count pair (warnings, error) of warnings, error on workspace.
pub fn workspace_diagnostic_count(&self) -> (i32, i32) {
self.diagnostics
.values()
.flatten()
.fold((0, 0), |mut counts, diag| {
match diag.severity {
Some(DiagnosticSeverity::WARNING) => counts.0 += 1,
Some(DiagnosticSeverity::ERROR) | None => counts.1 += 1,
_ => {}
}
counts
})
}
the-mikedavis marked this conversation as resolved.
Show resolved Hide resolved

pub fn ensure_cursor_in_view(&mut self, id: ViewId) {
let config = self.config();
let view = self.tree.get_mut(id);
Expand Down