feat(java): allow opting out default keymaps

These keymaps cannot be disabled in the { keys = { '{lhs}', false } }
fashion. So instead, this patch offers a chance to disable them in
opts.keys.

It makes which-key.nvim an optional dependency too.
This commit is contained in:
Frederick Zhang 2024-01-01 20:10:38 +11:00
parent 879e29504d
commit ddde91f260
No known key found for this signature in database
GPG key ID: 980A192C361BE1AE

View file

@ -62,7 +62,7 @@ return {
-- Set up nvim-jdtls to attach to java files. -- Set up nvim-jdtls to attach to java files.
{ {
"mfussenegger/nvim-jdtls", "mfussenegger/nvim-jdtls",
dependencies = { "folke/which-key.nvim" }, dependencies = { { "folke/which-key.nvim", optional = true } },
ft = java_filetypes, ft = java_filetypes,
opts = function() opts = function()
return { return {
@ -169,7 +169,12 @@ return {
callback = function(args) callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id) local client = vim.lsp.get_client_by_id(args.data.client_id)
if client and client.name == "jdtls" then if client and client.name == "jdtls" then
local wk = require("which-key") local ok, wk = pcall(require, "which-key")
local keys = vim.tbl_extend("force", {
action = ok,
test = ok,
}, opts.keys or {})
if keys.action then
wk.register({ wk.register({
["<leader>cx"] = { name = "+extract" }, ["<leader>cx"] = { name = "+extract" },
["<leader>cxv"] = { require("jdtls").extract_variable_all, "Extract Variable" }, ["<leader>cxv"] = { require("jdtls").extract_variable_all, "Extract Variable" },
@ -194,14 +199,14 @@ return {
"Extract Constant", "Extract Constant",
}, },
}, { mode = "v", buffer = args.buf }) }, { mode = "v", buffer = args.buf })
end
if opts.dap and Util.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then if opts.dap and Util.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then
-- custom init for Java debugger -- custom init for Java debugger
require("jdtls").setup_dap(opts.dap) require("jdtls").setup_dap(opts.dap)
require("jdtls.dap").setup_dap_main_class_configs() require("jdtls.dap").setup_dap_main_class_configs()
-- Java Test require Java debugger to work -- Java Test require Java debugger to work
if opts.test and mason_registry.is_installed("java-test") then if opts.test and keys.test and mason_registry.is_installed("java-test") then
-- custom keymaps for Java test runner (not yet compatible with neotest) -- custom keymaps for Java test runner (not yet compatible with neotest)
wk.register({ wk.register({
["<leader>t"] = { name = "+test" }, ["<leader>t"] = { name = "+test" },