zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
46.86k stars 2.68k forks source link

Format on save loses cursor position #5143

Open henrydaggett opened 1 year ago

henrydaggett commented 1 year ago

Check for existing issues

Describe the bug / provide steps to reproduce it

I'm writing an HTML document, and I have autosave every 2000ms with format on save enabled. Often when I click to add a class or something to an existing tag or try to move tags around the format might reformat and then move the cursor to the start of the next line of HTML

Expected behavior

The cursor should never be moved without user input

Environment

Zed 0.62.7 – /Applications/Zed.app macOS 12.6.1 architecture x86_64

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue

No response

SteveLauC commented 4 months ago

I am experiencing this with both Markdown and Rust files

https://github.com/zed-industries/zed/assets/96880612/96393247-b45f-4c27-979d-2f1af64ceb83

To reproduce:

  1. enter insert mode (I have vim mode enabled)
  2. input 1. (note there is a blank space after the dot)
  3. wait for auto save
  4. you got it

My env:

$ zed --version
Zed 0.133.7 – /Applications/Zed.app
$ uname -a
Darwin Steves-MacBook-Air.local 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:52 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T8103 arm64

My config:

{
  "theme": "Ayu Light",
  "vim_mode": true,
  "ui_font_size": 16,
  "buffer_font_size": 16,
  "autosave":  {
    "after_delay": {
      "milliseconds": 1000
    }
  },
  "format_on_save": "off",
  "remove_trailing_whitespace_on_save": false,
  "relative_line_numbers": true,
  "inlay_hints": {
    "enabled": true,
    "show_type_hints": true,
    "show_parameter_hints": true,
    "show_other_hints": true
  }
}

If I enable remove_trailing_whitespace_on_save, then the cursor won't be moved to the next line, it will be set to the last character in the current line.

Update: I fixed this issue by disabling ensure_final_newline_on_save:)