add: google java format for java config

This commit is contained in:
asep.komarudin 2024-06-23 22:08:20 +07:00
parent 66c83b9abb
commit ab36e30b69
3 changed files with 45 additions and 2 deletions

View file

@ -46,6 +46,30 @@ if pcode.active_java_config.active then
{ "<Leader>TS", function() require("neotest").run.stop() end, desc = "Stop" },
},
},
{
"stevearc/conform.nvim",
event = { "BufReadPre", "BufNewFile" },
config = function()
local conform = require("conform")
conform.setup({
formatters_by_ft = {
java = { "google-java-format" },
},
format_on_save = {
-- These options will be passed to conform.format()
timeout_ms = 500,
lsp_fallback = true,
},
})
vim.keymap.set({ "n", "v" }, "<leader>lF", function()
conform.format({
lsp_fallback = true,
async = false,
timeout_ms = 500,
})
end, { desc = "Format file or range (in visual mode)" })
end,
},
}
end
return M

View file

@ -106,6 +106,24 @@ return {
local supported_formatters = formatter.list_registered(buf_ft)
vim.list_extend(buf_client_names, supported_formatters)
-- add conform.nvim
local status, conform = pcall(require, "conform")
if status then
local formatters = conform.list_formatters_for_buffer()
if formatters and #formatters > 0 then
vim.list_extend(buf_client_names, formatters)
else
-- check if lspformat
local lsp_format = require("conform.lsp_format")
local bufnr = vim.api.nvim_get_current_buf()
local lsp_clients = lsp_format.get_format_clients({ bufnr = bufnr })
if not vim.tbl_isempty(lsp_clients) then
table.insert(buf_client_names, "LSP Formatter")
end
end
end
-- add linter
local supported_linters = linter.linter_list_registered(buf_ft)
vim.list_extend(buf_client_names, supported_linters)