okuuva / auto-save.nvim

🧶 Automatically save your changes in NeoVim
GNU General Public License v3.0
127 stars 7 forks source link

Make it work nicely with autoformat #55

Open primeapple opened 2 months ago

primeapple commented 2 months ago

I'm currently looking over https://github.com/AstroNvim/astrocommunity/tree/main/lua/astrocommunity/editing-support/auto-save-nvim . They use some magic to make autoformat work with this plugin.

I think we should support that out of the box, without any manual fumbling. Maybe it even works already? Let's have a little exploration on how it currently works with autoformat.

okuuva commented 2 months ago

I haven't had any issues with auto-save.nvim and autoformatting but then again I think I've configured it so that automatic saves do not trigger automatic saving. Hard to remember how it works since it Just Worksâ„¢ for me and I haven't had to think about in a long while. I'm sitting in a train tomorrow getting bored so I'll play around with my config as well to see if and how the two don't get along.

primeapple commented 1 month ago

That would be awesome! I personally don't use autoformat, but I'm interested in your experiences. Maybe it works quite well? Please post your setup :)

okuuva commented 1 month ago

Ok. The reason why it has been working for me is that I have set noautocmd = true. Without that auto-save triggers autoformat which then triggers auto-save again. Then undo and redo behave all wonky as you end up fighting against both auto-save and autoformatter. Basically what was reported in the pocco81 repo: https://github.com/pocco81/auto-save.nvim/issues/70#issuecomment-1554728631

I don't know if there is a universal way to disable autoformat with before_save callback and re-enable it in after_save callback. I took a look at how AstroNvim does it and it manipulates global variable autoformat_enabled which I believe is AstroNvim specific. I might be mistaken but to me it seems that we'd have to support all the different autoformatters (or at least autoformatter plugins) separately which I don't think is feasible.

But we already kinda support this. It's the noautocmd = true setting. To my understanding most (if not all) autoformatters hook into the BufWritePre autocommand to run the formatter before buffer gets saved.

Is there any use case for auto-save to ever trigger autocommands? I've never used it or needed it so I can't think of any. If not we could just remove the setting and always save with noautocmd. If there are then we could at least change the default value to true. I believe that would solve like 90% of the problems people have with auto-save and autoformatter clashing. And the people that absolutely want to run autocommands with auto-save should be savvy enough to figure out before_save and after_save callbacks to disable and re-enable their autoformatting plugin (or tool) themselves. I could cook up one as an example to the readme for the plugin I use (conform.nvim).

aksh1618 commented 1 month ago

Is there any use case for auto-save to ever trigger autocommands?

This was in fact the very use case that made me look for an auto-save plugin in the first place! I code in rust in neovim, and I have it set-up to run the linter and formatting on save. This in turn made me want to avoid having to save manually and have it auto-save as soon as I was done typing a line or two in order to get a virtually real-time linting and formatting experience, which led me to this plugin.

primeapple commented 1 month ago

How is your experience with it @aksh1618 ? Do you have any troubles? Do you think we need better support for autoformat or is it working as expected?