/ nvim / lua / lsp / servers / java.lua
java.lua
 1  return {
 2    {
 3      'neovim/nvim-lspconfig',
 4      opts = function()
 5        return {
 6          -- How to find the root dir for a given file name. The default comes from
 7          -- lspconfig which provides a function specifically for java projects.
 8          root_dir = require('lspconfig.server_configurations.jdtls').default_config.root_dir,
 9  
10          -- How to find the project name for a given root dir.
11          project_name = function(root_dir)
12            return root_dir and vim.fs.basename(root_dir)
13          end,
14  
15          -- Where are the config and workspace dirs for a project?
16          jdtls_config_dir = function(project_name)
17            return vim.fn.stdpath('cache') .. '/jdtls/' .. project_name .. '/config'
18          end,
19          jdtls_workspace_dir = function(project_name)
20            return vim.fn.stdpath('cache') .. '/jdtls/' .. project_name .. '/workspace'
21          end,
22  
23          -- How to run jdtls. This can be overridden to a full java command-line
24          -- if the Python wrapper script doesn't suffice.
25          cmd = { vim.fn.exepath('jdtls') },
26          full_cmd = function(opts)
27            local fname = vim.api.nvim_buf_get_name(0)
28            local root_dir = opts.root_dir(fname)
29            local project_name = opts.project_name(root_dir)
30            local cmd = vim.deepcopy(opts.cmd)
31            if project_name then
32              vim.list_extend(cmd, {
33                '-configuration',
34                opts.jdtls_config_dir(project_name),
35                '-data',
36                opts.jdtls_workspace_dir(project_name),
37              })
38            end
39            return cmd
40          end,
41  
42          -- These depend on nvim-dap, but can additionally be disabled by setting false here.
43          dap = { hotcodereplace = 'auto', config_overrides = {} },
44          dap_main = {},
45          test = true,
46          settings = {
47            java = {
48              inlayHints = {
49                parameterNames = {
50                  enabled = 'all',
51                },
52              },
53            },
54          },
55          setup = {
56            java = function() end,
57          },
58        }
59      end,
60    },
61  }