-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathjava.lua
More file actions
425 lines (395 loc) · 13.5 KB
/
Copy pathjava.lua
File metadata and controls
425 lines (395 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
local M = {}
local env = {
HOME = vim.env["HOME"],
JAVA_HOME = vim.env["JAVA_HOME"],
JDTLS_RUN_JAVA = vim.env["JDTLS_RUN_JAVA"],
JDTLS_HOME = vim.env["JDTLS_HOME"],
JDTLS_WORKSPACE = vim.env["JDTLS_WORKSPACE"],
JOL_JAR = vim.env["JOL_JAR"],
}
local maven = require("kide.core.utils.maven")
local jdtls_java = (function()
local jdtls_run_java = env.JDTLS_RUN_JAVA
if jdtls_run_java then
return jdtls_run_java
end
local java_home = env.JAVA_HOME
if java_home then
return java_home .. "/bin/java"
end
return "java"
end)()
local function or_default(a, v)
return require("kide.core.utils").or_default(a, v)
end
local function get_java_home()
return or_default(env.JAVA_HOME, "/opt/software/java/zulu17.34.19-ca-jdk17.0.3-macosx_aarch64")
end
local function get_java_ver_home(v, dv)
return vim.env["JAVA_" .. v .. "_HOME"] or dv
end
local function get_java_ver_sources(v, dv)
return vim.env["JAVA_" .. v .. "_SOURCES"] or dv
end
local function get_jdtls_workspace()
return or_default(env.JDTLS_WORKSPACE, env.HOME .. "/.jdtls-workspace/")
end
local vscode = require("kide.core.vscode")
local function get_jol_jar()
return env.JOL_JAR or "/opt/software/java/jol-cli-0.16-full.jar"
end
-- see https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
local ExecutionEnvironment = {
J2SE_1_5 = "J2SE-1.5",
JavaSE_1_6 = "JavaSE-1.6",
JavaSE_1_7 = "JavaSE-1.7",
JavaSE_1_8 = "JavaSE-1.8",
JavaSE_9 = "JavaSE-9",
JavaSE_10 = "JavaSE-10",
JavaSE_11 = "JavaSE-11",
JavaSE_12 = "JavaSE-12",
JavaSE_13 = "JavaSE-13",
JavaSE_14 = "JavaSE-14",
JavaSE_15 = "JavaSE-15",
JavaSE_16 = "JavaSE-16",
JavaSE_17 = "JavaSE-17",
JavaSE_18 = "JavaSE-18",
JavaSE_19 = "JavaSE-19",
}
local function fglob(path)
if path == "" then
return nil
end
return path
end
local runtimes = (function()
local result = {}
for _, value in pairs(ExecutionEnvironment) do
local version = vim.fn.split(value, "-")[2]
if string.match(version, "%.") then
version = vim.split(version, "%.")[2]
end
local java_home = get_java_ver_home(version)
local default_jdk = false
if java_home then
local java_sources = get_java_ver_sources(
version,
fglob(vim.fn.glob(java_home .. "/src.zip")) or fglob(vim.fn.glob(java_home .. "/lib/src.zip"))
)
if ExecutionEnvironment.JavaSE_1_8 == value then
default_jdk = true
end
table.insert(result, {
name = value,
path = java_home,
sources = java_sources,
default = default_jdk,
})
end
end
if #result == 0 then
vim.notify("Please config Java runtimes (JAVA_8_HOME...)")
end
return result
end)()
-- local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local root_dir = require("jdtls.setup").find_root({ ".git", "mvnw", "gradlew" })
local rwdir = root_dir or vim.fn.getcwd()
local workspace_dir = get_jdtls_workspace() .. require("kide.core.utils.md5").sumhexa(rwdir)
-- local jdtls_path = vscode.find_one("/redhat.java-*/server")
local function get_jdtls_path()
return or_default(env.JDTLS_HOME, vscode.find_one("/redhat.java-*/server"))
end
local function jdtls_launcher()
local jdtls_path = get_jdtls_path()
if jdtls_path then
elseif require("mason-registry").has_package("jdtls") then
jdtls_path = require("mason-registry").get_package("jdtls"):get_install_path()
end
local utils = require("kide.core.utils")
local jdtls_config = nil
if utils.is_mac then
jdtls_config = "/config_mac"
elseif utils.is_linux then
jdtls_config = "/config_linux"
elseif utils.is_win then
jdtls_config = "/config_win"
else
vim.notify("jdtls: unknown os", vim.log.levels.ERROR)
return nil
end
local lombok_jar = vscode.get_lombok_jar()
local cmd = {
jdtls_java,
"-Declipse.application=org.eclipse.jdt.ls.core.id1",
"-Dosgi.bundles.defaultStartLevel=4",
"-Declipse.product=org.eclipse.jdt.ls.core.product",
"-Dosgi.checkConfiguration=true",
"-Dosgi.sharedConfiguration.area=" .. vim.fn.glob(jdtls_path .. jdtls_config),
"-Dosgi.sharedConfiguration.area.readOnly=true",
"-Dosgi.configuration.cascaded=true",
"-Dlog.protocol=true",
"-Dlog.level=ALL",
"-Xmx4g",
"-XX:+UseZGC",
"--add-modules=ALL-SYSTEM",
"--add-opens",
"java.base/java.util=ALL-UNNAMED",
"--add-opens",
"java.base/java.lang=ALL-UNNAMED",
}
if lombok_jar ~= nil then
table.insert(cmd, "-javaagent:" .. lombok_jar)
end
table.insert(cmd, "-jar")
table.insert(cmd, vim.fn.glob(jdtls_path .. "/plugins/org.eclipse.equinox.launcher_*.jar"))
table.insert(cmd, "-data")
table.insert(cmd, workspace_dir)
return cmd
end
local bundles = {}
-- This bundles definition is the same as in the previous section (java-debug installation)
local vscode_java_debug_path = (function()
local p = vscode.find_one("/vscjava.vscode-java-debug-*/server")
if p then
return p
end
if require("mason-registry").has_package("java-debug-adapter") then
return require("mason-registry").get_package("java-debug-adapter"):get_install_path() .. "/extension/server"
end
end)()
if vscode_java_debug_path then
vim.list_extend(
bundles,
vim.split(vim.fn.glob(vscode_java_debug_path .. "/com.microsoft.java.debug.plugin-*.jar"), "\n")
)
end
-- /opt/software/lsp/java/vscode-java-test/server
-- vim.list_extend(bundles, vim.split(vim.fn.glob("/opt/software/lsp/java/vscode-java-test/server/*.jar"), "\n"));
local vscode_java_test_path = (function()
local p = vscode.find_one("/vscjava.vscode-java-test-*/server")
if p then
return p
end
if require("mason-registry").has_package("java-test") then
return require("mason-registry").get_package("java-test"):get_install_path() .. "/extension/server"
end
end)()
if vscode_java_test_path then
for _, bundle in ipairs(vim.split(vim.fn.glob(vscode_java_test_path .. "/*.jar"), "\n")) do
if not vim.endswith(bundle, "com.microsoft.java.test.runner-jar-with-dependencies.jar") then
table.insert(bundles, bundle)
end
end
end
-- /opt/software/lsp/java/vscode-java-decompiler/server/
local java_decoompiler_path = vscode.find_one("/dgileadi.java-decompiler-*/server")
if java_decoompiler_path then
vim.list_extend(bundles, vim.split(vim.fn.glob(java_decoompiler_path .. "/*.jar"), "\n"))
end
-- /opt/software/lsp/java/vscode-java-dependency/jdtls.ext/
-- vim.list_extend(bundles, vim.split(vim.fn.glob("/opt/software/lsp/java/vscode-java-dependency/jdtls.ext/com.microsoft.jdtls.ext.core/target/com.microsoft.jdtls.ext.core-*.jar"), "\n"));
-- /opt/software/lsp/java/vscode-java-dependency/server/
local java_dependency_path = vscode.find_one("/vscjava.vscode-java-dependency-*/server")
if java_dependency_path then
vim.list_extend(bundles, vim.split(vim.fn.glob(java_dependency_path .. "/*.jar"), "\n"))
end
local vscode_pde_path = vscode.find_one("/yaozheng.vscode-pde-*/server")
if vscode_pde_path then
vim.list_extend(bundles, vim.split(vim.fn.glob(vscode_pde_path .. "/*.jar"), "\n"))
end
-- vim.notify("SETUP: " .. vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf()), vim.log.levels.INFO)
-- See `:help vim.lsp.start_client` for an overview of the supported `config` options.
local config = {
-- The command that starts the language server
-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line
cmd = jdtls_launcher(),
filetypes = { "java" },
root_dir = root_dir,
-- 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 = {
maxConcurrentBuilds = 8,
home = get_java_home(),
project = {
encoding = "UTF-8",
},
foldingRange = { enabled = true },
selectionRange = { enabled = true },
import = {
gradle = { enabled = true },
maven = { enabled = true },
exclusions = {
"**/node_modules/**",
"**/.metadata/**",
"**/archetype-resources/**",
"**/META-INF/maven/**",
"**/.git/**",
},
},
inlayhints = {
parameterNames = { enabled = true },
},
autobuild = { enabled = true },
referenceCodeLens = { enabled = true },
implementationsCodeLens = { enabled = true },
templates = {
fileHeader = {
"/**",
" * ${type_name}",
" * @author ${user}",
" */",
},
typeComment = {
"/**",
" * ${type_name}",
" * @author ${user}",
" */",
},
},
eclipse = {
downloadSources = true,
},
maven = {
downloadSources = true,
updateSnapshots = true,
},
signatureHelp = {
enabled = true,
description = {
enabled = true,
},
},
contentProvider = { preferred = "fernflower" },
completion = {
favoriteStaticMembers = {
"org.assertj.core.api.Assertions.assertThat",
"org.assertj.core.api.Assertions.assertThatThrownBy",
"org.assertj.core.api.Assertions.assertThatExceptionOfType",
"org.assertj.core.api.Assertions.catchThrowable",
"java.util.Objects.requireNonNull",
"java.util.Objects.requireNonNullElse",
"org.mockito.Mockito.*",
},
filteredTypes = {
"com.sun.*",
"io.micrometer.shaded.*",
"java.awt.*",
"org.graalvm.*",
"jdk.*",
"sun.*",
},
},
sources = {
organizeImports = {
starThreshold = 9999,
staticStarThreshold = 9999,
},
},
configuration = {
maven = {
userSettings = maven.get_maven_settings(),
globalSettings = maven.get_maven_settings(),
},
runtimes = runtimes,
},
},
},
-- 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 = {
-- vim.fn.glob("/opt/software/lsp/java/java-debug/com.microsoft.java.debug.plugin/target/com.microsoft.java.debug.plugin-0.35.0.jar")
-- },
-- workspace = workspace_dir
-- },
}
local jdtls = require("jdtls")
jdtls.jol_path = get_jol_jar()
local extendedClientCapabilities = jdtls.extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true
extendedClientCapabilities.progressReportProvider = false
config["init_options"] = {
bundles = bundles,
extendedClientCapabilities = extendedClientCapabilities,
}
config["on_attach"] = function(client, buffer)
-- client.server_capabilities.semanticTokensProvider = nil
-- With `hotcodereplace = 'auto' the debug adapter will try to apply code changes
-- you make during a debug session immediately.
-- Remove the option if you do not want that.
require("jdtls").setup_dap({ hotcodereplace = "auto" })
require("jdtls.setup").add_commands()
-- TODO: 不知道为什么这个值一会有一会没有
client.server_capabilities["definitionProvider"] = true
-- require('jdtls.dap').setup_dap_main_class_configs({ verbose = true })
local opts = { silent = true, buffer = buffer }
vim.keymap.set("n", "<leader>dc", jdtls.test_class, opts)
vim.keymap.set("n", "<leader>dm", jdtls.test_nearest_method, opts)
vim.keymap.set("n", "<leader>ds", jdtls.pick_test, opts)
vim.keymap.set("n", "crv", jdtls.extract_variable, opts)
vim.keymap.set("v", "crm", [[<ESC><CMD>lua require('jdtls').extract_method(true)<CR>]], opts)
vim.keymap.set("n", "crc", jdtls.extract_constant, opts)
local create_command = vim.api.nvim_buf_create_user_command
create_command(buffer, "OR", require("jdtls").organize_imports, {
nargs = 0,
})
require("java-deps").attach(client, buffer, root_dir)
create_command(buffer, "JavaProjects", require("java-deps").toggle_outline, {
nargs = 0,
})
-- vim.notify(vim.api.nvim_buf_get_name(bufnr), vim.log.levels.INFO)
end
local capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities())
capabilities.textDocument.foldingRange = {
dynamicRegistration = false,
lineFoldingOnly = true,
}
-- capabilities.experimental = {
-- hoverActions = true,
-- hoverRange = true,
-- serverStatusNotification = true,
-- snippetTextEdit = true,
-- codeActionGroup = true,
-- ssr = true,
-- }
config.capabilities = capabilities
config.flags = {
debounce_text_changes = 150,
}
config.handlers = {}
config.handlers["language/status"] = function(_, s)
-- 使用 progress 查看状态
-- print("jdtls " .. s.type .. ": " .. s.message)
if "ServiceReady" == s.type then
require("jdtls.dap").setup_dap_main_class_configs({ verbose = true })
end
end
M.config = config
M.start = function(_)
jdtls.start_or_attach(config)
end
M.setup = function(opts)
require("kide.lsp.utils.jdtls").customize_jdtls()
local group = vim.api.nvim_create_augroup("kide_jdtls_java", { clear = true })
vim.api.nvim_create_autocmd({ "FileType" }, {
group = group,
pattern = { "java" },
desc = "jdtls",
callback = function(e)
if e.file == "java" and vim.bo[e.buf].buftype == "nofile" then
-- ignore
else
M.start(opts)
end
end,
})
end
return M