Skip to content

Conversation

@asiloisad
Copy link
Contributor

I occasionally observe the cursor appearing at an indented position on empty lines (e.g., cursor visually at column 4 when the line has 0 characters). The issue:

  • Only occurs on empty lines
  • Happens intermittently during normal editor use (navigation, pressing Enter, etc.)
  • Disappears after typing or actions that trigger full re-render (resizing, opening DevTools)
  • Related to timing between cursor position updates and display layer state

Investigation

  1. Text buffer layer - Correctly stores cursor position; clipPosition properly clamps to line length
  2. Display layer - translateScreenPosition and constrainScreenPosition correctly clip positions
  3. Cursor class - setScreenPosition correctly delegates to display marker
  4. Rendering layer - addCursorDecorationToMeasure uses marker.getScreenRange() which occasionally returns stale/unclipped position due to timing

The root cause appears to be a race condition where the display layer's spatial index or screen line data hasn't fully synchronized when cursor decoration rendering queries the marker's screen position.

Solution

Add defensive column clamping at the rendering level in addCursorDecorationToMeasure().

  • The text is inserted correctly (buffer operations work fine)
  • The cursor marker position is stored correctly
  • Only the display was wrong - fixed at the display level
  • Catches all cursor rendering cases regardless of how position was set
const lineLength = model.lineLengthForScreenRow(row);
if (column > lineLength) {
  column = lineLength;
}

This reuses the lineLengthForScreenRow() call that was already being made, so there's no performance impact.

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.

1 participant