Repository: astral-sh/ruff
File: crates/ty_ide/src/docstring.rs (Rust)

Code before:
        // So now we're actually in the literal block
        if let Some(literal) = starting_literal
            && !line.is_empty()
        {
            starting_literal = None;
            in_literal = true;
            in_any_code = true;
            block_indent = line_indent;
            output.push('\n');
            output.push_str(FENCE);
            output.push_str(literal);
            output.push('\n');
        }

        // If we're not in a codeblock and we see something that signals a doctest, start one
        if !in_any_code && line.starts_with(">>>") {
            block_indent = line_indent;
            in_doctest = true;
            in_any_code = true;
            // TODO: is there something more specific? `pycon`?
            output.push_str(FENCE);
            output.push_str("python\n");
        }

        // If we're not in a codeblock and we see a markdown codefence, start one
        if !in_any_code && line.starts_with("```") {
            let without_leading_ticks = line.trim_start_matches('`');
            let num_ticks = line.len() - without_leading_ticks.len();
            let ticks = &line[..num_ticks];
            // If we don't see this amount of ticks again on the line, assume we're opening a markdown block
            // (We *don't* want to consider ```hello``` as a codefence, that's inline code!)
            if !without_leading_ticks.contains(ticks) {
                // Unlike other blocks we don't need to emit fences because it's already markdown
                block_indent = line_indent;
                in_any_code = true;
                in_markdown_with_ticks = num_ticks;
            }
        // If we're in a markdown code fence and this line seems to terminate it, end the block
        } else if in_markdown_with_ticks > 0
            && line.len() - line.trim_start_matches('`').len() >= in_markdown_with_ticks
        {
            in_any_code = false;
            block_indent = 0;
            in_markdown_with_ticks = 0;
        }

        // If we're not in a codeblock and we see something that signals a literal block, start one
        let parsed_lit = line
            // first check for a line ending with `::`
            .strip_suffix("::")
            .map(|prefix| (prefix, None))

Review comment (suggestion):
I think we should also add support for `~~~` ([spec](https://spec.commonmark.org/0.31.2/#example-120))

Code after:
        // So now we're actually in the literal block
        if let Some(literal) = starting_literal
            && !line.is_empty()
        {
            starting_literal = None;
            in_literal = true;
            in_any_code = true;
            block_indent = line_indent;
            output.push('\n');
            output.push_str(FENCE);
            output.push_str(literal);
            output.push('\n');
        }

        // If we're not in a codeblock and we see something that signals a doctest, start one
        if !in_any_code && line.starts_with(">>>") {
            block_indent = line_indent;
            in_doctest = true;
            in_any_code = true;
            // TODO: is there something more specific? `pycon`?
            output.push_str(FENCE);
            output.push_str("python\n");
        }

        // If we're not in a codeblock and we see a markdown codefence, start one
        let has_tick_fence = line.starts_with("```");
        let has_tilde_fence = line.starts_with("~~~");
        if !in_any_code && (has_tick_fence || has_tilde_fence) {
            let without_leading_fence = if has_tick_fence {
                line.trim_start_matches('`')
            } else {
                line.trim_start_matches('~')
            };
            let fence_len = line.len() - without_leading_fence.len();
            let fence = &line[..fence_len];
            // If we don't see this amount of ticks again on the line, assume we're opening a markdown block
            // (We *don't* want to consider ```hello``` as a codefence, that's inline code!)
            if !without_leading_fence.contains(fence) {
                // Unlike other blocks we don't need to emit fences because it's already markdown
                block_indent = line_indent;
                in_any_code = true;
                in_markdown_with_fence = Some(fence.to_owned());
            }
        // If we're in a markdown code fence and this line seems to terminate it, end the block
        } else if let Some(fence) = &in_markdown_with_fence
            && line.starts_with(fence)
        {
            in_any_code = false;
            block_indent = 0;
            in_markdown_with_fence = None;
        }