Skip to content

Fix reflow issues#10

Merged
leynos merged 1 commit intomainfrom
codex/fix-build-errors-in-mdtablefix
Jun 13, 2025
Merged

Fix reflow issues#10
leynos merged 1 commit intomainfrom
codex/fix-build-errors-in-mdtablefix

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jun 13, 2025

Summary

  • fix escaping logic in split_cells
  • pad header rows to match column widths
  • handle inconsistent rows when \| | splits lines

Testing

  • cargo clippy -- -D warnings
  • cargo test

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

Summary by Sourcery

Fix table reflow to properly process escaped pipes, maintain consistent column widths with padding for headers and separators, and preserve original indentation

Bug Fixes:

  • Correct split_cells to break cells on escaped pipe characters instead of consuming them
  • Improve inconsistent‐row detection to skip bail‐out when extra splits result from escaped pipes

Enhancements:

  • Pad all rows including the separator to the computed column widths with proper alignment markers
  • Preserve and reapply original line indentation to the reflowed output

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of escaped pipe characters in tables, ensuring correct cell splitting.
    • Enhanced detection and handling of inconsistent column counts within tables.
    • Separator lines in tables now correctly align with column widths and preserve alignment markers.
    • Output formatting now maintains original indentation for better readability.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jun 13, 2025

Reviewer's Guide

Refactored split_cells to correctly handle escaped pipes; refined early exit for inconsistent rows using a split_within_line flag; deferred padding to the output stage to align data and header separator rows to computed column widths; and preserved original indentation in reformatted table lines.

Sequence Diagram: split_cells Escaped Pipe \\| Handling

sequenceDiagram
    participant fn as "split_cells function"
    participant chars as "Input line character iterator"
    participant current_cell as "current_cell: String"
    participant cells as "cells: Vec<String>"

    fn->>chars: Consumes '\' (backslash character)
    fn->>chars: Peeks next character
    alt Encountered escaped pipe ("\\|")
        fn->>chars: Confirms next char is '|', consumes it
        fn->>cells: push(current_cell.trim().to_string())
        fn->>current_cell: clear()
        fn->>fn: Continue to next char in input line
    else Other characters or non-pipe escape sequences
        fn->>fn: Handle other cases (e.g., add char to current_cell)
    end
Loading

Sequence Diagram: reflow_table Separator Line Formatting

sequenceDiagram
    participant R as "reflow_table function"
    participant SC as "split_cells()"
    participant widths as "column_widths: Vec<usize>"

    R->>R: Check if sep_line (header separator) exists
    alt sep_line exists
        R->>SC: split_cells(sep_line_content)
        SC-->>R: sep_line_cells: Vec<String>
        R->>R: Pad sep_line_cells with empty strings to match column_widths.len()
        R->>R: For each cell in sep_line_cells (indexed by i)
        activate R
            R->>widths: Get widths[i] (target width for column i)
            R->>R: Format cell: create string of '-' (or ':' for alignment) matching widths[i]
            R-->>R: formatted_separator_cell
        deactivate R
        R->>R: Collect into padded_sep_cells: Vec<String>
        R->>R: Construct final_sep_line (using indent, "|", and joining padded_sep_cells with " | ")
        R->>R: Integrate final_sep_line with main table output_data
    else No sep_line
        R->>R: Proceed with formatting table without a custom separator line
    end
Loading

File-Level Changes

Change Details Files
Improve escaped pipe handling in split_cells
  • Removed pushing literal '
' for | escapes
  • On |, push current cell to vector and clear buffer
  • Adjusted peek logic to split cells on escaped pipes
  • Refine inconsistent row detection in reflow_table
    • Introduced split_within_line flag to detect inline splits
    • Replaced old row-length mismatch guard with new condition using a HashSet
    • Only early-return original lines when not splitting within line
    src/lib.rs
    Defer cell padding and preserve indentation in output
    • Removed row padding during cleaning
    • Pad cells to computed widths during formatting
    • Pad separator cells to widths and align colon markers
    • Prepend original indent to all output and separator lines
    src/lib.rs

    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 Jun 13, 2025

    Walkthrough

    The changes revise the internal logic of the split_cells and reflow_table functions to alter how escaped pipe characters are handled, improve detection of inconsistent row lengths, preserve indentation in output, and enhance the formatting of separator lines in markdown tables. No public API changes were made.

    Changes

    File(s) Change Summary
    src/lib.rs Modified split_cells to treat escaped pipes as delimiters; updated reflow_table for improved row consistency checks, indentation preservation, and enhanced separator line formatting.

    Sequence Diagram(s)

    sequenceDiagram
        participant User
        participant reflow_table
        participant split_cells
    
        User->>reflow_table: Call with markdown table input
        reflow_table->>split_cells: Parse each row for cells
        split_cells-->>reflow_table: Return split cells (escaped pipes as delimiters)
        reflow_table->>reflow_table: Check for split_within_line and row consistency
        reflow_table->>reflow_table: Format rows with indentation and separator alignment
        reflow_table-->>User: Return formatted table output
    
    Loading

    Possibly related PRs

    Poem

    In the warren of code, a table anew,
    Escaped pipes now split, as markdown bunnies do.
    Indents are cherished, alignments precise,
    Separator lines trimmed to look nice.
    With every hop, the tables align—
    Markdown made neat, by design!
    🐇✨

    ✨ Finishing Touches
    • 📝 Generate Docstrings

    🪧 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 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 - here's some feedback:

    • Consider extracting the separator-line formatting into its own helper to reduce code duplication and improve readability.
    • Simplify the inconsistent row-length check by comparing each row’s length to the first row instead of building a HashSet of all lengths.
    • Add a clarifying comment or rename variables around the split_cells change so it’s obvious that \| now acts as a cell separator rather than producing a literal |.
    Prompt for AI Agents
    Please address the comments from this code review:
    ## Overall Comments
    - Consider extracting the separator-line formatting into its own helper to reduce code duplication and improve readability.
    - Simplify the inconsistent row-length check by comparing each row’s length to the first row instead of building a HashSet of all lengths.
    - Add a clarifying comment or rename variables around the `split_cells` change so it’s obvious that `\|` now acts as a cell separator rather than producing a literal `|`.

    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.

    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: 1

    🧹 Nitpick comments (1)
    src/lib.rs (1)

    134-143: Improved handling of inconsistent row lengths

    The refined logic correctly allows processing to continue when splits occur within lines, even if row lengths appear inconsistent. This addresses the PR objective of handling tables with internal splits.

    For improved readability, consider extracting the distinct length check:

    +    let distinct_row_lengths = cleaned
    +        .iter()
    +        .map(Vec::len)
    +        .collect::<std::collections::HashSet<_>>()
    +        .len();
    +
         if !split_within_line
    -        && cleaned
    -            .iter()
    -            .map(Vec::len)
    -            .collect::<std::collections::HashSet<_>>()
    -            .len()
    -            > 1
    +        && distinct_row_lengths > 1
         {
             return lines.to_vec();
         }
    📜 Review details

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

    📥 Commits

    Reviewing files that changed from the base of the PR and between 014e1e2 and ba2b752.

    📒 Files selected for processing (1)
    • src/lib.rs (3 hunks)
    ⏰ Context from checks skipped due to timeout of 90000ms (2)
    • GitHub Check: coverage
    • GitHub Check: build-test
    🔇 Additional comments (3)
    src/lib.rs (3)

    98-98: Good addition for detecting split patterns within lines

    The split_within_line boolean correctly identifies when the sentinel regex splits content within lines rather than between lines, which helps handle malformed tables appropriately.


    152-162: Excellent: Indentation preservation maintains document structure

    The addition of indentation preservation ensures that reflowed tables maintain their position within the document hierarchy, which is particularly important for nested content or consistently formatted documents.


    165-194: Well-implemented separator line handling with alignment preservation

    The separator line processing correctly:

    • Pads to match the column count
    • Preserves alignment markers (:) for left/right/centre alignment
    • Ensures minimum separator width of 3 characters (markdown standard)
    • Handles edge cases when no data rows exist

    This implementation successfully addresses the PR objective of ensuring header rows are padded to match column widths.

    Comment thread src/lib.rs
    Comment on lines 35 to 42
    if ch == '\\' {
    if let Some(&next) = chars.peek() {
    if next == '|' {
    current.push('|');
    chars.next();
    cells.push(current.trim().to_string());
    current.clear();
    continue;
    }
    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

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

    ⚠️ Potential issue

    Critical: Incorrect handling of escaped pipe characters

    The change to the escape handling logic is incorrect. When \| is encountered in a markdown table cell, it represents a literal pipe character that should be included in the cell content, not a cell delimiter. The current implementation incorrectly treats \| as a cell boundary.

    Apply this diff to fix the escape handling:

                 if let Some(&next) = chars.peek() {
                     if next == '|' {
                         chars.next();
    -                    cells.push(current.trim().to_string());
    -                    current.clear();
    +                    current.push('|');
                         continue;
                     }
                 }

    This ensures that \| is correctly interpreted as a literal pipe character within the cell content.

    📝 Committable suggestion

    ‼️ IMPORTANT
    Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    Suggested change
    if ch == '\\' {
    if let Some(&next) = chars.peek() {
    if next == '|' {
    current.push('|');
    chars.next();
    cells.push(current.trim().to_string());
    current.clear();
    continue;
    }
    if ch == '\\' {
    if let Some(&next) = chars.peek() {
    if next == '|' {
    chars.next();
    current.push('|');
    continue;
    }
    }
    }
    🤖 Prompt for AI Agents
    In src/lib.rs around lines 35 to 42, the current code incorrectly treats the
    escaped pipe sequence `\|` as a cell delimiter instead of a literal pipe
    character. To fix this, modify the escape handling logic so that when `\|` is
    encountered, it appends a literal pipe character to the current cell content
    rather than splitting the cell. This involves consuming the escape character and
    the pipe, then adding a pipe character to the current string instead of pushing
    and clearing it.
    

    @leynos leynos merged commit edaaf61 into main Jun 13, 2025
    3 checks passed
    @leynos leynos deleted the codex/fix-build-errors-in-mdtablefix branch June 13, 2025 21:43
    @coderabbitai coderabbitai Bot mentioned this pull request Jun 13, 2025
    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