Skip to content

[lexical-code] Bug Fix: Respect RTL when moving to line start/end in code blocks#7921

Merged
etrepum merged 3 commits intofacebook:mainfrom
ashmod:fix-rtl
Oct 15, 2025
Merged

[lexical-code] Bug Fix: Respect RTL when moving to line start/end in code blocks#7921
etrepum merged 3 commits intofacebook:mainfrom
ashmod:fix-rtl

Conversation

@ashmod
Copy link
Copy Markdown
Contributor

@ashmod ashmod commented Oct 13, 2025

Description

  • Current behavior
    • In code blocks, Command(CTRL)+Left/Right (MOVE_TO_START/END) on RTL lines moves the caret to the opposite visual end of the line.
  • New behavior
    • Detect the line’s direction and flip MOVE_TO_START/END semantics for RTL so the caret moves to the correct visual ends.
    • LTR behavior remains unchanged. Non-code content is unaffected.
  • Implementation
    • Add $getCodeLineDirection(anchor) in @lexical/code:
      • Scans the code line for the first strong LTR/RTL character across CodeHighlightNode/TabNode segments.
      • Falls back to the parent element’s direction; returns null if indeterminate.
    • Use $getCodeLineDirection in both highlighters (Prism, Shiki) to determine whether to invert MOVE_TO_START/END

Closes #7913

Test plan

Before

  • Steps to reproduce
    • Insert a code block; type an RTL string (e.g., "تجربة").
    • Place the caret within the line.
    • Press Command(CTRL)+Left (MOVE_TO_START) or Command(CTRL)+Right (MOVE_TO_END).
    • Caret jumps to the opposite visual end of the line.
  • Automated tests
    • No RTL-specific unit coverage in code blocks for MOVE_TO_START/END.

After

  • Steps to verify
    • Repeat the steps above.
    • MOVE_TO_START moves caret to visual left; MOVE_TO_END moves caret to visual right on RTL lines.
    • LTR lines behave as before.
  • Automated tests
    • Added RTL test cases to LexicalCodeNode.test.ts

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 13, 2025
@vercel
Copy link
Copy Markdown

vercel bot commented Oct 13, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
lexical Ready Ready Preview Comment Oct 13, 2025 9:31pm
lexical-playground Ready Ready Preview Comment Oct 13, 2025 9:31pm

Co-authored-by: Bob Ippolito <bob@redivi.com>
Copy link
Copy Markdown
Collaborator

@etrepum etrepum left a comment

Choose a reason for hiding this comment

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

This all seems to have the correct behavior but I am not fluent in any RTL language. @noamzaks do you have any opinion on this fix?

@noamzaks
Copy link
Copy Markdown
Contributor

This looks pretty good to me, although I don't have the best knowledge in BiDi specifications it looks familiar.

Thank you! :)

@etrepum etrepum added this pull request to the merge queue Oct 15, 2025
Merged via the queue into facebook:main with commit 064e2c0 Oct 15, 2025
45 checks passed
This was referenced Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: command+arrow moves to opposite side in RTL in code blocks

3 participants