Repository: astral-sh/ruff
File: crates/ruff_markdown/src/lib.rs (Rust)

Code before:
        unformatted.clear();
        if line.starts_with(FIRST_LINE) {
            let start = line.start();
            let mut end = line.full_end();
            unformatted.push_str(&source[TextRange::new(line.start() + offset, line.full_end())]);
            while let Some(next_line) = lines.peek() {
                if next_line.starts_with(CONTINUATION) {
                    end = next_line.full_end();
                    unformatted.push_str(&source[TextRange::new(next_line.start() + offset, end)]);
                    lines.next();
                } else {
                    break;
                }
            }
            let options = settings.to_format_options(PySourceType::Python, &unformatted, path);
            let Ok(formatted) =
                format_module_source(&unformatted, options).map(ruff_formatter::Printed::into_code)
            else {
                continue;
            };

            if formatted.len() != unformatted.len() || formatted != unformatted {
                result.push_str(&source[TextRange::new(last_match, start)]);
                for (idx, line) in formatted.universal_newlines().enumerate() {
                    result.push_str(if idx == 0 { FIRST_LINE } else { CONTINUATION });
                    result.push_str(&formatted[TextRange::new(line.start(), line.full_end())]);
                }
                last_match = end;
                changed = true;
            }
        }
    }

    if changed {
        result.push_str(&source[last_match.to_usize()..]);
        Some(result)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_snapshot;
    use ruff_linter::settings::types::{ExtensionMapping, ExtensionPair, Language};
    use ruff_workspace::FormatterSettings;

    use crate::{MarkdownResult, format_code_blocks};

    impl std::fmt::Display for MarkdownResult {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

Review comment (suggestion):
I think this is just `Line::full_range`:


```suggestion
                    result.push_str(&formatted[line.full_range()]);
```

Code after:
    let mut last_match = TextSize::new(0);
    let mut lines = source.universal_newlines().peekable();

    while let Some(line) = lines.next() {
        unformatted.clear();
        if line.starts_with(FIRST_LINE) {
            let start = line.start();
            let mut end = line.full_end();
            unformatted.push_str(&source[TextRange::new(line.start() + offset, line.full_end())]);
            while let Some(next_line) = lines.next_if(|line| line.starts_with(CONTINUATION_BLANK)) {
                end = next_line.full_end();
                let start = if next_line.trim_end() == CONTINUATION_BLANK {
                    next_line.end()
                } else {
                    next_line.start() + offset
                };
                unformatted.push_str(&source[TextRange::new(start, end)]);
            }
            let options = settings.to_format_options(PySourceType::Python, &unformatted, path);
            // Using `Printed::into_code` requires adding `ruff_formatter` as a direct
            // dependency, and I suspect that Rust can optimize the closure away regardless.
            #[expect(clippy::redundant_closure_for_method_calls)]
            let Ok(formatted) =
                format_module_source(&unformatted, options).map(|formatted| formatted.into_code())
            else {
                continue;
            };

            if formatted.len() != unformatted.len() || formatted != unformatted {
                result.push_str(&source[TextRange::new(last_match, start)]);
                for (idx, line) in formatted.universal_newlines().enumerate() {
                    result.push_str(if idx == 0 {
                        FIRST_LINE
                    } else if line.is_empty() {
                        CONTINUATION_BLANK
                    } else {
                        CONTINUATION
                    });
                    result.push_str(&formatted[line.full_range()]);
                }
                last_match = end;
                changed = true;
            }
        }
    }

    if changed {
        result.push_str(&source[last_match.to_usize()..]);
        Some(result)
    } else {
        None