-
Notifications
You must be signed in to change notification settings - Fork 785
Managing undo history #879
Comments
If you use a The only way to do this that I'm aware of would be null_ls.setup({
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_buf_create_user_command(bufnr, "LspFormatting", function()
vim.lsp.buf.format()
end)
end
end,
}) This seems to work when I manually call |
When you say you don't consider this an issue, with your setup do you have this jarring jumping effect - or is there a workaround for it? Screenshot.2022-05-21.at.15.17.35-converted.mp4For example, here I do My super hacky work around at the moment is: if client.supports_method("textDocument/formatting") then
vim.api.nvim_set_keymap("n", "u", "2u", { noremap = true })
end 😅 |
I am not sure what your setup looks like, but are you automatically writing the file when you leave insert mode? That looks painful, and if I were going to do that, I would definitely not set up formatting on save. With the recommended setup - a simple |
Yes, the default behaviour for most autosave plugins is to save on the |
With https://github.com/Pocco81/AutoSave.nvim, formatting before saving via the
|
I played around with AutoSave.nvim, and I think this works - if I make a change that requires the file to be formatted and then undo, it undoes both the formatting changes and my original change - but I'm not 100% sure what the expected behavior is, so I can't say for sure: local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
require("null-ls").setup({
sources = sources,
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_buf_create_user_command(bufnr, "LspFormatting", function()
-- or vim.lsp.buf.formatting(bufnr) on 0.8
vim.lsp.buf.formatting_sync()
end, {})
-- you can leave this out if your on_attach is unique to null-ls,
-- but if you share it with multiple servers, you'll want to keep it
vim.api.nvim_clear_autocmds({
group = augroup,
buffer = bufnr,
})
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
command = "undojoin | LspFormatting",
})
end
end,
}) I also ran into this issue when manually saving the file, but it seems to work well enough with AutoSave.nvim, at least during my quick testing. I have a better understanding of the use case now and am happy to investigate how we can support it. Having said that, I'll mention that null-ls is not doing anything special here - all it's doing is passing text document changes to Neovim's LSP handlers, so whatever issues null-ls has here will also exist with actual LSP formatting. |
That's awesome! I've played around with the two methods... the first has the advantage of there being no split-second view of the unformatted code, but it feels a tiny but perceivable bit slower. Your method feels snappier, although you do get the slightly disorientating view of the unformatted code - although I'm used to it personally. I prefer the faster implementation because not all text editing requires formatting anyway, so it's not worth slowing everything down for simple text changes, but leaving this comparison here for anyone else who might prefer the other method. Thanks again! |
I added a link to the workaround to the wiki, so I'm going to close this. |
Thanks! I solved it. I use nvim0.8 . local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
local null_ls = require("null-ls")
null_ls.setup({
sources = {
null_ls.builtins.formatting.gofmt,
},
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_buf_create_user_command(bufnr, "LspFormatting", function ()
vim.lsp.buf.format({ bufnr = bufnr })
end, {})
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
command = "undojoin | LspFormatting",
})
end
end,
}) |
Issues
Feature description
Hey! I'm moving from Neoformat to null-ls. I used to use Neoformat with https://github.com/Pocco81/AutoSave.nvim. null-ls has a way to format on save https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Formatting-on-save#code. Each time a code change is made or the text changes, the save functionality is run. That means it's generally saved twice. So when you try to undo the last change, it's undoing to the last formatted rather than the actual code content changing. This means you need to undo twice, and it can be a bit visually jumpy whilst it's trying to format you go through the changes. Neoformat used to handle this like so, https://github.com/sbdchd/neoformat#managing-undo-history - is there a way to do this with null-ls? Thanks!
Help
No
Implementation help
No response
The text was updated successfully, but these errors were encountered: