Repository: helix-editor/helix
File: helix-term/src/ui/statusline.rs (Rust)

Code before:
            // Even if there's no spinner; reserve its space to avoid elements frequently shifting.
            .unwrap_or(" ")
            .to_string(),
        None,
    );
}

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
        });

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

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

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

Review comment (bug):
[It could be smaller](https://github.com/rust-lang/rust/issues/139702) (both the source and the machine-code)

I'm unsure if the code is worth rewriting as it's not really perf-critical.

However, the implicit `i32` instead of something like `u16` feels like it could be improved.

I had the idea that if all of the counts are "exhausted" (either by overflow, or when reaching an arbitrary limit), then the loop can `break` early and the counts could be displayed as "999+" (just an example)

Code after:
            // Even if there's no spinner; reserve its space to avoid elements frequently shifting.
            .unwrap_or(" ")
            .to_string(),
        None,
    );
}

fn render_diagnostics<F>(context: &mut RenderContext, write: F)
where
    F: Fn(&mut RenderContext, String, Option<Style>) + Copy,
{
    use helix_core::diagnostic::Severity;
    let (hints, info, warnings, errors) =
        context
            .doc
            .diagnostics()
            .iter()
            .fold((0, 0, 0, 0), |mut counts, diag| {
                match diag.severity {
                    Some(Severity::Hint) | None => counts.0 += 1,
                    Some(Severity::Info) => counts.1 += 1,
                    Some(Severity::Warning) => counts.2 += 1,
                    Some(Severity::Error) => counts.3 += 1,
                }
                counts
            });

    for sev in &context.editor.config().statusline.diagnostics {
        match sev {
            Severity::Hint if hints > 0 => {
                write(
                    context,
                    "●".to_string(),
                    Some(context.editor.theme.get("hint")),
                );
                write(context, format!(" {} ", hints), None);
            }
            Severity::Info if info > 0 => {
                write(
                    context,
                    "●".to_string(),
                    Some(context.editor.theme.get("info")),
                );
                write(context, format!(" {} ", info), None);
            }
            Severity::Warning if warnings > 0 => {
                write(
                    context,
                    "●".to_string(),
                    Some(context.editor.theme.get("warning")),
                );