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

Code before:
        }
    }

    if changed {
        formatted.push_str(&source[last_match.to_usize()..]);
        MarkdownResult::Formatted(formatted)
    } else {
        MarkdownResult::Unchanged
    }
}

fn format_pycon_block(
    source: &str,
    path: Option<&Path>,
    settings: &FormatterSettings,
) -> Option<String> {
    static FIRST_LINE: &str = ">>> ";
    static CONTINUATION: &str = "... ";

    let offset = FIRST_LINE.text_len();
    let mut changed = false;
    let mut result = String::with_capacity(source.len());
    let mut unformatted = String::with_capacity(source.len());
    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.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() {

Review comment (suggestion):
We might actually want to skip this `with_capacity` call since `String::new()` won't allocate at all in the case that nothing changes and we're able to return `None`.

Code after:
                }
                break;
            }
        }
    }

    if changed {
        formatted.push_str(&source[last_match.to_usize()..]);
        MarkdownResult::Formatted(formatted)
    } else {
        MarkdownResult::Unchanged
    }
}

fn format_pycon_block(
    source: &str,
    path: Option<&Path>,
    settings: &FormatterSettings,
) -> Option<String> {
    static FIRST_LINE: &str = ">>> ";
    static CONTINUATION: &str = "... ";
    static CONTINUATION_BLANK: &str = "...";

    let offset = FIRST_LINE.text_len();
    let mut changed = false;
    let mut result = String::with_capacity(source.len());
    let mut unformatted = String::with_capacity(source.len());
    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())