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

Code before:
    path: Option<&Path>,
    settings: &FormatterSettings,
) -> MarkdownResult {
    let mut state = MarkdownState::On;
    let mut changed = false;
    let mut formatted = 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() {
        // Toggle code block formatting off/on
        if let Some(capture) = OFF_ON_DIRECTIVES.captures(&line) {
            let (_, [action]) = capture.extract();
            state = match action {
                "off" => MarkdownState::Off,
                "on" => MarkdownState::On,
                _ => state,
            };
        // Process code blocks
        } else if let Some(opening_capture) = MARKDOWN_CODE_FENCE.captures(&line) {
            let (_, [code_indent, opening_fence, language, _info]) = opening_capture.extract();
            let start = lines.peek().map(Line::start).unwrap_or_default();

            // Consume lines until reaching the matching/ending code fence
            for code_line in lines.by_ref() {
                if let Some(closing_capture) = MARKDOWN_CODE_FENCE.captures(&code_line) {
                    let (_, [_, closing_fence, _, _]) = closing_capture.extract();
                    // Found the end of the code block
                    if closing_fence == opening_fence {
                        let language = language.to_ascii_lowercase();
                        if state == MarkdownState::On
                            && matches!(
                                language.as_str(),
                                "python" | "py" | "python3" | "py3" | "pyi" | ""
                            )
                        {
                            // Maybe python, try formatting it
                            let end = code_line.start();
                            let unformatted_code = dedent(&source[TextRange::new(start, end)]);

                            let py_source_type = PySourceType::from_extension(&language);
                            let options =
                                settings.to_format_options(py_source_type, &unformatted_code, 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 formatted_code = format_module_source(&unformatted_code, options)
                                .map(|formatted| formatted.into_code());

                            // Formatting produced changes

Review comment (suggestion):
You could maybe save some indentation by using a let-else here. Alternatively, if you're only looking for this final line, I think you could maybe do something like:

```rust
let Some(closing_capture) = lines.by_ref().find_map(|code_line| MARKDOWN_CODE_FENCE.captures(&code_line) else {
    continue
}
```

Code after:
    path: Option<&Path>,
    settings: &FormatterSettings,
) -> MarkdownResult {
    let mut state = MarkdownState::On;
    let mut changed = false;
    let mut formatted = 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() {
        // Toggle code block formatting off/on
        if let Some(capture) = OFF_ON_DIRECTIVES.captures(&line) {
            let (_, [action]) = capture.extract();
            state = match action {
                "off" => MarkdownState::Off,
                "on" => MarkdownState::On,
                _ => state,
            };
        // Process code blocks
        } else if let Some(opening_capture) = MARKDOWN_CODE_FENCE.captures(&line) {
            let (_, [code_indent, opening_fence, language, _info]) = opening_capture.extract();
            let start = lines.peek().map(Line::start).unwrap_or_default();

            // Consume lines until reaching the matching/ending code fence
            for code_line in lines.by_ref() {
                let Some((_, [_, closing_fence, _, _])) = MARKDOWN_CODE_FENCE
                    .captures(&code_line)
                    .map(|cap| cap.extract())
                else {
                    continue;
                };

                // Found the matching end of the code block
                if closing_fence == opening_fence {
                    let language = language.to_ascii_lowercase();
                    if state == MarkdownState::On
                        && matches!(
                            language.as_str(),
                            "python" | "py" | "python3" | "py3" | "pyi" | ""
                        )
                    {
                        // Maybe python, try formatting it
                        let end = code_line.start();
                        let unformatted_code = dedent(&source[TextRange::new(start, end)]);

                        let py_source_type = PySourceType::from_extension(&language);
                        let options =
                            settings.to_format_options(py_source_type, &unformatted_code, path);

                        // Using `Printed::into_code` requires adding `ruff_formatter` as a direct
                        // dependency, and I suspect that Rust can optimize the closure away regardless.