|
| 1 | +local M = {} |
| 2 | + |
| 3 | +function M.setup() |
| 4 | + vim.api.nvim_create_autocmd("FileType", { |
| 5 | + pattern = "java", |
| 6 | + callback = function() |
| 7 | + local jdtls = require("jdtls") |
| 8 | + |
| 9 | + local root_dir = require("jdtls.setup").find_root({ |
| 10 | + ".classpath", |
| 11 | + ".project", |
| 12 | + "pom.xml", |
| 13 | + "build.gradle", |
| 14 | + "gradlew", |
| 15 | + ".git", |
| 16 | + }) |
| 17 | + if not root_dir then |
| 18 | + root_dir = vim.fn.getcwd() |
| 19 | + end |
| 20 | + local project_name = vim.fn.fnamemodify(root_dir, ":p:h:t") |
| 21 | + local workspace_dir = vim.fn.stdpath("cache") .. "/jdtls/" .. project_name |
| 22 | + |
| 23 | + -- Bemol workspace folders — added on_attach after jdtls initializes |
| 24 | + local bemol_dir = vim.fs.find({ ".bemol" }, { path = root_dir, upward = true, type = "directory" })[1] |
| 25 | + |
| 26 | + local config = { |
| 27 | + cmd = { "jdtls", "-data", workspace_dir }, |
| 28 | + root_dir = root_dir, |
| 29 | + capabilities = require("blink.cmp").get_lsp_capabilities(vim.lsp.protocol.make_client_capabilities()), |
| 30 | + settings = { |
| 31 | + java = { |
| 32 | + signatureHelp = { enabled = true }, |
| 33 | + import = { enabled = true }, |
| 34 | + rename = { enabled = true }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + on_attach = function(_, bufnr) |
| 38 | + if bemol_dir then |
| 39 | + local file = io.open(bemol_dir .. "/ws_root_folders", "r") |
| 40 | + if file then |
| 41 | + for line in file:lines() do |
| 42 | + if not vim.tbl_contains(vim.lsp.buf.list_workspace_folders(), line) then |
| 43 | + vim.lsp.buf.add_workspace_folder(line) |
| 44 | + end |
| 45 | + end |
| 46 | + file:close() |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + local opts = { buffer = bufnr } |
| 51 | + vim.keymap.set("n", "<leader>jo", jdtls.organize_imports, opts) |
| 52 | + vim.keymap.set("n", "<leader>jev", jdtls.extract_variable, opts) |
| 53 | + vim.keymap.set("v", "<leader>jem", jdtls.extract_method, opts) |
| 54 | + end, |
| 55 | + } |
| 56 | + |
| 57 | + jdtls.start_or_attach(config) |
| 58 | + end, |
| 59 | + }) |
| 60 | +end |
| 61 | + |
| 62 | +return M |
0 commit comments