Skip to content

Commit 2f5271b

Browse files
committed
fix: improved java/kt lang support
1 parent 35d82fa commit 2f5271b

5 files changed

Lines changed: 53 additions & 131 deletions

File tree

Lines changed: 42 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,52 @@
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
94

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")
567

578
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" })
6010
local project_name = vim.fn.fnamemodify(root_dir, ":p:h:t")
6111
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]
7017

7118
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+
},
13450
}
13551

13652
jdtls.start_or_attach(config)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vim.opt_local.tabstop = 4
2+
vim.opt_local.softtabstop = 4
3+
vim.opt_local.shiftwidth = 4

roles/neovim/files/lua/plugins/conform.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ return {
1212
opts = {
1313
formatters_by_ft = {
1414
lua = { "stylua" },
15-
-- Conform will run multiple formatters sequentially
1615
python = { "isort", "black" },
17-
-- Use a sub-list to run only the first available formatter
1816
javascript = { "prettier" },
1917
typescript = { "prettier" },
2018
json = { "prettier" },
19+
java = { "google-java-format" },
20+
kotlin = { "ktlint" },
2121
},
2222
},
2323
}

roles/neovim/files/lua/plugins/lspconfig.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ return {
6060

6161
mason.setup()
6262

63-
local mason_ensure_installed =
64-
{ "ts_ls", "templ", "rust_analyzer", "lua_ls", "stylua", "jdtls", "kotlin-language-server", "codelldb" }
63+
local mason_ensure_installed = {
64+
"ts_ls", "templ", "rust_analyzer", "lua_ls", "stylua",
65+
"jdtls", "kotlin-language-server", "ktlint", "google-java-format",
66+
"codelldb",
67+
}
6568

6669
mason_tool_installer.setup({
6770
ensure_installed = mason_ensure_installed,

roles/neovim/files/lua/plugins/treesitter.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ return {
77
build = ":TSUpdate",
88
opts = {
99
ensure_installed = {
10-
"vimdoc", "go", "lua", "java", "rust", "typescript",
10+
"vimdoc", "go", "lua", "java", "kotlin", "rust", "typescript",
1111
"javascript", "json", "yaml", "toml", "markdown", "bash", "html", "css",
1212
},
1313
sync_install = false,

0 commit comments

Comments
 (0)