From ddde91f260bdbbdaf7df208bff59756c424ad25c Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Mon, 1 Jan 2024 20:10:38 +1100 Subject: [PATCH] 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. --- lua/lazyvim/plugins/extras/lang/java.lua | 61 +++++++++++++----------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/lua/lazyvim/plugins/extras/lang/java.lua b/lua/lazyvim/plugins/extras/lang/java.lua index f7e7ca5f..bf39d765 100644 --- a/lua/lazyvim/plugins/extras/lang/java.lua +++ b/lua/lazyvim/plugins/extras/lang/java.lua @@ -62,7 +62,7 @@ return { -- Set up nvim-jdtls to attach to java files. { "mfussenegger/nvim-jdtls", - dependencies = { "folke/which-key.nvim" }, + dependencies = { { "folke/which-key.nvim", optional = true } }, ft = java_filetypes, opts = function() return { @@ -169,39 +169,44 @@ return { callback = function(args) local client = vim.lsp.get_client_by_id(args.data.client_id) if client and client.name == "jdtls" then - local wk = require("which-key") - wk.register({ - ["cx"] = { name = "+extract" }, - ["cxv"] = { require("jdtls").extract_variable_all, "Extract Variable" }, - ["cxc"] = { require("jdtls").extract_constant, "Extract Constant" }, - ["gs"] = { require("jdtls").super_implementation, "Goto Super" }, - ["gS"] = { require("jdtls.tests").goto_subjects, "Goto Subjects" }, - ["co"] = { require("jdtls").organize_imports, "Organize Imports" }, - }, { mode = "n", buffer = args.buf }) - wk.register({ - ["c"] = { name = "+code" }, - ["cx"] = { name = "+extract" }, - ["cxm"] = { - [[lua require('jdtls').extract_method(true)]], - "Extract Method", - }, - ["cxv"] = { - [[lua require('jdtls').extract_variable_all(true)]], - "Extract Variable", - }, - ["cxc"] = { - [[lua require('jdtls').extract_constant(true)]], - "Extract Constant", - }, - }, { mode = "v", buffer = args.buf }) - + 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({ + ["cx"] = { name = "+extract" }, + ["cxv"] = { require("jdtls").extract_variable_all, "Extract Variable" }, + ["cxc"] = { require("jdtls").extract_constant, "Extract Constant" }, + ["gs"] = { require("jdtls").super_implementation, "Goto Super" }, + ["gS"] = { require("jdtls.tests").goto_subjects, "Goto Subjects" }, + ["co"] = { require("jdtls").organize_imports, "Organize Imports" }, + }, { mode = "n", buffer = args.buf }) + wk.register({ + ["c"] = { name = "+code" }, + ["cx"] = { name = "+extract" }, + ["cxm"] = { + [[lua require('jdtls').extract_method(true)]], + "Extract Method", + }, + ["cxv"] = { + [[lua require('jdtls').extract_variable_all(true)]], + "Extract Variable", + }, + ["cxc"] = { + [[lua require('jdtls').extract_constant(true)]], + "Extract Constant", + }, + }, { mode = "v", buffer = args.buf }) + end if opts.dap and Util.has("nvim-dap") and mason_registry.is_installed("java-debug-adapter") then -- custom init for Java debugger require("jdtls").setup_dap(opts.dap) require("jdtls.dap").setup_dap_main_class_configs() -- 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) wk.register({ ["t"] = { name = "+test" },