okuuva / auto-save.nvim

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

Expose `immediate_save` function #36

Closed primeapple closed 1 year ago

primeapple commented 1 year ago

Recently I discovered another issue that could be solved with the help of this plugin.

I use neotest quite a lot. I mapped the action to run a test file to <leader>rf. So, whenever I change a test, I immediately run the test file afterwards. However, since I use a 2 second delay for autosave this leads to the test running with the previous file content.

Of course we could manually execute the :w command before running the test, but this leads to autosave having an unneeded timer open, as well as the autocommand events not being handled.

What I need is public function like before_save that takes another function as an argument, and returns a callback. When calling this an immediate save is invoked and after that the given function is executed. However this seems a little overkill what if we also need an after_save function later?

So the best option here seems to make the immediate_save function public. I'll add a PR for this.

okuuva commented 1 year ago

So the best option here seems to make the immediate_save function public.

This to me seems like we'd be reinventing the wheel. Saving is a core functionality of any text editor, there shouldn't be a need to expose this functionality in a plugin.

Of course we could manually execute the :w command before running the test, but this leads to autosave having an unneeded timer open as well as the autocommand events not being handled.

Should we then listen to BufWritePost event and clear any timers for the saved buffers?

I don't get the part about "autocommand events not being handled" though, could you elaborate?

primeapple commented 1 year ago

Should we then listen to BufWritePost event and clear any timers for the saved buffers?

I really like this, however there is more about this. First off all the auto-save autocommands are not running, if we don't save via the auto-save method and use the vim method instead. The other part is that there might be buffers where we don't want to save. This is handled by our save method right now (conditions, silent, ...)

I don't get the part about "autocommand events not being handled" though, could you elaborate?

Yes. We have our own autocommands that run when things are autosaved. If we don't save via the auto-save method, we don't run these commands.

All in all it's difficult to answer. You are totally right with reinventing the wheel. My idea was that if the user uses the auto-save plugin he should preferably always save via this method. Maybe even remapping :w and ZZ to immediate_save. But I might be wrong about this.

primeapple commented 1 year ago

I thought more about that. I think you are right, manual saving should be done by the manual save command, which is :w.

I created a function to do this instead:

M.with_presave = function(callback)
    return function()
        local buf = api.nvim_get_current_buf()
        api.nvim_buf_call(buf, function()
            vim.cmd('silent! write')
        end)
        callback()
    end
end