Hey, what is the exact lua equivalent to this autocmd?
augroup lsp_document_format
autocmd! * <buffer>
autocmd BufWritePre <buffer> lua vim.lsp.buf.format()
augroup END
Thanks! (:
Hey, what is the exact lua equivalent to this autocmd?
augroup lsp_document_format
autocmd! * <buffer>
autocmd BufWritePre <buffer> lua vim.lsp.buf.format()
augroup END
Thanks! (:
somthing like this
vim.api.nvim_create_autocmd("BufWritePre", {
callback = function() vim.lsp.buf.format() end,
group = vim.api.nvim_create_augroup("lsp_document_format", {clear = true})
})
Where did you stuck?
I wanted to attach an autocmd to a buffer. In my lsp configuration, I have this on_attach(client, bufnr)
function and I want to create an autocmd in it like this:
local grp = vim.api.nvim_create_augroup("lsp_document_format", { clear = true })
local autocmd = vim.api.nvim_create_autocmd
autocmd("BufWritePre", {
callback = function()
if client.supports_method("textDocument/formatting") then
vim.lsp.buf.format()
else
vim.schedule(function()
print('No formatter')
end)
end
end,
group = grp
})
My problem is this: If when I launch neovim, the first buffer I save doesn’t have a formatter (python for example) then all the buffers I open afterwards, including the ones that do (lua for example) will display the same message. On the contrary if the first buffer I save has one then all the buffers I open afterwards will execute vim.lsp.buf.format()
.
I don’t really know why. I thought maybe the problem was that the autocmd was not " attached " to the buffer.
Thank you very much for you help!
There is a buffer for the nvim_create_autocmd
. You can set buffer = 0
for the current buffer, so it will run only in this buffer.
if client.supports_method("textDocument/formatting") then
vim.api.nvim_create_autocmd("BufWritePre", {
callback = function() vim.lsp.buf.format() end,
group = vim.api.nvim_create_augroup("lsp_document_format", {clear = true})
buffer = 0
})
end
It’s exactly what I was looking for. Thanks!