Skip to content

Ensure uniform column widths#7

Merged
leynos merged 3 commits intomainfrom
codex/uniformly-pad-table-columns
Jun 13, 2025
Merged

Ensure uniform column widths#7
leynos merged 3 commits intomainfrom
codex/uniformly-pad-table-columns

Conversation

@leynos
Copy link
Copy Markdown
Owner

@leynos leynos commented Jun 13, 2025

Summary

  • pad table columns to equal width
  • test uniform width behaviour on sample tables
  • expose split_cells for doctests
  • fix doctest snippets

Testing

  • cargo clippy -- -D warnings
  • cargo test

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

Summary by Sourcery

Pad Markdown table columns to uniform width in reflow_table, expose split_cells publicly, update doctest imports, and add integration tests to verify uniform column widths.

New Features:

  • Pad table columns to equal width in reflow_table
  • Expose split_cells publicly for external use and doctests

Enhancements:

  • Refactor row processing in reflow_table to separate cleaning and width calculation

Documentation:

  • Update doctest snippets to import split_cells, reflow_table, process_stream, and rewrite functions

Tests:

  • Add integration tests to verify uniform column widths for sample tables

Summary by CodeRabbit

  • New Features
    • Improved table formatting with column-wise alignment, ensuring cells are padded to uniform widths for better readability.
  • Documentation
    • Updated code examples to clarify usage and imports for public functions.
  • Tests
    • Added integration tests to verify consistent column alignment in reflowed Markdown tables and preservation of non-table content.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 13, 2025

Warning

Rate limit exceeded

@leynos has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 4 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between ba45a3e and 954fb00.

📒 Files selected for processing (2)
  • src/lib.rs (5 hunks)
  • tests/integration.rs (2 hunks)

Walkthrough

The changes make the split_cells function public and update code examples for clarity, explicitly showing imports. The core logic of reflow_table is revised to align table columns by padding cells to uniform widths. Three new integration tests verify that reflowed tables have consistent column alignment and that non-table lines remain unchanged.

Changes

File(s) Change Summary
src/lib.rs Made split_cells public; updated examples for explicit imports; revised reflow_table to align columns by padding cells to uniform widths; updated examples for process_stream and rewrite.
tests/integration.rs Added three integration tests verifying uniform column widths in reflowed tables and preservation of non-table lines after processing.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant reflow_table
    participant split_cells

    User->>reflow_table: Provide table lines
    reflow_table->>split_cells: Split each line into cells
    reflow_table->>reflow_table: Calculate max width per column
    reflow_table->>reflow_table: Pad each cell to column width
    reflow_table-->>User: Return aligned table lines
Loading

Possibly related PRs

  • Implement markdown table fixer #1: Introduces the initial implementation of reflow_table, process_stream, and rewrite; the current PR refines and builds directly on this foundation.
  • Implement markdown table fixer #2: Implements the initial reflow_table logic; the current PR enhances this with column-wise alignment and public exposure of split_cells.

Poem

Tables now stand in tidy rows,
Aligned in columns, neat as prose.
With split_cells public, all can see,
How Markdown’s order came to be.
Tests ensure the lines conform—
A rabbit’s work, precise and warm!
🐇✨

✨ 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.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Jun 13, 2025

Reviewer's Guide

This PR refactors table handling to enforce uniform column widths by computing and applying padding, makes key utilities public for doctests, updates example snippets, and adds integration tests for width consistency.

Updated Class Diagram for mdtablefix Module Public API

classDiagram
  class mdtablefix {
    +split_cells(line: &str) : Vec~String~
    +reflow_table(lines: &[String]) : Vec~String~
    +process_stream(lines: &[String]) : Vec~String~
    +rewrite(path: &Path) : Result~void, std::io::Error~
  }
  <<Module>> mdtablefix
Loading

File-Level Changes

Change Details Files
Expose split_cells for external use
  • Changed split_cells signature from private to public
  • Added use mdtablefix::split_cells to its doctest
src/lib.rs
Refactor reflow_table to pad columns uniformly
  • Replaced chained iterator map with explicit cleaning loop
  • Computed maximum width per column across all rows
  • Applied left-justified formatting to each cell based on computed widths
src/lib.rs
Correct and extend doctest examples
  • Added use statements for reflow_table, process_stream, and rewrite
  • Updated examples to use String::from for input lines
  • Marked file-processing snippet with no_run attribute
src/lib.rs
Add integration tests for uniform width behavior
  • Introduced two tests verifying all rows have equal column widths
  • Asserted per-column length consistency across sample tables
tests/integration.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

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 merging the row-cleanup and width-calculation loops into one pass to reduce temporary allocations and simplify the logic.
  • Current padding uses String.len(), which can miscompute widths for multi-byte (Unicode) characters—consider using a unicode-width library for accurate alignment.
  • The two integration tests duplicate the same column-width assertion logic; extracting that into a shared helper would make them more concise and maintainable.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider merging the row-cleanup and width-calculation loops into one pass to reduce temporary allocations and simplify the logic.
- Current padding uses String.len(), which can miscompute widths for multi-byte (Unicode) characters—consider using a unicode-width library for accurate alignment.
- The two integration tests duplicate the same column-width assertion logic; extracting that into a shared helper would make them more concise and maintainable.

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

🔭 Outside diff range comments (1)
src/lib.rs (1)

86-95: ⚠️ Potential issue

max_cols calculation silently discards blank cells, shifting column indexes

Counting only non-empty cells (filter(|c| !c.is_empty())) means a legitimately empty column (e.g. | foo | | bar |) is ignored.
If any row contains an empty cell in the middle, the column indexes of everything to the right shift left, producing mis-aligned output or false “inconsistent column” rejections.

-    let max_cols = rows
-        .iter()
-        .map(|r| r.iter().filter(|c| !c.is_empty()).count())
+    // Count every cell, even if it is empty, to preserve column positions
+    let max_cols = rows
+        .iter()
+        .map(|r| r.len())
         .max()
         .unwrap_or(0);

The subsequent consistency check (count != 0 && count != max_cols) must use the same logic:

-        let count = r.iter().filter(|c| !c.is_empty()).count();
+        let count = r.len();
🧹 Nitpick comments (3)
tests/integration.rs (3)

123-125: Use the direct capture in format! to silence Clippy

-        writeln!(f, "{}", line).unwrap();
+        writeln!(f, "{line}").unwrap();

This removes the needless temporary string formatting and fixes the CI failure.

🧰 Tools
🪛 GitHub Actions: CI

[error] 124-124: Clippy error: variables can be used directly in the format! string. Suggested fix: change writeln!(f, "{}", line).unwrap(); to writeln!(f, "{line}").unwrap();.


147-151: Redundant closure – replace with str::len

-        .map(|c| c.len())
+        .map(str::len)

Applying this quiets Clippy and reads better.

🧰 Tools
🪛 GitHub Actions: CI

[error] 150-150: Clippy error: redundant closure detected. Suggested fix: replace .map(|c| c.len()) with .map(str::len).


171-175: Same optimisation applies here:

-        .map(|c| c.len())
+        .map(str::len)
🧰 Tools
🪛 GitHub Actions: CI

[error] 174-174: Clippy error: redundant closure detected. Suggested fix: replace .map(|c| c.len()) with .map(str::len).

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3d777b8 and 0704de7.

📒 Files selected for processing (2)
  • src/lib.rs (5 hunks)
  • tests/integration.rs (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/integration.rs (1)
src/lib.rs (1)
  • reflow_table (57-126)
🪛 GitHub Actions: CI
tests/integration.rs

[error] 124-124: Clippy error: variables can be used directly in the format! string. Suggested fix: change writeln!(f, "{}", line).unwrap(); to writeln!(f, "{line}").unwrap();.


[error] 150-150: Clippy error: redundant closure detected. Suggested fix: replace .map(|c| c.len()) with .map(str::len).


[error] 174-174: Clippy error: redundant closure detected. Suggested fix: replace .map(|c| c.len()) with .map(str::len).

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: coverage

Comment thread src/lib.rs
@leynos leynos force-pushed the codex/uniformly-pad-table-columns branch from ba45a3e to 954fb00 Compare June 13, 2025 20:57
@leynos leynos merged commit 959117f into main Jun 13, 2025
1 of 3 checks passed
@leynos leynos deleted the codex/uniformly-pad-table-columns branch June 13, 2025 21:03
This was referenced Jul 31, 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