Skip to content

Conversation

@mscolnick
Copy link
Contributor

Fixes #6788

When using marimo edit --watch or auto-formatting, the cursor would jump to the top of a cell on save (or auto-save). This occurred because file watching would detect changes, send an update-cell-codes message to the frontend, and the frontend would replace the entire editor content without preserving the cursor position.

This also fixes an old issue where the cursor would jump to the top of a cell after auto-formatting (regardless of whether it was on watch mode or not).

Solution

Created a new replaceEditorContent() utility that:

  • Checks if the editor has focus before replacing content
  • When focused, uses line-based cursor preservation:
    • Keeps the cursor on the same line number
    • Maintains the same column position within that line
    • If the line shrinks, clamps the cursor to the end of the line
    • If the line no longer exists, moves the cursor to the end of the last line
  • Prevents the cursor from jumping during external updates (like from file watching or auto-formatting)

…r auto-formatting

Fixes #6788

When using `marimo edit --watch` or auto-formatting, the cursor would jump to the top of a cell on save (or auto-save). This occurred because file watching would detect changes, send an `update-cell-codes` message to the frontend, and the frontend would replace the entire editor content without preserving the cursor position.

This also fixes an old issue where the cursor would jump to the top of a cell after auto-formatting (regardless of whether it was on watch mode or not).

## Solution

Created a new `replaceEditorContent()` utility that:

- Checks if the editor has focus before replacing content
- When focused, uses **line-based cursor preservation**:
  - Keeps the cursor on the same line number
  - Maintains the same column position within that line
  - If the line shrinks, clamps the cursor to the end of the line
  - If the line no longer exists, moves the cursor to the end of the last line
- Prevents the cursor from jumping during external updates (like from file watching or auto-formatting)
@vercel
Copy link

vercel bot commented Oct 15, 2025

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

Project Deployment Preview Comments Updated (UTC)
marimo-docs Ready Ready Preview Comment Oct 15, 2025 3:16pm

Light2Dark
Light2Dark previously approved these changes Oct 15, 2025
## 📝 Summary

<!--
Provide a concise summary of what this pull request is addressing.

If this PR fixes any issues, list them here by number (e.g., Fixes
#123).
-->

was curious on some of these behaviours, added tests

## 🔍 Description of Changes

<!--
Detail the specific changes made in this pull request. Explain the
problem addressed and how it was resolved. If applicable, provide before
and after comparisons, screenshots, or any relevant details to help
reviewers understand the changes easily.
-->

## 📋 Checklist

- [x] I have read the [contributor
guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md).
- [ ] For large changes, or changes that affect the public API: this
change was discussed or approved through an issue, on
[Discord](https://marimo.io/discord?ref=pr), or the community
[discussions](https://github.com/marimo-team/marimo/discussions) (Please
provide a link if applicable).
- [x] I have added tests for the changes made.
- [x] I have run the code and verified that it works as expected.
@mscolnick mscolnick merged commit 6730cde into main Oct 15, 2025
26 checks passed
@mscolnick mscolnick deleted the ms/fix-format branch October 15, 2025 15:52
@dmadisetti dmadisetti added the bug Something isn't working label Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cursor Randomly Jumps to Top of Cell When Using --watch

4 participants