Skip to main content

truncated_source_window

Function truncated_source_window 

Source
pub fn truncated_source_window(
    input: &str,
    byte_span: Span,
    context: usize,
) -> (String, Span)
Expand description

Truncates a source string to a bounded window around an error span.

Takes context bytes on each side of the error location. Returns the truncated source and an adjusted span that is relative to the truncated window. This prevents unbounded memory usage when error diagnostics embed large source files.

Slicing is safe on multi-byte UTF-8: window boundaries are adjusted to char boundaries.

For multi-line inputs the window expands to the nearest line boundaries so that line numbers in the diagnostic output are consistent. For single-line (minified) inputs with a large requested context, the window is clamped to a smaller focused snippet since a wall of unbroken text is not helpful.

Use with Span::try_from_row_column when you only have (row, col) from the parser, or directly when you already have the byte offset from the parser.