Skip to content

Fix scrolling after wrapped lines#108

Merged
hgwr merged 1 commit intomainfrom
codex/fix-cursor-jump-issue-on-line-wrap
Aug 9, 2025
Merged

Fix scrolling after wrapped lines#108
hgwr merged 1 commit intomainfrom
codex/fix-cursor-jump-issue-on-line-wrap

Conversation

@hgwr
Copy link
Owner

@hgwr hgwr commented Aug 9, 2025

Summary

  • adjust j movement so leaving a wrapped line doesn't scroll the window unexpectedly

Testing

  • cargo build --verbose
  • cargo test --verbose
  • pytest -n auto e2e --verbose

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

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @hgwr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

I've addressed an issue where moving the cursor down (j command) in the editor would cause unexpected screen scrolling when navigating through lines that wrap. My changes refine the NextLine command's behavior to accurately account for wrapped lines, ensuring that the cursor moves smoothly to the next logical line without causing the display to jump. This significantly improves the user experience when editing text with word wrapping enabled.

Highlights

  • Improved Cursor Movement for Wrapped Lines: The core change is within the NextLine command, which is typically bound to the j key for moving the cursor down. The previous implementation could cause the screen to jump unexpectedly when moving from a wrapped line to the next logical line.
  • Precise Calculation of Remaining Wrapped Lines: The updated logic now calculates how many display lines remain on the current logical line from the cursor's current position. This calculation considers the terminal's width and the width of individual characters.
  • Smarter Screen and Window Scrolling: Based on the calculated remaining lines, the code intelligently adjusts both the cursor's screen position and the editor's window position in the buffer. This ensures that the screen scrolls only when necessary and by the correct amount, preventing jarring visual jumps.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request adjusts the 'j' key movement to correctly handle scrolling with wrapped lines. The logic for calculating line wrapping information is duplicated, which can be refactored for better maintainability and slight performance improvement. Otherwise, the change correctly addresses the issue of unexpected scrolling.

Comment on lines +113 to +136
// 現在のカーソル位置が行の何行目に相当するかを計算
let mut width = 0usize;
let mut cursor_row_in_line = 0usize;
for (i, c) in current_line.chars().enumerate() {
if i >= editor.cursor_position_in_buffer.col {
break;
}
width += get_char_width(c) as usize;
if width >= editor.terminal_size.width as usize {
width = 0;
cursor_row_in_line += 1;
}
}

// 行全体が何行に折り返されるかを計算
width = 0;
let mut line_height = 1usize;
for c in current_line.chars() {
width += get_char_width(c) as usize;
if width >= editor.terminal_size.width as usize {
width = 0;
line_height += 1;
}
}

Choose a reason for hiding this comment

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

medium

The logic to calculate cursor_row_in_line and line_height involves iterating over the characters of current_line twice. This can be optimized by combining these two loops into a single pass over the line's characters. This refactoring will improve code readability and reduce redundancy.

            // Calculate both cursor_row_in_line and total line_height in one pass
            let mut width = 0usize;
            let mut cursor_row_in_line = 0usize;
            let mut line_height = 1usize;
            let cursor_col = editor.cursor_position_in_buffer.col;

            for (i, c) in current_line.chars().enumerate() {
                if i == cursor_col {
                    cursor_row_in_line = line_height - 1;
                }

                width += get_char_width(c) as usize;
                if width >= editor.terminal_size.width as usize {
                    width = 0;
                    line_height += 1;
                }
            }
            if cursor_col >= current_line.chars().count() {
                cursor_row_in_line = line_height - 1;
            }

@hgwr hgwr merged commit 6874a57 into main Aug 9, 2025
1 check passed
@hgwr hgwr deleted the codex/fix-cursor-jump-issue-on-line-wrap branch August 9, 2025 05:49
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