Skip to content

Reset list numbers after breaks and headings#106

Merged
leynos merged 2 commits intomainfrom
codex/add-integration-tests-for-list-numbering
Jul 20, 2025
Merged

Reset list numbers after breaks and headings#106
leynos merged 2 commits intomainfrom
codex/add-integration-tests-for-list-numbering

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jul 20, 2025

Summary

  • reset counters when encountering headings and thematic breaks
  • cover the new behaviour in list integration tests

Testing

  • make fmt
  • make lint
  • make test

https://chatgpt.com/codex/tasks/task_e_687cb8e8fe8483229002165161799248

Summary by Sourcery

Reset list numbering to restart after Markdown headings and thematic breaks and cover the new behavior with integration tests

New Features:

  • Restart ordered list counters when encountering top-level headings
  • Restart ordered list counters when encountering thematic breaks

Enhancements:

  • Expose THEMATIC_BREAK_RE as pub(crate) and add expect messages for regex initialization

Documentation:

  • Update README to document restarting numbering after headings and thematic breaks

Tests:

  • Add list renumbering integration tests for restarts after headings, breaks, and their combination

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jul 20, 2025

Reviewer's Guide

Enhances the list renumbering logic to reset counters on encountering Markdown headings or thematic breaks, improves regex initialization for safety, extends integration tests and data fixtures for the new behavior, and updates the README to reflect these changes.

Class diagram for regex and renumbering logic changes

classDiagram
    class renumber_lists {
        +Vec<String> renumber_lists(lines: &[String])
        -counters: Vec<usize>
        -prev_blank: bool
    }
    class HEADING_RE {
        +Regex
    }
    class THEMATIC_BREAK_RE {
        +Regex
    }
    renumber_lists --> HEADING_RE : uses
    renumber_lists --> THEMATIC_BREAK_RE : uses
Loading

Flow diagram for list renumbering with heading and break resets

flowchart TD
    A[Start renumbering lists] --> B{Line is heading or thematic break?}
    B -- Yes --> C[Clear list counters]
    C --> D[Output line as-is]
    D --> E[Continue to next line]
    B -- No --> F[Handle paragraph restart and indentation]
    F --> G[Output line]
    G --> E
    E --> B
Loading

File-Level Changes

Change Details Files
Reset list numbering counters upon encountering ATX headings or thematic breaks
  • Introduce HEADING_RE regex to detect ATX headings
  • Import THEMATIC_BREAK_RE from breaks module
  • Add logic in renumber_lists to clear counters and continue on heading or break
src/lists.rs
Harden regex initializations for numbered lists and thematic breaks
  • Use expect instead of unwrap when constructing NUMBERED_RE
  • Make THEMATIC_BREAK_RE pub(crate) and use expect on its regex initialization
src/lists.rs
src/breaks.rs
Extend and rename integration tests for list renumbering scenarios
  • Rename existing test to restart_after_top_heading
  • Add test cases for restart_after_break, restart_after_heading, and combined scenarios in test_renumber_cases
  • Add corresponding input/output data files under tests/data
tests/lists.rs
tests/data/renumber_break_restart_input.txt
tests/data/renumber_break_restart_expected.txt
tests/data/renumber_heading_restart_input.txt
tests/data/renumber_heading_restart_expected.txt
tests/data/renumber_break_heading_restart_input.txt
tests/data/renumber_break_heading_restart_expected.txt
Update README to mention resetting numbering after breaks and headings
  • Extend description sentence to include thematic breaks and headings
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 20, 2025

Summary by CodeRabbit

  • New Features

    • Ordered list numbering now automatically restarts after a heading or thematic break in Markdown documents.
  • Bug Fixes

    • Improved handling of list numbering to ensure correct behaviour after interruptions by headings or thematic breaks.
  • Documentation

    • Updated documentation to clarify that list numbering restarts after headings and thematic breaks.
  • Tests

    • Added new test cases to verify correct list renumbering behaviour across headings and thematic breaks.

Walkthrough

Introduce logic in the renumber_lists function to reset list numbering when encountering Markdown headings or thematic breaks. Add two new regular expressions to detect these elements. Expand the test suite with new input and expected output files, and update or add tests to verify correct list renumbering behaviour across headings and breaks.

Changes

File(s) Change Summary
src/lists.rs Add regexes for headings and thematic breaks; update renumber_lists to reset counters on matches.
src/breaks.rs Make THEMATIC_BREAK_RE crate-visible; update regex init to use expect instead of unwrap.
tests/lists.rs Rename and update test for heading resets; add new cases for breaks and heading restarts.
tests/data/renumber_break_restart_input.txt
tests/data/renumber_break_restart_expected.txt
tests/data/renumber_heading_restart_input.txt
tests/data/renumber_heading_restart_expected.txt
tests/data/renumber_break_heading_restart_input.txt
tests/data/renumber_break_heading_restart_expected.txt
Add new test input and expected output files covering list renumbering after headings and breaks.
README.md Update --renumber option docs to clarify numbering restarts after headings and thematic breaks.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant renumber_lists
    participant HeadingOrBreakDetector

    User->>renumber_lists: Process markdown lines
    loop For each line
        renumber_lists->>HeadingOrBreakDetector: Check if line is heading or break
        alt Is heading or break
            HeadingOrBreakDetector-->>renumber_lists: true
            renumber_lists->>renumber_lists: Clear list counters, output line unchanged
        else Not heading or break
            HeadingOrBreakDetector-->>renumber_lists: false
            renumber_lists->>renumber_lists: Continue normal list renumbering logic
        end
    end
Loading

Possibly related PRs

Poem

When headings and breaks appear in sight,
The lists now pause and reset their might.
No more numbers running astray—
Fresh starts bloom after lines of grey.
Markdown’s order, crisp and neat,
Marches on with renumbered beat!
#️⃣✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cb8d3e0 and 2cdca03.

📒 Files selected for processing (3)
  • README.md (1 hunks)
  • src/breaks.rs (1 hunks)
  • src/lists.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.md

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • AGENTS.md

⚙️ CodeRabbit Configuration File

**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • AGENTS.md

⚙️ CodeRabbit Configuration File

🧬 Code Graph Analysis (1)
src/lists.rs (1)
src/wrap.rs (1)
  • is_fence (240-240)
🔇 Additional comments (5)
src/breaks.rs (1)

11-14: LGTM! Visibility and error handling improvements align with coding guidelines.

The change from private to pub(crate) visibility enables proper cross-module usage, and replacing .unwrap() with .expect() provides better error context as required by the coding guidelines.

src/lists.rs (3)

5-5: Import statement correctly enables cross-module usage.

The import of THEMATIC_BREAK_RE from the breaks module aligns with the visibility change made in src/breaks.rs.


17-19: Error handling improvement follows coding guidelines.

Replacing .unwrap() with .expect() provides better error context and aligns with the coding guidelines requirement to prefer .expect() over .unwrap().


123-128: Confirm heading and thematic break regex function correctly

Tests demonstrate HEADING_RE matches basic headings, indented headings (up to 3 spaces), and empty headings, while rejecting over-indented lines. THEMATIC_BREAK_RE matches asterisk (***), dash (---) and underscore (___) breaks as intended. No changes required.

• File src/lists.rs (lines 123–128): HEADING_RE and THEMATIC_BREAK_RE patterns validated.

README.md (1)

41-41: Documentation accurately reflects the enhanced list renumbering behaviour.

The addition of "a thematic break, or a heading" correctly documents the new functionality implemented in the code changes.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/add-integration-tests-for-list-numbering

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @leynos - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/lists.rs:12` </location>
<code_context>

+// Lines starting with optional indentation followed by '#' characters denote
+// Markdown ATX headings. A space or end of line must follow the hashes.
+static HEADING_RE: std::sync::LazyLock<Regex> =
+    std::sync::LazyLock::new(|| Regex::new(r"^[ ]{0,3}#{1,6}(?:\s|$)").unwrap());
+
+// Matches Markdown thematic breaks using '*', '-' or '_' characters.
</code_context>

<issue_to_address>
Consider using a more precise regex for ATX headings.

The current regex also matches headings without a space after the hashes (e.g., '###foo'), which is invalid in CommonMark. Update the regex to require at least one space after the hashes, unless the heading is empty. For example: r"^[ ]{0,3}#{1,6}(?:[ \t]+|$)".
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
static HEADING_RE: std::sync::LazyLock<Regex> =
    std::sync::LazyLock::new(|| Regex::new(r"^[ ]{0,3}#{1,6}(?:\s|$)").unwrap());
=======
static HEADING_RE: std::sync::LazyLock<Regex> =
    std::sync::LazyLock::new(|| Regex::new(r"^[ ]{0,3}#{1,6}(?:[ \t]+|$)").unwrap());
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/lists.rs Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2e3395a and cb8d3e0.

📒 Files selected for processing (8)
  • src/lists.rs (2 hunks)
  • tests/data/renumber_break_heading_restart_expected.txt (1 hunks)
  • tests/data/renumber_break_heading_restart_input.txt (1 hunks)
  • tests/data/renumber_break_restart_expected.txt (1 hunks)
  • tests/data/renumber_break_restart_input.txt (1 hunks)
  • tests/data/renumber_heading_restart_expected.txt (1 hunks)
  • tests/data/renumber_heading_restart_input.txt (1 hunks)
  • tests/lists.rs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

Instructions used from:

Sources:
📄 CodeRabbit Inference Engine

  • AGENTS.md

⚙️ CodeRabbit Configuration File

🔇 Additional comments (9)
tests/data/renumber_break_restart_input.txt (1)

1-8: Test data structure is correct for break restart scenario.

The input properly demonstrates a numbered list continuing after a thematic break, which will validate the reset behaviour.

tests/data/renumber_break_restart_expected.txt (1)

1-8: Expected output correctly demonstrates list reset after thematic break.

The numbering properly resets to "1. Next" after the break, validating the intended behaviour.

tests/data/renumber_heading_restart_input.txt (1)

1-8: Test data appropriately structured for heading restart validation.

The input demonstrates a numbered list continuing after a heading, providing proper test coverage for the reset behaviour.

tests/data/renumber_heading_restart_expected.txt (1)

1-8: Expected output correctly shows list reset after heading.

The numbering appropriately resets to "1. Next" following the heading, confirming the desired behaviour.

src/lists.rs (1)

126-131: Logic correctly implements counter reset behaviour.

The conditional properly detects headings and thematic breaks, clears counters to reset numbering state, and maintains correct flow control.

tests/data/renumber_break_heading_restart_input.txt (1)

1-10: LGTM: Test input correctly demonstrates the scenario.

The test input properly sets up a numbered list interrupted by both a thematic break and heading, with the continuation item incorrectly numbered as "3" to test the renumbering functionality.

tests/data/renumber_break_heading_restart_expected.txt (1)

1-10: LGTM: Expected output correctly demonstrates list restart behaviour.

The expected output properly shows the list restarting at "1. Next" after the thematic break and heading, which aligns with the PR objectives.

tests/lists.rs (2)

45-49: LGTM: Function rename improves clarity and expected output is correct.

The function name restart_after_top_heading is more descriptive than the previous name, and the updated expected output correctly demonstrates list restart behaviour after a heading.


115-126: LGTM: New test cases provide comprehensive coverage.

The new parameterised test cases properly cover the three scenarios: restart after break, restart after heading, and restart after both break and heading. The use of external test data files follows the established pattern and maintains good separation of test data from test logic.

Comment thread src/lists.rs Outdated
Comment thread src/lists.rs Outdated
@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Jul 20, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 20, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@leynos
Copy link
Copy Markdown
Owner Author

leynos commented Jul 20, 2025

@sourcery-ai review

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @leynos - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@leynos leynos merged commit 1b41d7f into main Jul 20, 2025
2 checks passed
@leynos leynos deleted the codex/add-integration-tests-for-list-numbering branch July 20, 2025 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant