Closed sirstanislav closed 1 year ago
i had the same problem after adding formatting, when opening a new file and running :au LspFormatting
only the new buffer was present( that's why in your case when App.java is reopened, formating start working but it does not work in other files anymore), I solved it by adding clear = false
to nvim_create_augroup
:
local augroup = vim.api.nvim_create_augroup("LspFormatting", { clear = false })
and now, all buffers are present when :au LspFormatting
is executed.
i don't know why it's not necessary with null-ls, i'd rather stay with the doubt.
My current config (ftplugin/java.lua
):
local lsp = require("plugins.lsp.lsp-servers")
local augroup = vim.api.nvim_create_augroup("LspFormatting", { clear = false })
local on_attach = function(client, bufnr)
lsp.on_attach(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format({ async = false })
end,
})
end
end
local javaconfig = {
cmd = {
"/home/levioneyh/.local/share/nvim/mason/bin/jdtls",
"-data",
"/home/levioneyh/.cache/jdtls/workspace",
"-configuration",
"/home/levioneyh/.cache/jdtls/config",
},
root_dir = vim.fs.dirname(vim.fs.find({ "gradlew", ".git", "mvnw" }, { upward = true })[1]),
on_attach = on_attach,
capabilities = lsp.default_capabilities,
}
require("jdtls").start_or_attach(javaconfig)
on_attach
is called per buffer. If you use the same group name without clear = false
it will override the previous autocmd definition.
You can either:
nvim_create_augroup
with { clear = false }vim.lsp.buf.format
in a if
that tests if there are any clients connected to the current buffer, to avoid warnings on non-lsp buffers.Thank you guys. Now it's works
LSP client configuration
Eclipse.jdt.ls version
1.25.0
Steps to Reproduce
Creting project with Maven cli:
Formatting doesn't work in opened file App.java for example, after opens another file/class Users.java in new buffer where formatting is works. If i close previous file App.java where formating doesn't work and reopen it, formating start working on save, but finished work in opened Users.java.
LspInfo in file where formatting on save stop working after opening another file.
I think the problem in this function
I have copied from null ls config where it works fine for other formatters.
Expected Result
Make formatting on save working in every file. Thank you for jdtls and any suggestions. Here is full repo with all configs
Actual Result
Clean logs from this startup and try to formatting first two files. I'm understand what there is some other errors, not related maybe for formatting. But anyway.
:JdtShowLogs
.log
lsp.log