nvim-metals
nvim-metals copied to clipboard
Formatting on save discards latest changes to file
Describe the bug
Sometimes (like every 15 or 30 minutes) when I save a file (:w<CR>
) the auto formatting on file save discards my latest changes to the file and reformats the version of the file before and puts that version in the buffer.
I enabled auto formatting on save like this:
metals_config.on_attach = function(client, bufnr)
require("metals").setup_dap()
vim.cmd("autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()")
end
Sadly, it is not easily reproduceable. It happens more or less random and not too often.
Expected behavior
It should always format the newest version of the file.
Operating system
Linux
Version of Metals
v0.11.7
Commit of nvim-metals
9b9fad5fcdf725c07c7c549064fb0347d26770dd
Maybe try changing BufWritePre
to BufWritePost
. I think that might help.
I see that BufWritePost
is triggered after writing the whole buffer to a file.
You can read more if you execute :help BufWritePost
in your neovim.
I'll give it a shot. Thanks!
I'm not yet sure whether it works or not, but it seems to have the problem that I now sometimes get vim to ask me for confirming with Enter after doing :w<CR>
:
Maybe because the
"jvm/src/main/scala/org/felher/inheritance/Main.scala" 66L, 2492B written
and
LSP[metals][Log] Compiling inheritanceJVM
are coming in at the same time and it now needs two lines?
are coming in at the same time and it now needs two lines?
Sort of yea, but one is coming because of the -F
shortmess and the other is from Metals. Sort of an aside but for the metals logs, I highly highly recommend setting metals_config.init_options.statusBarProvider = "on"
and this will then send all of these types of messages to metals_status
instead. Then you can access it via vim.g["metals_status"]
in your statusline. Everything works way smoother when you have that set.
I should have more time tomorrow to actually look at this issue with the formatting.
Great! I'll give statusBarProvider = "on"
a try! Thanks!
Just a heads up I played around with this today and wasn't able to mimic it. I figured I'd have a hard time since you said it only happens every 15 or 30 minutes. However I tried it on some small and very large files (large ones sucked since the sync called literally locked everything. I even had a timeout a couple times). But did switching to BufWritePost
help?
If you notice this again, here's what would be super helpful. NOTE: that you'll need to create an lsp.trace.json
file to get this info. You can find instructions for that here. But look at the trace file and you should see a textDocument/formatting
request and a response, a textDocument/didChange
notification, followed by a textDocument/didSave
notification. I'd be curious to see the order of which these all happen and what they contain. It might help pin this down.
Thank you for playing with it! I didn't have any issues with BufWritePost
yet (after I set the statusBarProvider) but I have not used it that long. I made a mark in my calendar a week from now. Then I'll report back whether it happened again.
It seems that using ButWritePost
has fixed the issue. I had it only once again in the last week and this might well have been a mistake on my part. (i.e. hitting u before :w) or something. Feel free to close it :)
Awesome. If you happen to hit on this again don't hesitate to respond back. Also thanks for the answer @antosha417!