Skip to content

Code blocks have an optional text-wrap toggle #2009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2025

Conversation

samejr
Copy link
Member

@samejr samejr commented May 1, 2025

User requested improvement: CodeBlock component has a button that toggles between "Text wrap" and "Text inline".

Notes:

  • It sets classes like whitespace-pre-wrap and break-word using isWrapped
  • This option doesn't display on the full modal view
  • It doesn't honor any code indentation when it wraps annoyingly

CleanShot 2025-05-01 at 13 45 40@2x

CleanShot 2025-05-01 at 13 50 13@2x

Summary by CodeRabbit

  • New Features

    • Added a toggle button to code blocks for enabling or disabling text wrapping, improving readability for long lines of code.
    • Introduced new inline and wrap icons for visual feedback when toggling text wrapping in code blocks.
  • Enhancements

    • Code blocks now support an optional text wrapping feature, which can be enabled by developers as needed.

Copy link

changeset-bot bot commented May 1, 2025

⚠️ No Changeset found

Latest commit: 3012de8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented May 1, 2025

Walkthrough

This update introduces two new SVG icon components, TextInlineIcon and TextWrapIcon, for use in the web application's UI. The CodeBlock component is enhanced with an optional text wrapping feature, controlled by a new showTextWrapping prop and an internal toggle state. The code block's rendering logic and styling are updated to support dynamic text wrapping, affecting both the main view and modal view. The PacketDisplay component is modified to enable the text wrapping toggle in its default code block display scenario.

Changes

File(s) Change Summary
.../assets/icons/TextInlineIcon.tsx, .../assets/icons/TextWrapIcon.tsx Added new React SVG icon components: TextInlineIcon and TextWrapIcon, each accepting an optional className.
.../components/code/CodeBlock.tsx Added optional showTextWrapping prop and internal state to toggle code wrapping; updated rendering and styling logic; extended HighlightCode subcomponent to support wrapping.
.../components/runs/v3/PacketDisplay.tsx Enabled the showTextWrapping prop on CodeBlock in the default display case.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CodeBlock
    participant HighlightCode

    User->>CodeBlock: Render with showTextWrapping prop
    CodeBlock->>User: Display code and toolbar
    User->>CodeBlock: Click wrap toggle button
    CodeBlock->>CodeBlock: Toggle isWrapped state
    CodeBlock->>HighlightCode: Render code with isWrapped prop
    HighlightCode->>User: Display code with/without wrapping
Loading

Poem

In the garden of code where the carrots grow bright,
Two new icons shimmer, SVGs in the light.
Now code blocks can wrap, like a hug from a friend,
No more scrolling sideways, let the long lines bend!
With a click and a hop, the text wraps in place—
A bunny’s delight in this code-friendly space. 🥕✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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
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: 0

🧹 Nitpick comments (2)
apps/webapp/app/components/code/CodeBlock.tsx (2)

340-340: Shared wrap state between main and modal views

The same isWrapped state is used for both the main view and modal view, but the toggle is only available in the main view. This seems intentional based on the PR objectives, but users might expect to be able to toggle wrapping in the modal view as well.

Consider either:

  1. Making the wrap state independent for the modal view
  2. Adding the toggle button to the modal view
  3. Adding a note in the PR description about this design decision

Also applies to: 395-395


486-486: Note about indentation preservation

As mentioned in the PR objectives, this implementation doesn't preserve code indentation when text wrapping is enabled. This is a known limitation rather than a bug.

If preserving indentation is important for code readability, consider exploring CSS solutions like white-space: pre-line combined with custom indentation preservation logic, though this would be a more complex enhancement.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between aacec56 and 3012de8.

📒 Files selected for processing (4)
  • apps/webapp/app/assets/icons/TextInlineIcon.tsx (1 hunks)
  • apps/webapp/app/assets/icons/TextWrapIcon.tsx (1 hunks)
  • apps/webapp/app/components/code/CodeBlock.tsx (13 hunks)
  • apps/webapp/app/components/runs/v3/PacketDisplay.tsx (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
apps/webapp/app/components/code/CodeBlock.tsx (3)
apps/webapp/app/components/primitives/Tooltip.tsx (4)
  • TooltipProvider (118-118)
  • Tooltip (118-118)
  • TooltipTrigger (118-118)
  • TooltipContent (118-118)
apps/webapp/app/assets/icons/TextInlineIcon.tsx (1)
  • TextInlineIcon (1-41)
apps/webapp/app/assets/icons/TextWrapIcon.tsx (1)
  • TextWrapIcon (1-34)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: units / 🧪 Unit Tests
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - npm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (11)
apps/webapp/app/assets/icons/TextInlineIcon.tsx (1)

1-41: Icon component looks well-implemented

The TextInlineIcon component is well-structured with proper SVG viewBox, stroke settings, and accessibility considerations. The component properly accepts an optional className prop for styling flexibility.

apps/webapp/app/assets/icons/TextWrapIcon.tsx (1)

1-34: Icon component follows best practices

The TextWrapIcon component is well-implemented with proper SVG attributes and styling. It maintains consistency with TextInlineIcon in terms of API and styling approach.

apps/webapp/app/components/runs/v3/PacketDisplay.tsx (1)

47-47: Good integration of the text wrapping feature

The addition of the showTextWrapping prop to the CodeBlock component in the default case is a clean way to enable the text wrapping functionality for JSON content.

apps/webapp/app/components/code/CodeBlock.tsx (8)

6-6: Icon imports properly organized

The imports for the new icon components are correctly positioned in the file.

Also applies to: 12-12


36-37: Good JSDoc for the new prop

The JSDoc comment for the showTextWrapping prop clearly describes its purpose.


191-191: State and prop setup looks good

The showTextWrapping prop with default value of false and the isWrapped state are properly initialized.

Also applies to: 211-211


273-291: Well-implemented toggle button with tooltip

The text wrapping toggle button is well-implemented with:

  • Conditional rendering based on showTextWrapping prop
  • Clean state toggling implementation
  • Appropriate tooltip that changes based on the current state
  • Proper use of icons to indicate the current/next state

340-340: Consistent prop passing to HighlightCode

The isWrapped state is properly passed to both instances of the HighlightCode component (main view and modal view).

Also applies to: 395-395, 451-451, 463-463


345-349: Dynamic styling based on wrap state

The conditional styling for the container and pre elements based on the isWrapped state is well-implemented. The use of cn() utility makes the code clean and readable.

Also applies to: 354-360


476-488: Refactored styling logic improves maintainability

The refactoring of style classes into variables (containerClasses and preClasses) improves code maintainability and reduces duplication. Good use of the cn() utility for conditional class application.


530-532: Consider potential issues with line number positioning

The sticky positioning of line numbers in wrapped mode is a good approach, but be aware that it might have inconsistent behavior across different browsers or in complex layouts.

Consider testing this functionality in different browsers (especially older ones) to ensure the sticky positioning of line numbers works as expected in wrapped mode.

Also applies to: 540-543

@matt-aitken matt-aitken merged commit df0bce3 into main May 1, 2025
11 of 12 checks passed
@matt-aitken matt-aitken deleted the word-wrap-on-code-blocks branch May 1, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants