|
1 | | --- ~/.config/nvim/ftplugin/java.lua |
2 | | --- helper function for checking if a table contains a value |
3 | | -local function contains(table, value) |
4 | | - for _, table_value in ipairs(table) do |
5 | | - if table_value == value then |
6 | | - return true |
7 | | - end |
8 | | - end |
| 1 | +vim.opt_local.tabstop = 4 |
| 2 | +vim.opt_local.softtabstop = 4 |
| 3 | +vim.opt_local.shiftwidth = 4 |
9 | 4 |
|
10 | | - return false |
11 | | -end |
12 | | - |
13 | | --- helper function for finding a filename in a directory which matches |
14 | | --- the specified pattern |
15 | | -local function find_file(directory, pattern) |
16 | | - local filename_found = '' |
17 | | - local pfile = io.popen('ls "' .. directory .. '"') |
18 | | - |
19 | | - if (pfile == nil) then |
20 | | - return '' |
21 | | - end |
22 | | - |
23 | | - for filename in pfile:lines() do |
24 | | - if (string.find(filename, pattern) ~= nil) then |
25 | | - filename_found = filename |
26 | | - break |
27 | | - end |
28 | | - end |
29 | | - |
30 | | - return filename_found |
31 | | -end |
32 | | - |
33 | | --- gathers all of the bemol-generated files and adds them to the LSP workspace |
34 | | -local function bemol() |
35 | | - local bemol_dir = vim.fs.find({ ".bemol" }, { upward = true, type = "directory" })[1] |
36 | | - local ws_folders_lsp = {} |
37 | | - if bemol_dir then |
38 | | - local file = io.open(bemol_dir .. "/ws_root_folders", "r") |
39 | | - if file then |
40 | | - for line in file:lines() do |
41 | | - table.insert(ws_folders_lsp, line) |
42 | | - end |
43 | | - file:close() |
44 | | - end |
45 | | - |
46 | | - for _, line in ipairs(ws_folders_lsp) do |
47 | | - if not contains(vim.lsp.buf.list_workspace_folders(), line) then |
48 | | - vim.lsp.buf.add_workspace_folder(line) |
49 | | - end |
50 | | - end |
51 | | - end |
52 | | -end |
53 | | - |
54 | | -local jdtls = require "jdtls" |
55 | | -local jdtls_setup = require "jdtls.setup" |
| 5 | +local jdtls = require("jdtls") |
| 6 | +local jdtls_setup = require("jdtls.setup") |
56 | 7 |
|
57 | 8 | local home = os.getenv("HOME") |
58 | | -local root_markers = { ".bemol", } |
59 | | -local root_dir = jdtls_setup.find_root(root_markers) |
| 9 | +local root_dir = jdtls_setup.find_root({ ".git", "build.gradle", "build.gradle.kts", "pom.xml", ".bemol" }) |
60 | 10 | local project_name = vim.fn.fnamemodify(root_dir, ":p:h:t") |
61 | 11 | local workspace_dir = home .. "/.cache/jdtls/workspace/" .. project_name |
62 | | -local path_to_mason_packages = home .."/.local/share/nvim/mason/packages" |
63 | | -local path_to_jdtls = path_to_mason_packages .. "/jdtls" |
64 | | -local os_type = vim.fn.has("macunix") and "mac" or "linux" |
65 | | -local path_to_config = path_to_jdtls .. "/config_" .. os_type |
66 | | -local path_to_lombok = path_to_jdtls .. "/lombok.jar" |
67 | | -local path_to_plugins = path_to_jdtls .. "/plugins/" |
68 | | --- the eclipse jar is suffixed with a bunch of version nonsense, so we find it by pattern matching |
69 | | -local path_to_jar = path_to_plugins .. find_file(path_to_plugins, "org.eclipse.equinox.launcher_") |
| 12 | +local path_to_jdtls = home .. "/.local/share/nvim/mason/packages/jdtls" |
| 13 | +local os_type = vim.fn.has("macunix") == 1 and "mac" or "linux" |
| 14 | +local path_to_jar = vim.fs.find(function(name) |
| 15 | + return name:match("org.eclipse.equinox.launcher_.*%.jar$") |
| 16 | +end, { path = path_to_jdtls .. "/plugins/", type = "file" })[1] |
70 | 17 |
|
71 | 18 | local config = { |
72 | | - cmd = { |
73 | | - -- assumes the java binary is in your PATH and at least java17; |
74 | | - -- if not, specify the full path to the binary |
75 | | - "java", |
76 | | - "-Declipse.application=org.eclipse.jdt.ls.core.id1", |
77 | | - "-Dosgi.bundles.defaultStartLevel=4", |
78 | | - "-Declipse.product=org.eclipse.jdt.ls.core.product", |
79 | | - "-Dlog.protocol=true", |
80 | | - "-Dlog.level=ALL", |
81 | | - "-Xmx1g", |
82 | | - "-javaagent:" .. path_to_lombok, |
83 | | - -- "-add-opens", |
84 | | - -- "java.base/java.util=ALL-UNNAMED", |
85 | | - -- "java.base/java.lang=ALL-UNNAMED", |
86 | | - |
87 | | - "-jar", |
88 | | - path_to_jar, |
89 | | - |
90 | | - "-configuration", |
91 | | - path_to_config, |
92 | | - |
93 | | - "-data", |
94 | | - workspace_dir, |
95 | | - }, |
96 | | - |
97 | | - root_dir = root_dir, |
98 | | - |
99 | | - capabilities = { |
100 | | - workspace = { |
101 | | - configuration = true |
102 | | - }, |
103 | | - textDocument = { |
104 | | - completion = { |
105 | | - completionItem = { |
106 | | - snippetSupport = true |
107 | | - } |
108 | | - } |
109 | | - } |
110 | | - }, |
111 | | - |
112 | | - settings = { |
113 | | - java = { |
114 | | - references = { |
115 | | - includeDecompiledSources = true, |
116 | | - }, |
117 | | - eclipse = { |
118 | | - downloadSources = true, |
119 | | - }, |
120 | | - maven = { |
121 | | - downloadSources = true, |
122 | | - }, |
123 | | - sources = { |
124 | | - organizeImports = { |
125 | | - starThreshold = 9999, |
126 | | - staticStarThreshold = 9999, |
127 | | - }, |
128 | | - }, |
129 | | - } |
130 | | - }, |
131 | | - |
132 | | - -- run our bemol function when the LSP attaches to the buffer |
133 | | - on_attach = bemol, |
| 19 | + cmd = { |
| 20 | + "java", |
| 21 | + "-Declipse.application=org.eclipse.jdt.ls.core.id1", |
| 22 | + "-Dosgi.bundles.defaultStartLevel=4", |
| 23 | + "-Declipse.product=org.eclipse.jdt.ls.core.product", |
| 24 | + "-Dlog.protocol=true", |
| 25 | + "-Dlog.level=ALL", |
| 26 | + "-Xmx1g", |
| 27 | + "-javaagent:" .. path_to_jdtls .. "/lombok.jar", |
| 28 | + "--add-modules=ALL-SYSTEM", |
| 29 | + "--add-opens", "java.base/java.util=ALL-UNNAMED", |
| 30 | + "--add-opens", "java.base/java.lang=ALL-UNNAMED", |
| 31 | + "-jar", path_to_jar, |
| 32 | + "-configuration", path_to_jdtls .. "/config_" .. os_type, |
| 33 | + "-data", workspace_dir, |
| 34 | + }, |
| 35 | + root_dir = root_dir, |
| 36 | + capabilities = { |
| 37 | + workspace = { configuration = true }, |
| 38 | + textDocument = { completion = { completionItem = { snippetSupport = true } } }, |
| 39 | + }, |
| 40 | + settings = { |
| 41 | + java = { |
| 42 | + references = { includeDecompiledSources = true }, |
| 43 | + eclipse = { downloadSources = true }, |
| 44 | + maven = { downloadSources = true }, |
| 45 | + sources = { |
| 46 | + organizeImports = { starThreshold = 9999, staticStarThreshold = 9999 }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
134 | 50 | } |
135 | 51 |
|
136 | 52 | jdtls.start_or_attach(config) |
0 commit comments