zed icon indicating copy to clipboard operation
zed copied to clipboard

`format_on_save` does not work with `autosave`

Open ata-drag opened this issue 1 year ago • 1 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

When format_on_save: "on" is set together with autosave, the buffer is not formatted even after manual save. I do not expect the buffer to be formatted after it is automatically saved, but I do expect it to format when I press cmd+s or :w.

As of now I need to explicitly run editor: format from command palette or disable autosave to get the buffer formatted on save.

Environment

Zed: v0.149.3 (Zed Preview) OS: macOS 14.6.1 Memory: 32 GiB Architecture: aarch64

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

Settings file:

{
  "soft_wrap": "prefer_line",
  "theme": "Github Dark",
  "ui_font_size": 14,
  "buffer_font_size": 12,
  "buffer_font_family": "Monaco Nerd Font Mono",
  "vim_mode": true,
  "autosave": {
     "after_delay": {
        "milliseconds": 1000
    }
  },
  "tab_size": 2,
  "remove_trailing_whitespace_on_save": true,
  "format_on_save": "on"
}

If applicable, attach your Zed.log file to this issue.

No response

ata-drag avatar Aug 21 '24 11:08 ata-drag

Having this issue as well. Saving unchanged code should still format if you have "format_on_save": "on"

malssid avatar Aug 21 '24 16:08 malssid

I believe I know what's happening. Zed only invokes the external formatter on save if the buffer is dirty (has unsaved changes). Zed does not auto-format on autosave (fine) but if then if you manually save with the non-dirty buffer you expect it should format_on_save. That makes sense.

As workaround, you can manually invoke formatting (cmd-shift-p, editor: format) or cmd-shift-i.

notpeter avatar Aug 29 '24 18:08 notpeter

I believe I know what's happening. Zed only invokes the external formatter on save if the buffer is dirty (has unsaved changes). Zed does not auto-format on autosave (fine) but if then if you manually save with the non-dirty buffer you expect it should format_on_save. That makes sense.

As workaround, you can manually invoke formatting (cmd-shift-p, editor: format) or cmd-shift-i.

Thanks. I just added a 1500 ms delay on the auto-save, which has been working for me. So if I wanna format code + save, I manually save right after making a change.

malssid avatar Aug 29 '24 19:08 malssid

I believe I know what's happening. Zed only invokes the external formatter on save if the buffer is dirty (has unsaved changes). Zed does not auto-format on autosave (fine) but if then if you manually save with the non-dirty buffer you expect it should format_on_save. That makes sense.

As workaround, you can manually invoke formatting (cmd-shift-p, editor: format) or cmd-shift-i.

is there a way to make formating work with autosave on delay instead of just manual autosave?

UdittLamba avatar Sep 12 '24 12:09 UdittLamba

Hey guys, I ran into this problem and i created a workaround, on my settings.json it got like this:

{
  "autosave": {
    "after_delay": {
      "milliseconds": 0
    }
  }
}

And on my keymaps.json

[
  {
    "context": "Editor",
    "bindings": {
      "ctrl-s": "editor::Format"
  }
]

This way the file is always saved and when I press CTRL+S the file is formatted.

TheRafaelFarias avatar Dec 22 '24 14:12 TheRafaelFarias

I have opposite problem. I don't want to trigger format on save but it gets triggered regardless with the following configuration.

{
  "autosave": "on_focus_change",
  "format_on_save": "off"
}

naiyerasif avatar Apr 07 '25 15:04 naiyerasif

https://github.com/zed-industries/zed/pull/32626 have fixed it:

https://github.com/user-attachments/assets/5b1fb176-6ddf-4668-a92d-4470a78f3c22

SomeoneToIgnore avatar Jun 30 '25 15:06 SomeoneToIgnore