Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving while continueing to edit the buffer overwrites the post-save changes #21

Closed
DanCardin opened this issue Mar 4, 2022 · 10 comments

Comments

@DanCardin
Copy link

Kapture.2022-03-04.at.12.55.37.mp4

The actual buffer that gets saved seems to be the pre-format version, which makes sense because of the fact that it's using BufWritePost (btw, could it possibly be BufWritePre, so that one ends up with an unedited buffer after saving), fwiw.

@CKolkey
Copy link

CKolkey commented Mar 4, 2022

Seems like this can be corrected with if vim.fn.mode() == 'n' and (check changetick hasn't changed) in the handler callback function.

@DanCardin
Copy link
Author

That's probably a decent idea anyway, but i can equally repro by saving then quickly pasting or doing other normal mode things.

Ideally it would no-op if the state of the buffer had changed at all from the savepoint, although i dont know the practicality of doing so.

@lukas-reineke
Copy link
Owner

The actual buffer that gets saved seems to be the pre-format version

This is by design, it is unrelated to BufWritePost and BufWritePre. Formatting is done asynchronously, so the buffer is first saved like it is, then formatting is triggered, once the formatting results come back the plugin checks if it's safe to overwrite the buffer and write it again.

Ideally it would no-op if the state of the buffer had changed at all from the savepoint

It should do that. I use changedtick to check for changes, but it looks like that is not updated while you are still in insert mode. Like @CKolkey said, the plugin needs to check that as well. This should be an easy fix.

@lukas-reineke
Copy link
Owner

Can you test if #22 fixes the issue?

@DanCardin
Copy link
Author

I believe i'm still seeing it happen

@lukas-reineke
Copy link
Owner

Are you sure? I can't reproduce it anymore.

@lukas-reineke
Copy link
Owner

I release #22 now, but please open the issue again if you can still reproduce it.

@DanCardin
Copy link
Author

I switched back to master and can confirm that my git log contains the commit. I dont know if there's a way for me to manually verify that it's being picked up correctly.

But the above example repro behaves similarly, but not the same

Kapture.2022-03-07.at.09.25.20.mp4

@lukas-reineke
Copy link
Owner

hm, can you add some logging to try to debug this?

Can you print the output of vim.api.nvim_get_mode().mode here

if
vim.api.nvim_buf_get_var(ctx.bufnr, "format_changedtick")
== vim.api.nvim_buf_get_var(ctx.bufnr, "changedtick")
and not vim.startswith(vim.api.nvim_get_mode().mode, "i")
then

        print(vim.api.nvim_get_mode().mode)
        if
            vim.api.nvim_buf_get_var(ctx.bufnr, "format_changedtick")
                == vim.api.nvim_buf_get_var(ctx.bufnr, "changedtick")
            and not vim.startswith(vim.api.nvim_get_mode().mode, "i")
        then

@lukas-reineke lukas-reineke reopened this Mar 8, 2022
@DanCardin
Copy link
Author

DanCardin commented Mar 8, 2022

Okay, adding the print (or rather vim.notify) actually lead me down a path. I think your pr did fix it, it just wasn't being picked up despite the on-disk code being changed. some combination of packercompile and lua caching which i cleared eventually made the notify show up and it now seems to be working. Sorry for the false alarm!

I was esp thrown off by the seemingly different behavior to what i was seeing originally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants