From 13b7af2302e37a058c62ec81e60664d3a32798b6 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 28 Jul 2024 22:04:34 +0200 Subject: [PATCH 01/10] install jdtls with mason --- lua/kickstart/plugins/lspconfig.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index e0daba5..60b764b 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -217,12 +217,17 @@ return { 'markdownlint', 'isort', 'black', + 'jdtls', }) require('mason-tool-installer').setup { ensure_installed = ensure_installed } require('mason-lspconfig').setup { handlers = { function(server_name) + if server_name == 'jdtls' then + return + end + local server = servers[server_name] or {} -- This handles overriding only values explicitly passed -- by the server configuration above. Useful when disabling From 45ad1e013b1fa5b8559e390fb9c2d757cab16019 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 28 Jul 2024 22:04:57 +0200 Subject: [PATCH 02/10] install nvim-jdtls as dep to help with config --- lua/kickstart/plugins/lspconfig.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index 60b764b..10470b1 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -6,6 +6,7 @@ return { { 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants 'williamboman/mason-lspconfig.nvim', 'WhoIsSethDaniel/mason-tool-installer.nvim', + { 'mfussenegger/nvim-jdtls', ft = 'java' }, -- Useful status updates for LSP. -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})` From a33a2ae8d8d9151422fc07f99ef26a05bf2dd41c Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 28 Jul 2024 22:06:51 +0200 Subject: [PATCH 03/10] copy from basic setup with mason install path --- lua/kickstart/plugins/lspconfig.lua | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index 10470b1..b58e7b9 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -199,6 +199,14 @@ return { }, } + local function jdtls_setup() + local config = { + cmd = { require('mason-registry').get_package('jdtls'):get_install_path() .. '/bin/jdtls' }, + root_dir = vim.fs.dirname(vim.fs.find({ 'gradlew', '.git', 'mvnw' }, { upward = true })[1]), + } + require('jdtls').start_or_attach(config) + end + -- Ensure the servers and tools above are installed -- To check the current status of installed tools and/or manually install -- other tools, you can run @@ -226,6 +234,7 @@ return { handlers = { function(server_name) if server_name == 'jdtls' then + jdtls_setup() return end From 2da89bd3e71dcd6d2359afbc17d9ae31eabdcf9f Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 28 Jul 2024 22:08:18 +0200 Subject: [PATCH 04/10] expand to advanced setup --- lua/kickstart/plugins/lspconfig.lua | 62 ++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index b58e7b9..b885082 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -200,9 +200,67 @@ return { } local function jdtls_setup() + local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t') + -- calculate workspace dir + local workspace_dir = vim.fn.stdpath 'data' .. '/site/java/workspace-root/' .. project_name + -- get the mason install path + local install_path = require('mason-registry').get_package('jdtls'):get_install_path() + local jdtls_path = vim.fn.glob(install_path .. '/plugins/org.eclipse.equinox.launcher_*.jar') + + -- try to detect sysname for config + local sysname = 'win' + if vim.fn.has 'unix' then + sysname = 'linux' + elseif vim.fn.has 'mac' then + sysname = 'mac' + end + -- set default config according to sysname + local config_path = install_path .. '/config_' .. sysname + + -- See `:help vim.lsp.start_client` for an overview of the supported `config` options. local config = { - cmd = { require('mason-registry').get_package('jdtls'):get_install_path() .. '/bin/jdtls' }, - root_dir = vim.fs.dirname(vim.fs.find({ 'gradlew', '.git', 'mvnw' }, { upward = true })[1]), + -- The command that starts the language server + -- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line + cmd = { + 'java', + + '-Declipse.application=org.eclipse.jdt.ls.core.id1', + '-Dosgi.bundles.defaultStartLevel=4', + '-Declipse.product=org.eclipse.jdt.ls.core.product', + '-Dlog.protocol=true', + '-Dlog.level=ALL', + '-Xmx1g', + '--add-modules=ALL-SYSTEM', + '--add-opens', + 'java.base/java.util=ALL-UNNAMED', + '--add-opens', + 'java.base/java.lang=ALL-UNNAMED', + + '-jar', + jdtls_path, + + '-configuration', + config_path, + + '-data', + workspace_dir, + }, + + capabilities = capabilities, + root_dir = vim.fs.root(0, { '.git', 'mvnw', 'gradlew' }), + + -- 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 = {}, + }, + + -- Language server `initializationOptions` + -- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation + init_options = { + bundles = {}, + }, } require('jdtls').start_or_attach(config) end From d9eca6205af6a94189c753e92324d339fdaaca4d Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 28 Jul 2024 22:08:44 +0200 Subject: [PATCH 05/10] trigger on java file opened --- lua/kickstart/plugins/lspconfig.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index b885082..d7ee82d 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -262,7 +262,12 @@ return { bundles = {}, }, } - require('jdtls').start_or_attach(config) + vim.api.nvim_create_autocmd('FileType', { + pattern = 'java', + callback = function() + require('jdtls').start_or_attach(config) + end, + }) end -- Ensure the servers and tools above are installed From 9373dd686ad1996b246730ccb14b7f0a31c4c2bf Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 28 Jul 2024 22:19:32 +0200 Subject: [PATCH 06/10] wrap jdtls config in function for dynamic workspace --- lua/kickstart/plugins/lspconfig.lua | 126 ++++++++++++++-------------- 1 file changed, 65 insertions(+), 61 deletions(-) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index d7ee82d..8d14c30 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -200,72 +200,76 @@ return { } local function jdtls_setup() - local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t') - -- calculate workspace dir - local workspace_dir = vim.fn.stdpath 'data' .. '/site/java/workspace-root/' .. project_name - -- get the mason install path - local install_path = require('mason-registry').get_package('jdtls'):get_install_path() - local jdtls_path = vim.fn.glob(install_path .. '/plugins/org.eclipse.equinox.launcher_*.jar') + --- @param workspace_dir string + local function generate_config(workspace_dir) + -- get the mason install path + local install_path = require('mason-registry').get_package('jdtls'):get_install_path() + local jdtls_path = vim.fn.glob(install_path .. '/plugins/org.eclipse.equinox.launcher_*.jar') - -- try to detect sysname for config - local sysname = 'win' - if vim.fn.has 'unix' then - sysname = 'linux' - elseif vim.fn.has 'mac' then - sysname = 'mac' + -- try to detect sysname for config + local sysname = 'win' + if vim.fn.has 'unix' then + sysname = 'linux' + elseif vim.fn.has 'mac' then + sysname = 'mac' + end + -- set default config according to sysname + local config_path = install_path .. '/config_' .. sysname + + -- 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 = { + 'java', + + '-Declipse.application=org.eclipse.jdt.ls.core.id1', + '-Dosgi.bundles.defaultStartLevel=4', + '-Declipse.product=org.eclipse.jdt.ls.core.product', + '-Dlog.protocol=true', + '-Dlog.level=ALL', + '-Xmx1g', + '--add-modules=ALL-SYSTEM', + '--add-opens', + 'java.base/java.util=ALL-UNNAMED', + '--add-opens', + 'java.base/java.lang=ALL-UNNAMED', + + '-jar', + jdtls_path, + + '-configuration', + config_path, + + '-data', + workspace_dir, + }, + + capabilities = capabilities, + root_dir = vim.fs.root(0, { '.git', 'mvnw', 'gradlew' }), + + -- 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 = {}, + }, + + -- Language server `initializationOptions` + -- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation + init_options = { + bundles = {}, + }, + } + return config end - -- set default config according to sysname - local config_path = install_path .. '/config_' .. sysname - - -- 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 = { - 'java', - - '-Declipse.application=org.eclipse.jdt.ls.core.id1', - '-Dosgi.bundles.defaultStartLevel=4', - '-Declipse.product=org.eclipse.jdt.ls.core.product', - '-Dlog.protocol=true', - '-Dlog.level=ALL', - '-Xmx1g', - '--add-modules=ALL-SYSTEM', - '--add-opens', - 'java.base/java.util=ALL-UNNAMED', - '--add-opens', - 'java.base/java.lang=ALL-UNNAMED', - - '-jar', - jdtls_path, - - '-configuration', - config_path, - - '-data', - workspace_dir, - }, - - capabilities = capabilities, - root_dir = vim.fs.root(0, { '.git', 'mvnw', 'gradlew' }), - - -- 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 = {}, - }, - - -- Language server `initializationOptions` - -- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation - init_options = { - bundles = {}, - }, - } vim.api.nvim_create_autocmd('FileType', { pattern = 'java', callback = function() - require('jdtls').start_or_attach(config) + local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t') + -- calculate workspace dir + local workspace_dir = vim.fn.stdpath 'data' .. '/site/java/workspace-root/' .. project_name + require('jdtls').start_or_attach(generate_config(workspace_dir)) end, }) end From b2d0788ede217a8caf18ec3c0aa64175435fed29 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 28 Oct 2024 17:57:16 +0100 Subject: [PATCH 07/10] add nvim-jdtls to lazy-lock --- lazy-lock.json | 1 + 1 file changed, 1 insertion(+) diff --git a/lazy-lock.json b/lazy-lock.json index 518f25e..edd85b3 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -20,6 +20,7 @@ "mini.nvim": { "branch": "main", "commit": "d4ce7d025f9c7bb4d55ebc4fd88987651e632893" }, "nvim": { "branch": "main", "commit": "7be452ee067978cdc8b2c5f3411f0c71ffa612b9" }, "nvim-cmp": { "branch": "main", "commit": "ae644feb7b67bf1ce4260c231d1d4300b19c6f30" }, + "nvim-jdtls": { "branch": "master", "commit": "efe813854432a314b472226dca813f0f2598d44a" }, "nvim-lint": { "branch": "master", "commit": "b3ab4ba88845893663d0b7122ee82921adf28358" }, "nvim-lspconfig": { "branch": "master", "commit": "541f3a2781de481bb84883889e4d9f0904250a56" }, "nvim-treesitter": { "branch": "master", "commit": "03452942dfbd998701d4123ccad2090e1bc7e9f1" }, From 6e9877e9592ea3fd224beddb612e0489bbb0b83e Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sat, 2 Nov 2024 15:52:33 +0100 Subject: [PATCH 08/10] detect processing-java core library installation path --- lua/kickstart/plugins/lspconfig.lua | 56 ++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index 98eded4..3d53e3f 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -224,6 +224,54 @@ return { -- set default config according to sysname local config_path = install_path .. '/config_' .. sysname + -- START SEARCH FOR PROCESSING -- + -- detect install path based on system + local is_windows = vim.fn.has 'win32' == 1 + local cmds = is_windows and { + '(gcm processing.exe).source', + } or { + 'whereis', + 'processing.exe', + } + + -- run syscall and process output + local output = vim.system(cmds, { text = true }):wait() + local path = output.stdout or '' + if not is_windows then + -- on linux, get substr starting with path + local start_idx = string.find(path, '/') + path = (start_idx ~= nil) and string.sub(path, start_idx) or '' + end + + -- for windows, remove backslashes + path = string.gsub(path, '\\', '/') + -- remove filename at end of path + local end_idx = string.find(path, '/[^/]*$') + path = string.sub(path, 1, end_idx) + + -- in case of chocolatey install, navigate to binaries + local is_chocolatey = string.find(path, 'chocolatey') + if is_chocolatey then + path = path .. '../lib/processing/tools/' + -- list directories for program version + cmds = is_windows and { + 'ls', + path, + '|select name', + '|Out-String', + } or { + 'ls', + '-l', + path, + } + + output = vim.system(cmds, { text = true }):wait() + local version = string.match(output.stdout, 'processing[^%s]*') + path = path .. version + end + -- set path to processing-core library + path = path .. '/core/library/core.jar' + -- See `:help vim.lsp.start_client` for an overview of the supported `config` options. local config = { -- The command that starts the language server @@ -260,7 +308,13 @@ return { -- 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 = {}, + java = { + project = { + referencedLibraries = { + is_windows and string.gsub(path, '/', '\\') or path, + }, + }, + }, }, -- Language server `initializationOptions` From ad214db84ee648c235edbb8e4d222f48a356a9fe Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 5 Nov 2024 11:37:16 +0100 Subject: [PATCH 09/10] Revert "detect processing-java core library installation path" This reverts commit 6e9877e9592ea3fd224beddb612e0489bbb0b83e. --- lua/kickstart/plugins/lspconfig.lua | 56 +---------------------------- 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index 3d53e3f..98eded4 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -224,54 +224,6 @@ return { -- set default config according to sysname local config_path = install_path .. '/config_' .. sysname - -- START SEARCH FOR PROCESSING -- - -- detect install path based on system - local is_windows = vim.fn.has 'win32' == 1 - local cmds = is_windows and { - '(gcm processing.exe).source', - } or { - 'whereis', - 'processing.exe', - } - - -- run syscall and process output - local output = vim.system(cmds, { text = true }):wait() - local path = output.stdout or '' - if not is_windows then - -- on linux, get substr starting with path - local start_idx = string.find(path, '/') - path = (start_idx ~= nil) and string.sub(path, start_idx) or '' - end - - -- for windows, remove backslashes - path = string.gsub(path, '\\', '/') - -- remove filename at end of path - local end_idx = string.find(path, '/[^/]*$') - path = string.sub(path, 1, end_idx) - - -- in case of chocolatey install, navigate to binaries - local is_chocolatey = string.find(path, 'chocolatey') - if is_chocolatey then - path = path .. '../lib/processing/tools/' - -- list directories for program version - cmds = is_windows and { - 'ls', - path, - '|select name', - '|Out-String', - } or { - 'ls', - '-l', - path, - } - - output = vim.system(cmds, { text = true }):wait() - local version = string.match(output.stdout, 'processing[^%s]*') - path = path .. version - end - -- set path to processing-core library - path = path .. '/core/library/core.jar' - -- See `:help vim.lsp.start_client` for an overview of the supported `config` options. local config = { -- The command that starts the language server @@ -308,13 +260,7 @@ return { -- 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 = { - project = { - referencedLibraries = { - is_windows and string.gsub(path, '/', '\\') or path, - }, - }, - }, + java = {}, }, -- Language server `initializationOptions` From c3569c1b5137efbe7871e76fa271d533e1e2765b Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 5 Nov 2024 11:42:23 +0100 Subject: [PATCH 10/10] explore workspace dir pusing oil.nvim to configure libraries manually --- lua/kickstart/plugins/lspconfig.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lua/kickstart/plugins/lspconfig.lua b/lua/kickstart/plugins/lspconfig.lua index 98eded4..dff0980 100644 --- a/lua/kickstart/plugins/lspconfig.lua +++ b/lua/kickstart/plugins/lspconfig.lua @@ -29,6 +29,9 @@ return { -- Allows extra capabilities provided by nvim-cmp 'hrsh7th/cmp-nvim-lsp', + + -- Needed to explore Workspace in Java + 'stevearc/oil.nvim', }, config = function() -- Brief aside: **What is LSP?** @@ -273,11 +276,15 @@ return { end vim.api.nvim_create_autocmd('FileType', { pattern = 'java', - callback = function() + callback = function(opt) local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t') -- calculate workspace dir local workspace_dir = vim.fn.stdpath 'data' .. '/site/java/workspace-root/' .. project_name require('jdtls').start_or_attach(generate_config(workspace_dir)) + vim.keymap.set('n', 'we', 'Oil ' .. workspace_dir .. '', { + desc = '[W]orkspace [E]xplorer', + buffer = opt.buf, + }) end, }) end