Skip to content
Open
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
43 changes: 43 additions & 0 deletions harper-core/src/linting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,49 @@ where
}
}

pub mod debug {
use crate::Token;

/// Formats a lint match with surrounding context for debug output.
///
/// The function takes the same `matched_tokens` and `source`, and `context` parameters
/// passed to `[match_to_lint_with_context]`.
///
/// # Arguments
/// * `log` - `matched_tokens`
/// * `ctx` - `context`, or `None` if calling from `[match_to_lint]`
/// * `src` - `source` from `[match_to_lint]` / `[match_to_lint_with_context]`
///
/// # Returns
/// A string with ANSI escape codes where:
/// - Context tokens are dimmed before and after the matched tokens in normal weight.
/// - Markup and formatting text hidden in whitespace tokens is filtered out.
pub fn format_lint_match(
log: &[Token],
ctx: Option<(&[Token], &[Token])>,
src: &[char],
) -> String {
let fmt = |tokens: &[Token]| {
tokens
.iter()
.filter(|t| !t.kind.is_unlintable())
.map(|t| t.span.get_content_string(src))
.collect::<String>()
};

if let Some((pro, epi)) = ctx {
format!(
"\x1b[2m{}\x1b[0m{}\x1b[2m{}\x1b[0m",
fmt(pro),
fmt(log),
fmt(epi)
)
} else {
fmt(log)
}
}
}

#[cfg(test)]
pub mod tests {
use crate::parsers::Markdown;
Expand Down
Loading