mdwright-lint 0.1.2

Lint diagnostics, rule execution, suppressions, and standard rules for mdwright
Documentation
//! `math/unbalanced-env` — LaTeX `\begin{env}` with no matching
//! `\end{env}` at the same nesting depth.
//!
//! Environments outside `\[ … \]` are common in mathematical prose
//! (`KaTeX` renders them directly). An open `\begin` with no close
//! turns the rest of the document into math in the author's mental
//! model; pulldown-cmark parses it as prose and the document
//! renders badly.
//!
//! Companion rule [`super::math_unbalanced_delim::MathUnbalancedDelim`]
//! covers primitive delimiter imbalance.

use crate::diagnostic::Diagnostic;
use crate::rule::LintRule;
use mdwright_document::Document;
use mdwright_document::MathError;

pub struct MathUnbalancedEnv;

impl LintRule for MathUnbalancedEnv {
    fn name(&self) -> &str {
        "math/unbalanced-env"
    }

    fn description(&self) -> &str {
        "LaTeX `\\begin{env}` with no matching `\\end{env}` at the same nesting depth."
    }

    fn explain(&self) -> &str {
        include_str!("explain/math__unbalanced_env.md")
    }

    fn check(&self, doc: &Document, out: &mut Vec<Diagnostic>) {
        for err in doc.math_errors() {
            let MathError::UnbalancedEnv { name, range } = err else {
                continue;
            };
            let message = format!(
                "unbalanced `\\begin{{{name}}}` — no matching `\\end{{{name}}}` before end of document or next code/HTML block"
            );
            if let Some(d) = Diagnostic::at(doc, 0, range.clone(), message, None) {
                out.push(d);
            }
        }
    }
}