Skip to content

fix: move caret to previous line on arrow-up inside code blocks - #8978

Open
BetterAndBetterII wants to merge 1 commit into
AppFlowy-IO:mainfrom
BetterAndBetterII:fix/code-block-arrow-up
Open

BetterAndBetterII wants to merge 1 commit into
AppFlowy-IO:mainfrom
BetterAndBetterII:fix/code-block-arrow-up

Conversation

@BetterAndBetterII

@BetterAndBetterII BetterAndBetterII commented Aug 29, 2026

Copy link
Copy Markdown

Summary

  • Arrow-up inside a multi-line code block used geometry-based vertical movement, which includes the language-picker header and jumped to the previous block.
  • Register a newline-based arrowUpInCodeBlock handler before arrowUpToTitle so Up moves to the previous line (same column, clamped) while the first line still falls through to the default handler.

Fixes #8936

Test plan

  • flutter test test/unit_test/document/shortcuts/code_block_arrow_command_test.dart (7 cases)
  • Manual: in a multi-line code block, Up from line 2+ stays in the block on the previous line; Up from line 1 leaves the block

Summary by Sourcery

Fix Up-arrow navigation in multi-line code blocks so the caret moves between code lines without skipping to the previous block.

Bug Fixes:

  • Keep the caret within multi-line code blocks when pressing Up, moving to the previous line at the same column and clamping to shorter lines.
  • Allow Up from the first code-block line to continue to the default behavior and leave the block.

Tests:

  • Add unit coverage for code-block vertical movement, column clamping, boundary behavior, and ignored cases.

Geometry-based vertical movement includes the language-picker header,
so Up jumped to the previous block instead of the previous line.
Register a newline-based arrow-up handler before arrowUpToTitle.

Fixes AppFlowy-IO#8936
@CLAassistant

CLAassistant commented Aug 29, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@sourcery-ai

sourcery-ai Bot commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Introduces a prioritized, newline-based Up-arrow handler for code blocks, keeping the caret within the block on lines after the first while preserving existing fallback behavior at the first line and in other contexts.

Sequence diagram for code block Arrow-Up handling

sequenceDiagram
    participant User
    participant Shortcuts as Command shortcuts
    participant Handler as arrowUpInCodeBlock
    participant Editor as EditorState
    participant Default as Default arrow-up handler

    User->>Shortcuts: Arrow Up
    Shortcuts->>Handler: _arrowUpInCodeBlockHandler(editorState)
    Handler->>Editor: getNodeAtPath(selection.end.path)
    alt Collapsed caret in a code block after the first line
        Handler->>Editor: selection = Selection.collapsed(previous line offset)
        Handler-->>User: KeyEventResult.handled
    else First line or non-code-block context
        Handler-->>Shortcuts: KeyEventResult.ignored
        Shortcuts->>Default: Handle Arrow Up
    end
Loading

File-Level Changes

Change Details Files
Adds a dedicated Up-arrow command that performs newline-based vertical navigation within code blocks.
  • Validates collapsed selections located in code-block nodes.
  • Calculates the previous line and preserves the caret column, clamping to the shorter line.
  • Returns ignored on the first line or for unsupported selection/node states so existing handlers can continue.
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/code_block/code_block_arrow_command.dart
Registers the code-block navigation command ahead of broader Up-arrow handlers.
  • Places the new command before title and standard arrow-up shortcuts to prevent geometry-based movement across the code-block header or into the previous node.
frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/shortcuts/command_shortcuts.dart
Adds unit coverage for code-block Up-arrow behavior and fallback cases.
  • Tests same-column movement, line transitions, column clamping, empty lines, first-line fallback, non-code-block selections, and expanded selections.
frontend/appflowy_flutter/test/unit_test/document/shortcuts/code_block_arrow_command_test.dart

Assessment against linked issues

Issue Objective Addressed Explanation
#8936 Ensure pressing the Up arrow from a line after the first line inside a multi-line code block moves the caret to the preceding line within the same code block, preserving the column where possible and clamping it for shorter lines.
#8936 Allow pressing the Up arrow from the first line of a code block to retain the existing behavior of moving to the preceding block or node.
#8936 Add automated coverage for code-block Up-arrow behavior, including line movement, column clamping, first-line fallback, and ignored cases.

Possibly linked issues


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

@sourcery-ai sourcery-ai Bot left a comment

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.

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

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.

[Bug] Code block cursor issue

2 participants