-
-
Notifications
You must be signed in to change notification settings - Fork 63
Sample Configurations
Phạm Võ Quang Minh edited this page May 30, 2024
·
17 revisions
This page contains some sample configurations. You're welcome to add your own or link to your dotfiles.
@hydroakri Configuration
init.lua
require("lazy").setup({
{
"mfussenegger/nvim-jdtls",
ft = { "java" },
config = function()
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local workspace_dir = "/path/to/workspace-root/" .. project_name
local config = {
-- The command that starts the language server
-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line
cmd = {
-- 💀
"/usr/lib/jvm/java-17-openjdk/bin/java", -- or '/path/to/java17_or_newer/bin/java'
-- depends on if `java` is in your $PATH env variable and if it points to the right version.
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
"-Dosgi.bundles.defaultStartLevel=4",
"-Declipse.product=org.eclipse.jdt.ls.core.product",
"-Dlog.protocol=true",
"-Dlog.level=ALL",
"-Xmx1g",
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
-- 💀
"-jar",
"/home/hydroakri/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.1.6.700.v20231214-2017.jar",
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
-- Must point to the Change this to
-- eclipse.jdt.ls installation the actual version
-- 💀
"-configuration",
"/home/hydroakri/.local/share/nvim/mason/packages/jdtls/config_linux",
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^
-- Must point to the Change to one of `linux`, `win` or `mac`
-- eclipse.jdt.ls installation Depending on your system.
-- 💀
-- See `data directory configuration` section in the README
"-data",
workspace_dir,
},
-- 💀
-- This is the default if not provided, you can remove it. Or adjust as needed.
-- One dedicated LSP server & client will be started per unique root_dir
root_dir = require("jdtls.setup").find_root({ ".git", "mvnw", "gradlew" }),
-- Here you can configure eclipse.jdt.ls specific settings
-- See https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
-- for a list of options
settings = {
java = {},
},
-- Language server `initializationOptions`
-- You need to extend the `bundles` with paths to jar files
-- if you want to use additional eclipse.jdt.ls plugins.
--
-- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation
--
-- If you don't plan on using the debugger or other eclipse.jdt.ls plugins you can remove this
init_options = {
bundles = {},
},
}
-- This starts a new client & server,
-- or attaches to an existing client & server depending on the `root_dir`.
require("jdtls").start_or_attach(config)
end,
},
})
@cjgratacos Configuration
java-lsp.sh
#!/usr/bin/env bash
JAR="$HOME/.local/share/lsp/jdtls-server/plugins/org.eclipse.equinox.launcher_*.jar"
GRADLE_HOME='$HOME/.sdkman/candidates/gradle/current/bin/gradle' java \
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044 \
-javaagent:$HOME/.local/jars/lombok.jar \
-Declipse.application=org.eclipse.jdt.ls.core.id1 \
-Dosgi.bundles.defaultStartLevel=4 \
-Declipse.product=org.eclipse.jdt.ls.core.product \
-Dlog.protocol=true \
-Dlog.level=ALL \
-Xms1g \
-Xmx2G \
-jar $(echo "$JAR") \
-configuration "$HOME/.local/share/lsp/jdtls-server/config_linux" \
-data "$1" \
--add-modules=ALL-SYSTEM \
--add-opens java.base/java.util=ALL-UNNAMED \
--add-opens java.base/java.lang=ALL-UNNAMED
jdtls_setup.lua
local M = {}
function M.setup()
local on_attach = function(client, bufnr)
require'jdtls.setup'.add_commands()
require'jdtls'.setup_dap()
require'lsp-status'.register_progress()
require'compe'.setup {
enabled = true;
autocomplete = true;
debug = false;
min_length = 1;
preselect = 'enable';
throttle_time = 80;
source_timeout = 200;
incomplete_delay = 400;
max_abbr_width = 100;
max_kind_width = 100;
max_menu_width = 100;
documentation = true;
source = {
path = true;
buffer = true;
calc = true;
vsnip = false;
nvim_lsp = true;
nvim_lua = true;
spell = true;
tags = true;
snippets_nvim = false;
treesitter = true;
};
}
require'lspkind'.init()
require'lspsaga'.init_lsp_saga()
-- Kommentary
vim.api.nvim_set_keymap("n", "<leader>/", "<plug>kommentary_line_default", {})
vim.api.nvim_set_keymap("v", "<leader>/", "<plug>kommentary_visual_default", {})
require'formatter'.setup{
filetype = {
java = {
function()
return {
exe = 'java',
args = { '-jar', os.getenv('HOME') .. '/.local/jars/google-java-format.jar', vim.api.nvim_buf_get_name(0) },
stdin = true
}
end
}
}
}
vim.api.nvim_exec([[
augroup FormatAutogroup
autocmd!
autocmd BufWritePost *.java FormatWrite
augroup end
]], true)
local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end
local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
-- Mappings.
local opts = { noremap=true, silent=true }
buf_set_keymap('n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
buf_set_keymap('n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
buf_set_keymap('n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
buf_set_keymap('n', '<leader>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
buf_set_keymap('n', '<leader>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
buf_set_keymap('n', '<leader>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
buf_set_keymap('n', '<leader>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
buf_set_keymap('n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references() && vim.cmd("copen")<CR>', opts)
buf_set_keymap('n', '<leader>e', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
buf_set_keymap('n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
buf_set_keymap('n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
buf_set_keymap('n', '<leader>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
-- Java specific
buf_set_keymap("n", "<leader>di", "<Cmd>lua require'jdtls'.organize_imports()<CR>", opts)
buf_set_keymap("n", "<leader>dt", "<Cmd>lua require'jdtls'.test_class()<CR>", opts)
buf_set_keymap("n", "<leader>dn", "<Cmd>lua require'jdtls'.test_nearest_method()<CR>", opts)
buf_set_keymap("v", "<leader>de", "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>", opts)
buf_set_keymap("n", "<leader>de", "<Cmd>lua require('jdtls').extract_variable()<CR>", opts)
buf_set_keymap("v", "<leader>dm", "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>", opts)
buf_set_keymap("n", "<leader>cf", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
vim.api.nvim_exec([[
hi LspReferenceRead cterm=bold ctermbg=red guibg=LightYellow
hi LspReferenceText cterm=bold ctermbg=red guibg=LightYellow
hi LspReferenceWrite cterm=bold ctermbg=red guibg=LightYellow
augroup lsp_document_highlight
autocmd!
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
augroup END
]], false)
end
local root_markers = {'gradlew', 'pom.xml'}
local root_dir = require('jdtls.setup').find_root(root_markers)
local home = os.getenv('HOME')
local capabilities = {
workspace = {
configuration = true
},
textDocument = {
completion = {
completionItem = {
snippetSupport = true
}
}
}
}
local workspace_folder = home .. "/.workspace" .. vim.fn.fnamemodify(root_dir, ":p:h:t")
local config = {
flags = {
allow_incremental_sync = true,
};
capabilities = capabilities,
on_attach = on_attach,
}
config.settings = {
['java.format.settings.url'] = home .. "/.config/nvim/language-servers/java-google-formatter.xml",
['java.format.settings.profile'] = "GoogleStyle",
java = {
signatureHelp = { enabled = true };
contentProvider = { preferred = 'fernflower' };
completion = {
favoriteStaticMembers = {
"org.hamcrest.MatcherAssert.assertThat",
"org.hamcrest.Matchers.*",
"org.hamcrest.CoreMatchers.*",
"org.junit.jupiter.api.Assertions.*",
"java.util.Objects.requireNonNull",
"java.util.Objects.requireNonNullElse",
"org.mockito.Mockito.*"
}
};
sources = {
organizeImports = {
starThreshold = 9999;
staticStarThreshold = 9999;
};
};
codeGeneration = {
toString = {
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}"
}
};
configuration = {
runtimes = {
{
name = "JavaSE-11",
path = home .. "/.sdkman/candidates/java/11.0.10-open/",
},
{
name = "JavaSE-14",
path = home .. "/.sdkman/candidates/java/14.0.2-open/",
},
{
name = "JavaSE-15",
path = home .. "/.sdkman/candidates/java/15.0.1-open/",
},
}
};
};
}
config.cmd = {'java-lsp', workspace_folder}
config.on_attach = on_attach
config.on_init = function(client, _)
client.notify('workspace/didChangeConfiguration', { settings = config.settings })
end
-- local jar_patterns = {
-- '/dev/microsoft/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-*.jar',
-- '/dev/dgileadi/vscode-java-decompiler/server/*.jar',
-- '/dev/microsoft/vscode-java-test/server/*.jar',
-- }
-- local bundles = {}
-- for _, jar_pattern in ipairs(jar_patterns) do
-- for _, bundle in ipairs(vim.split(vim.fn.glob(home .. jar_pattern), '\n')) do
-- if not vim.endswith(bundle, 'com.microsoft.java.test.runner.jar') then
-- table.insert(bundles, bundle)
-- end
-- end
-- end
local extendedClientCapabilities = require'jdtls'.extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true
config.init_options = {
-- bundles = bundles;
extendedClientCapabilities = extendedClientCapabilities;
}
-- UI
local finders = require'telescope.finders'
local sorters = require'telescope.sorters'
local actions = require'telescope.actions'
local pickers = require'telescope.pickers'
require('jdtls.ui').pick_one_async = function(items, prompt, label_fn, cb)
local opts = {}
pickers.new(opts, {
prompt_title = prompt,
finder = finders.new_table {
results = items,
entry_maker = function(entry)
return {
value = entry,
display = label_fn(entry),
ordinal = label_fn(entry),
}
end,
},
sorter = sorters.get_generic_fuzzy_sorter(),
attach_mappings = function(prompt_bufnr)
actions.goto_file_selection_edit:replace(function()
local selection = actions.get_selected_entry(prompt_bufnr)
actions.close(prompt_bufnr)
cb(selection.value)
end)
return true
end,
}):find()
end
-- Server
require('jdtls').start_or_attach(config)
end
return M
init.vim
augroup jdtls_lsp
autocmd!
autocmd FileType java lua require'jdtls_setup'.setup()
augroup end
@mfussenegger Configuration
ftplugin/java.lua
-- Eclipse Java development tools (JDT) Language Server downloaded from:
-- https://www.eclipse.org/downloads/download.php?file=/jdtls/milestones/1.21.0/jdt-language-server-1.21.0-202303161431.tar.gz
local jdtls = require('jdtls')
-- Change or delete this if you don't depend on nvim-cmp for completions.
local cmp_nvim_lsp = require('cmp_nvim_lsp')
-- Change jdtls_path to wherever you have your Eclipse Java development tools (JDT) Language Server downloaded to.
local jdtls_path = vim.fn.stdpath('data') .. '/language-servers/jdt-language-server'
local launcher_jar = vim.fn.glob(jdtls_path .. '/plugins/org.eclipse.equinox.launcher_*.jar')
local workspace_dir = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')
vim.o.tabstop = 4
vim.o.shiftwidth = 0
-- for completions
local client_capabilities = vim.lsp.protocol.make_client_capabilities()
local capabilities = cmp_nvim_lsp.default_capabilities(client_capabilities)
local function get_config_dir()
-- Unlike some other programming languages (e.g. JavaScript)
-- lua considers 0 truthy!
if vim.fn.has('linux') == 1 then
return 'config_linux'
elseif vim.fn.has('mac') == 1 then
return 'config_mac'
else
return 'config_win'
end
end
-- See `:help vim.lsp.start_client` for an overview of the supported `config` options.
local config = {
capabilities = capabilities,
cmd = {
-- This sample path was tested on Cygwin, a "unix-like" Windows environment.
-- Please contribute to this Wiki if this doesn't work for another Windows
-- environment like [Git for Windows](https://gitforwindows.org/) or PowerShell.
-- JDTLS currently needs Java 17 to work, but you can replace this line with "java"
-- if Java 17 is on your PATH.
"C:/Program Files/Java/jdk-17.0.4.1/bin/java",
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
"-Dosgi.bundles.defaultStartLevel=4",
"-Declipse.product=org.eclipse.jdt.ls.core.product",
"-Dlog.protocol=true",
"-Dlog.level=ALL",
"-Xmx1G",
"--add-modules=ALL-SYSTEM",
"--add-opens", "java.base/java.util=ALL-UNNAMED",
"--add-opens", "java.base/java.lang=ALL-UNNAMED",
"-jar", launcher_jar,
"-configuration", vim.fs.normalize(jdtls_path .. '/' .. get_config_dir()),
"-data", vim.fn.expand('~/.cache/jdtls-workspace/') .. workspace_dir
},
settings = {
['java.format.settings.url'] = vim.fn.expand("~/formatter.xml")
},
root_dir = vim.fs.dirname(vim.fs.find({ 'pom.xml', '.git' }, { upward = true })[1]),
init_options = {
-- https://github.com/eclipse/eclipse.jdt.ls/wiki/Language-Server-Settings-&-Capabilities#extended-client-capabilities
extendedClientCapabilities = jdtls.extendedClientCapabilities,
},
on_attach = function(client, bufnr)
-- https://github.com/mfussenegger/dotfiles/blob/833d634251ebf3bf7e9899ed06ac710735d392da/vim/.config/nvim/ftplugin/java.lua#L88-L94
local opts = { silent = true, buffer = bufnr }
vim.keymap.set('n', "<leader>lo", jdtls.organize_imports, { desc = 'Organize imports', buffer = bufnr })
-- Should 'd' be reserved for debug?
vim.keymap.set('n', "<leader>df", jdtls.test_class, opts)
vim.keymap.set('n', "<leader>dn", jdtls.test_nearest_method, opts)
vim.keymap.set('n', '<leader>rv', jdtls.extract_variable_all, { desc = 'Extract variable', buffer = bufnr })
vim.keymap.set('v', '<leader>rm', [[<ESC><CMD>lua require('jdtls').extract_method(true)<CR>]],
{ desc = 'Extract method', buffer = bufnr })
vim.keymap.set('n', '<leader>rc', jdtls.extract_constant, { desc = 'Extract constant', buffer = bufnr })
end
}
jdtls.start_or_attach(config)
@pavva91 Configuration
I'm coming from kickstart so I'm using:
I keep the 💀 for parts that you eventually need to change
lua/jdtls/jdtls_setup.lua
local M = {}
function M.setup()
local jdtls = require("jdtls")
local jdtls_dap = require("jdtls.dap")
local jdtls_setup = require("jdtls.setup")
local home = os.getenv("HOME")
local root_markers = { ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" }
local root_dir = jdtls_setup.find_root(root_markers)
local project_name = vim.fn.fnamemodify(root_dir, ":p:h:t")
local workspace_dir = home .. "/.cache/jdtls/workspace" .. project_name
-- 💀
local path_to_mason_packages = home .. "/.local/share/nvim/mason/packages"
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
local path_to_jdtls = path_to_mason_packages .. "/jdtls"
local path_to_jdebug = path_to_mason_packages .. "/java-debug-adapter"
local path_to_jtest = path_to_mason_packages .. "/java-test"
local path_to_config = path_to_jdtls .. "/config_linux"
local lombok_path = path_to_jdtls .. "/lombok.jar"
-- 💀
local path_to_jar = path_to_jdtls .. "/plugins/org.eclipse.equinox.launcher_1.6.500.v20230717-2134.jar"
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
local bundles = {
vim.fn.glob(path_to_jdebug .. "/extension/server/com.microsoft.java.debug.plugin-*.jar", true),
}
vim.list_extend(bundles, vim.split(vim.fn.glob(path_to_jtest .. "/extension/server/*.jar", true), "\n"))
-- LSP settings for Java.
local on_attach = function(_, bufnr)
jdtls.setup_dap({ hotcodereplace = "auto" })
jdtls_dap.setup_dap_main_class_configs()
jdtls_setup.add_commands()
-- Create a command `:Format` local to the LSP buffer
vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
vim.lsp.buf.format()
end, { desc = "Format current buffer with LSP" })
require("lsp_signature").on_attach({
bind = true,
padding = "",
handler_opts = {
border = "rounded",
},
hint_prefix = " ",
}, bufnr)
-- NOTE: comment out if you don't use Lspsaga
require 'lspsaga'.init_lsp_saga()
end
local capabilities = {
workspace = {
configuration = true
},
textDocument = {
completion = {
completionItem = {
snippetSupport = true
}
}
}
}
local config = {
flags = {
allow_incremental_sync = true,
}
}
config.cmd = {
--
-- -- 💀
"java", -- or '/path/to/java17_or_newer/bin/java'
-- depends on if `java` is in your $PATH env variable and if it points to the right version.
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
"-Dosgi.bundles.defaultStartLevel=4",
"-Declipse.product=org.eclipse.jdt.ls.core.product",
"-Dlog.protocol=true",
"-Dlog.level=ALL",
"-Xmx1g",
"-javaagent:" .. lombok_path,
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
-- 💀
"-jar",
path_to_jar,
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
-- Must point to the Change this to
-- eclipse.jdt.ls installation the actual version
-- 💀
"-configuration",
path_to_config,
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^
-- Must point to the Change to one of `linux`, `win` or `mac`
-- eclipse.jdt.ls installation Depending on your system.
-- 💀
-- See `data directory configuration` section in the README
"-data",
workspace_dir,
}
config.settings = {
java = {
references = {
includeDecompiledSources = true,
},
format = {
enabled = true,
settings = {
url = vim.fn.stdpath("config") .. "/lang_servers/intellij-java-google-style.xml",
profile = "GoogleStyle",
},
},
eclipse = {
downloadSources = true,
},
maven = {
downloadSources = true,
},
signatureHelp = { enabled = true },
contentProvider = { preferred = "fernflower" },
-- eclipse = {
-- downloadSources = true,
-- },
-- implementationsCodeLens = {
-- enabled = true,
-- },
completion = {
favoriteStaticMembers = {
"org.hamcrest.MatcherAssert.assertThat",
"org.hamcrest.Matchers.*",
"org.hamcrest.CoreMatchers.*",
"org.junit.jupiter.api.Assertions.*",
"java.util.Objects.requireNonNull",
"java.util.Objects.requireNonNullElse",
"org.mockito.Mockito.*",
},
filteredTypes = {
"com.sun.*",
"io.micrometer.shaded.*",
"java.awt.*",
"jdk.*",
"sun.*",
},
importOrder = {
"java",
"javax",
"com",
"org",
},
},
sources = {
organizeImports = {
starThreshold = 9999,
staticStarThreshold = 9999,
},
},
codeGeneration = {
toString = {
template = "${object.className}{${member.name()}=${member.value}, ${otherMembers}}",
-- flags = {
-- allow_incremental_sync = true,
-- },
},
useBlocks = true,
},
-- configuration = {
-- runtimes = {
-- {
-- name = "java-17-openjdk",
-- path = "/usr/lib/jvm/default-runtime/bin/java"
-- }
-- }
-- }
-- project = {
-- referencedLibraries = {
-- "**/lib/*.jar",
-- },
-- },
},
}
config.on_attach = on_attach
config.capabilities = capabilities
config.on_init = function(client, _)
client.notify('workspace/didChangeConfiguration', { settings = config.settings })
end
local extendedClientCapabilities = require 'jdtls'.extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true
config.init_options = {
bundles = bundles,
extendedClientCapabilities = extendedClientCapabilities,
}
-- Start Server
require('jdtls').start_or_attach(config)
-- Set Java Specific Keymaps
require("jdtls.keymaps")
end
return M
lua/jdtls/keymaps.lua
-- NOTE: Java specific keymaps with which key
vim.cmd(
"command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_compile JdtCompile lua require('jdtls').compile(<f-args>)"
)
vim.cmd(
"command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime(<f-args>)"
)
vim.cmd("command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()")
vim.cmd("command! -buffer JdtJol lua require('jdtls').jol()")
vim.cmd("command! -buffer JdtBytecode lua require('jdtls').javap()")
vim.cmd("command! -buffer JdtJshell lua require('jdtls').jshell()")
local status_ok, which_key = pcall(require, "which-key")
if not status_ok then
return
end
local opts = {
mode = "n", -- NORMAL mode
prefix = "<leader>",
buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
silent = true, -- use `silent` when creating keymaps
noremap = true, -- use `noremap` when creating keymaps
nowait = true, -- use `nowait` when creating keymaps
}
local vopts = {
mode = "v", -- VISUAL mode
prefix = "<leader>",
buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings
silent = true, -- use `silent` when creating keymaps
noremap = true, -- use `noremap` when creating keymaps
nowait = true, -- use `nowait` when creating keymaps
}
local mappings = {
J = {
name = "Java",
o = { "<Cmd>lua require'jdtls'.organize_imports()<CR>", "Organize Imports" },
v = { "<Cmd>lua require('jdtls').extract_variable()<CR>", "Extract Variable" },
c = { "<Cmd>lua require('jdtls').extract_constant()<CR>", "Extract Constant" },
t = { "<Cmd>lua require'jdtls'.test_nearest_method()<CR>", "Test Method" },
T = { "<Cmd>lua require'jdtls'.test_class()<CR>", "Test Class" },
u = { "<Cmd>JdtUpdateConfig<CR>", "Update Config" },
},
}
local vmappings = {
J = {
name = "Java",
v = { "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>", "Extract Variable" },
c = { "<Esc><Cmd>lua require('jdtls').extract_constant(true)<CR>", "Extract Constant" },
m = { "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>", "Extract Method" },
},
}
which_key.register(mappings, opts)
which_key.register(vmappings, vopts)
-- If you want you can add here Old School Mappings. Me I setup Telescope, LSP and Lspsaga mapping somewhere else and I just reuse them
-- vim.keymap.set("gI", vim.lsp.buf.implementation,{ desc = "[G]oto [I]mplementation" })
-- vim.keymap.set("<leader>D", vim.lsp.buf.type_definition,{ desc = "Type [D]efinition" })
-- vim.keymap.set("<leader>hh", vim.lsp.buf.signature_help,{ desc = "Signature [H][H]elp Documentation" })
-- vim.keymap.set("gD", vim.lsp.buf.declaration,{ desc = "[G]oto [D]eclaration" })
-- vim.keymap.set("<leader>wa", vim.lsp.buf.add_workspace_folder,{ desc = "[W]orkspace [A]dd Folder" })
-- vim.keymap.set("<leader>wr", vim.lsp.buf.remove_workspace_folder,{ desc = "[W]orkspace [R]emove Folder" })
-- vim.keymap.set("<leader>wl", function()
-- print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
-- end, "[W]orkspace [L]ist Folders")
-- Create a command `:Format` local to the LSP buffer
-- vim.api.nvim_buf_create_user_command(bufnr, "Format", function(_)
-- vim.lsp.buf.format()
-- end, { desc = "Format current buffer with LSP" })
-- vim.keymap.set("n", "gr", vim.lsp.buf.references, { desc = "[G]oto [R]eferences - Java", expr = true, silent = true })
-- vim.keymap.set("n","gr", require("telescope.builtin").lsp_references,{ desc = "[G]oto [R]eferences" })
-- vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, { desc = "" })
-- vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { desc = "" })
-- vim.keymap.set('n', 'K', vim.lsp.buf.hover, { desc = "" })
-- vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, { desc = "" })
-- vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, { desc = "" })
-- vim.keymap.set('n', '<leader>wa', vim.lsp.buf.add_workspace_folder, { desc = "" })
-- vim.keymap.set('n', '<leader>wr', vim.lsp.buf.remove_workspace_folder, { desc = "" })
-- vim.keymap.set('n', '<leader>wl', print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', { desc = "" })
-- vim.keymap.set('n', '<leader>D', vim.lsp.buf.type_definition, { desc = "" })
-- vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, { desc = "" })
-- vim.keymap.set('n', 'gr', vim.lsp.buf.references() && vim.cmd("copen")<CR>', { desc = "" })
-- vim.keymap.set('n', '<leader>e', vim.lsp.diagnostic.show_line_diagnostics, { desc = "" })
-- vim.keymap.set('n', '[d', vim.lsp.diagnostic.goto_prev, { desc = "" })
-- vim.keymap.set('n', ']d', vim.lsp.diagnostic.goto_next, { desc = "" })
-- vim.keymap.set('n', '<leader>q', vim.lsp.diagnostic.set_loclist, { desc = "" })
-- -- Java specific
-- vim.keymap.set("n", "<leader>di", "<Cmd>lua require'jdtls'.organize_imports()<CR>", { desc = "" })
-- vim.keymap.set("n", "<leader>dt", "<Cmd>lua require'jdtls'.test_class()<CR>", { desc = "" })
-- vim.keymap.set("n", "<leader>dn", "<Cmd>lua require'jdtls'.test_nearest_method()<CR>", { desc = "" })
-- vim.keymap.set("v", "<leader>de", "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>", { desc = "" })
-- vim.keymap.set("n", "<leader>de", "<Cmd>lua require('jdtls').extract_variable()<CR>", { desc = "" })
-- vim.keymap.set("v", "<leader>dm", "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>", { desc = "" })
--
-- vim.keymap.set("n", "<leader>cf", "<cmd>lua vim.lsp.buf.formatting()<CR>", { desc = "" })
init.lua
- Import plugin with Lazy:
require("lazy").setup({
{
"mfussenegger/nvim-jdtls",
},
}, {})
- Create autocmd to run on every buffer with filetype java
vim.cmd [[
augroup jdtls_lsp
autocmd!
autocmd FileType java lua require'jdtls.jdtls_setup'.setup()
augroup end
]]
@qmi03 Configuration
-
lua/plugins/java.lua.
You can see my full setup with other plugins at this repo.
I'm using: