diff --git a/lua/pcode/plugins/lang/java.lua b/lua/pcode/plugins/lang/java.lua index abb776d..325589f 100644 --- a/lua/pcode/plugins/lang/java.lua +++ b/lua/pcode/plugins/lang/java.lua @@ -18,10 +18,28 @@ end M = { { "williamboman/mason-lspconfig.nvim", + -- stylua: ignore opts = function(_, opts) opts.skip_config = opts.skip_config or {} vim.list_extend(opts.skip_config, { "jdtls" }) + -- Set vim motion for + l + h to show code documentation about the code the cursor is currently over if available + vim.keymap.set("n", "lh", vim.lsp.buf.hover, { desc = "Code Hover Documentation" }) + -- Set vim motion for + l + d to go where the code/variable under the cursor was defined + vim.keymap.set("n", "ld", vim.lsp.buf.definition, { desc = "Code Goto Definition" }) + -- Set vim motion for + l + a for display code action suggestions for code diagnostics in both normal and visual mode + vim.keymap.set({ "n", "v" }, "la", vim.lsp.buf.code_action, { desc = "Code Actions" }) + -- Set vim motion for + l + r to display references to the code under the cursor + vim.keymap.set("n", "lr", require("telescope.builtin").lsp_references, { desc = "Code Goto References" }) + -- Set vim motion for + l + i to display implementations to the code under the cursor + vim.keymap.set("n", "li", require("telescope.builtin").lsp_implementations, { desc = "Code Goto Implementations" }) + -- Set a vim motion for + l + R to smartly rename the code under the cursor + vim.keymap.set("n", "lR", vim.lsp.buf.rename, { desc = "Code Rename" }) + -- Set a vim motion for + l + D to go to where the code/object was declared in the project (class file) + vim.keymap.set("n", "lD", vim.lsp.buf.declaration, { desc = "Code Goto Declaration" }) end, + keys = { + { "l", "", desc = "LSP", mode = { "n", "v" } }, + }, }, { "mfussenegger/nvim-jdtls", @@ -103,8 +121,6 @@ M = { end local function attach_jdtls() - local fname = vim.api.nvim_buf_get_name(0) - -- Configuration can be augmented and overridden by opts.jdtls local config = extend_or_override({ cmd = opts.full_cmd(opts), @@ -134,56 +150,32 @@ M = { vim.api.nvim_create_autocmd("LspAttach", { callback = function(args) local client = vim.lsp.get_client_by_id(args.data.client_id) + -- stylua: ignore if client and client.name == "jdtls" then - local wk = require("which-key") - wk.add({ - { - mode = "n", - buffer = args.buf, - { "cx", group = "extract" }, - { "cxv", require("jdtls").extract_variable_all, desc = "Extract Variable" }, - { "cxc", require("jdtls").extract_constant, desc = "Extract Constant" }, - { "gs", require("jdtls").super_implementation, desc = "Goto Super" }, - { "co", require("jdtls").organize_imports, desc = "Organize Imports" }, - }, - }) - wk.add({ - { - mode = "v", - buffer = args.buf, - { "cx", group = "extract" }, - { - "cxm", - [[lua require('jdtls').extract_method(true)]], - desc = "Extract Method", - }, - { - "cxv", - [[lua require('jdtls').extract_variable_all(true)]], - desc = "Extract Variable", - }, - { - "cxc", - [[lua require('jdtls').extract_constant(true)]], - desc = "Extract Constant", - }, - }, - }) + -- add keymaps + vim.keymap.set('n', 'J', "", { desc = "Java" }) + -- Set a Vim motion to + J + o to organize imports in normal mode + vim.keymap.set('n', 'Jo', " lua require('jdtls').organize_imports()", { desc = "Java Organize Imports" }) + -- Set a Vim motion to + J + v to extract the code under the cursor to a variable + vim.keymap.set('n', 'Jv', " lua require('jdtls').extract_variable()", { desc = "Java Extract Variable" }) + -- Set a Vim motion to + J + v to extract the code selected in visual mode to a variable + vim.keymap.set('v', 'Jv', " lua require('jdtls').extract_variable(true)", { desc = "Java Extract Variable" }) + -- Set a Vim motion to + J + C to extract the code under the cursor to a static variable + vim.keymap.set('n', 'JC', " lua require('jdtls').extract_constant()", { desc = "Java Extract Constant" }) + -- Set a Vim motion to + J + C to extract the code selected in visual mode to a static variable + vim.keymap.set('v', 'JC', " lua require('jdtls').extract_constant(true)", { desc = "Java Extract Constant" }) + -- Set a Vim motion to + J + t to run the test method currently under the cursor + vim.keymap.set('n', 'Jt', " lua require('jdtls').test_nearest_method()", { desc = "Java Test Method" }) + -- Set a Vim motion to + J + t to run the test method that is currently selected in visual mode + vim.keymap.set('v', 'Jt', " lua require('jdtls').test_nearest_method(true)", { desc = "Java Test Method" }) + -- Set a Vim motion to + J + T to run an entire test suite (class) + vim.keymap.set('n', 'JT', " lua require('jdtls').test_class()", { desc = "Java Test Class" }) + -- Set a Vim motion to + J + u to update the project configuration + vim.keymap.set('n', 'Ju', " JdtUpdateConfig", { desc = "Java Update Config" }) if opts.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(opts.dap_main) - - -- Java Test require Java debugger to work - -- if opts.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" }, - -- ["tt"] = { require("jdtls.dap").test_class, "Run All Test" }, - -- ["tr"] = { require("jdtls.dap").test_nearest_method, "Run Nearest Test" }, - -- ["tT"] = { require("jdtls.dap").pick_test, "Run Test" }, - -- }, { mode = "n", buffer = args.buf }) - -- end end -- User can set additional keymaps in opts.on_attach