Skip to content

Fix cursor misalignment when scrolling past wrapped lines#110

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

Fix cursor misalignment when scrolling past wrapped lines#110
hgwr merged 1 commit intomainfrom
codex/fix-cursor-jump-in-long-line-wrap

Conversation

@hgwr
Copy link
Owner

@hgwr hgwr commented Aug 9, 2025

Summary

  • keep cursor position consistent when scrolling past long wrapped lines
  • add utility helper for calculating wrapped line height

Testing

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

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

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 a bug where the cursor position would become misaligned when scrolling through text that wraps across multiple lines in the editor. My changes ensure that the cursor's screen position is accurately maintained by correctly calculating the height of wrapped lines and adjusting the cursor's screen position accordingly during vertical scrolling.

Highlights

  • Cursor Position Consistency: Ensured the cursor's screen position remains consistent and accurate, especially when scrolling past long, wrapped lines in the editor.
  • Line Height Calculation: Introduced a new utility function get_line_height to precisely calculate the number of screen rows a given text line occupies, taking into account terminal width and character widths for proper line wrapping.
  • Scrolling Logic Enhancement: Updated the NextLine command's scrolling mechanism to correctly account for the screen real estate occupied by wrapped lines, preventing cursor misalignment during vertical navigation.
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.

@hgwr hgwr merged commit 9deabc5 into main Aug 9, 2025
1 check passed
@hgwr hgwr deleted the codex/fix-cursor-jump-in-long-line-wrap branch August 9, 2025 06:17
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

This pull request aims to fix cursor misalignment when scrolling past wrapped lines by introducing a helper function get_line_height and using it to correctly calculate screen line changes. This is a good approach.

My review has identified a critical flaw in the implementation of get_line_height which would lead to incorrect calculations and further bugs. I've also suggested a minor simplification for better code clarity.

Additionally, I noticed that the old logic for calculating line height still exists in NextLine::execute. It would be beneficial to replace it with a call to the new get_line_height helper to improve maintainability and ensure consistency.

Comment on lines +8 to +17
let mut w = 0usize;
let mut h = 1usize;
for c in line.chars() {
w += get_char_width(c) as usize;
if w >= width as usize {
w = 0;
h += 1;
}
}
h

Choose a reason for hiding this comment

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

critical

The current logic for calculating line height is flawed. It incorrectly increments the line count when the accumulated width is exactly equal to the terminal width, which leads to an extra line being counted for lines that perfectly fit the width. For example, for a line "abc" and width 3, it would return 2 instead of 1.

This can cause significant cursor misalignments, which is what this PR aims to fix. Here is a corrected implementation that properly handles line wrapping.

Suggested change
let mut w = 0usize;
let mut h = 1usize;
for c in line.chars() {
w += get_char_width(c) as usize;
if w >= width as usize {
w = 0;
h += 1;
}
}
h
let mut w = 0usize;
let mut h = 1usize;
let width = width as usize;
for c in line.chars() {
let char_w = get_char_width(c) as usize;
if w + char_w > width {
h += 1;
w = char_w;
} else {
w += char_w;
}
}
h

Comment on lines +182 to +187
let max_window_start =
if editor.buffer.lines.len() > editor.content_height() as usize {
editor.buffer.lines.len() - editor.content_height() as usize
} else {
0
};

Choose a reason for hiding this comment

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

medium

This if expression can be simplified by using saturating_sub, which is more idiomatic in Rust for this kind of calculation. It subtracts two numbers, returning 0 if underflow would occur, which is exactly the logic implemented here.

Suggested change
let max_window_start =
if editor.buffer.lines.len() > editor.content_height() as usize {
editor.buffer.lines.len() - editor.content_height() as usize
} else {
0
};
let max_window_start = editor.buffer.lines.len().saturating_sub(editor.content_height() as usize);

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