From 2fa962b9af7496a4e4ac8c8ea974b0117354ddc9 Mon Sep 17 00:00:00 2001 From: s1n7ax Date: Fri, 17 Nov 2023 22:29:16 +0530 Subject: [PATCH] refactor!: let jdtls wrapper script handle launching the server --- lua/java-core/ls/servers/jdtls/config.lua | 37 ++++++ lua/java-core/ls/servers/jdtls/init.lua | 67 ++++++++++ lua/java-core/ls/servers/jdtls/plugins.lua | 32 +++++ .../{ => ls/servers/jdtls}/workspace.lua | 0 lua/java-core/server.lua | 123 ------------------ lua/java-core/settings.lua | 27 ---- lua/java-core/utils/path.lua | 10 ++ lua/java-core/utils/plugin.lua | 39 ------ lua/java-core/utils/table.lua | 44 ------- 9 files changed, 146 insertions(+), 233 deletions(-) create mode 100644 lua/java-core/ls/servers/jdtls/config.lua create mode 100644 lua/java-core/ls/servers/jdtls/init.lua create mode 100644 lua/java-core/ls/servers/jdtls/plugins.lua rename lua/java-core/{ => ls/servers/jdtls}/workspace.lua (100%) delete mode 100644 lua/java-core/server.lua delete mode 100644 lua/java-core/settings.lua create mode 100644 lua/java-core/utils/path.lua delete mode 100644 lua/java-core/utils/plugin.lua delete mode 100644 lua/java-core/utils/table.lua diff --git a/lua/java-core/ls/servers/jdtls/config.lua b/lua/java-core/ls/servers/jdtls/config.lua new file mode 100644 index 0000000..f0ee056 --- /dev/null +++ b/lua/java-core/ls/servers/jdtls/config.lua @@ -0,0 +1,37 @@ +local M = {} + +function M.get_config() + return { + init_options = { + extendedClientCapabilities = { + classFileContentsSupport = true, + generateToStringPromptSupport = true, + hashCodeEqualsPromptSupport = true, + advancedExtractRefactoringSupport = true, + advancedOrganizeImportsSupport = true, + generateConstructorsPromptSupport = true, + generateDelegateMethodsPromptSupport = true, + moveRefactoringSupport = true, + overrideMethodsPromptSupport = true, + executeClientCommandSupport = true, + inferSelectionSupport = { + 'extractMethod', + 'extractVariable', + 'extractConstant', + 'extractVariableAllOccurrence', + }, + }, + }, + + handlers = { + --@TODO + --overriding '$/progress' is necessary because by default it's using the + --lspconfig progress handler which prints the wrong value in the latest + --jdtls version (tested on 1.29.0). + --https://github.com/neovim/nvim-lspconfig/issues/2897 + ['$/progress'] = vim.lsp.handlers['$/progress'], + }, + } +end + +return M diff --git a/lua/java-core/ls/servers/jdtls/init.lua b/lua/java-core/ls/servers/jdtls/init.lua new file mode 100644 index 0000000..3b88f6b --- /dev/null +++ b/lua/java-core/ls/servers/jdtls/init.lua @@ -0,0 +1,67 @@ +local util = require('lspconfig.util') +local path = require('java-core.utils.path') +local mason = require('java-core.utils.mason') +local plugins = require('java-core.ls.servers.jdtls.plugins') +local log = require('java-core.utils.log') +local workspace = require('java-core.ls.servers.jdtls.workspace') +local config = require('java-core.ls.servers.jdtls.config') + +local M = {} + +---@class JavaCoreGetConfigOptions +---@field root_markers string[] list of files to find the root dir of a project +---Ex:- { 'pom.xml', 'build.gradle', '.git' } +---@field jdtls_plugins string[] list of jdtls plugins to load on start up +---Ex:- { 'java-test', 'java-debug-adapter' } + +---Returns a configuration for jdtls that you can pass into the setup of nvim-lspconfig +---@param opts JavaCoreGetConfigOptions +---@return LSPSetupConfig # jdtls setup configuration +function M.get_config(opts) + log.debug('generating jdtls config') + + local jdtls_path = mason.get_pkg_path('jdtls') + local lombok_path = path.join(jdtls_path, 'lombok.jar') + local jdtls_cache_path = path.join(vim.fn.stdpath('cache'), 'jdtls') + local plugin_paths = + plugins.get_plugin_paths({ 'java-test', 'java-debug-adapter' }) + + local base_config = config.get_config() + + base_config.cmd = { + 'jdtls', + '-configuration', + jdtls_cache_path, + '-data', + workspace.get_default_workspace(), + '-javaagent:' .. lombok_path, + } + + base_config.root_dir = M.get_root_finder(opts.root_markers) + base_config.init_options.bundles = plugin_paths + base_config.init_options.workspace = workspace.get_default_workspace() + + log.debug('generated jdtls setup config: ', base_config) + + return base_config +end + +---Returns a function that finds the java project root +---@private +---@param root_markers string[] list of files to find the root dir of a project +---@return function +function M.get_root_finder(root_markers) + return function(file_name) + log.info('finding the root_dir') + log.debug('root_markers: ', root_markers) + + local root = util.root_pattern(unpack(root_markers))(file_name) + + if root then + log.fmt_debug('root of: %s is: %s', file_name, root) + return root + end + end +end + +return M diff --git a/lua/java-core/ls/servers/jdtls/plugins.lua b/lua/java-core/ls/servers/jdtls/plugins.lua new file mode 100644 index 0000000..b5acc1d --- /dev/null +++ b/lua/java-core/ls/servers/jdtls/plugins.lua @@ -0,0 +1,32 @@ +local mason = require('java-core.utils.mason') +local path = require('java-core.utils.path') +local file = require('java-core.utils.file') + +local List = require('java-core.utils.list') + +local M = {} + +local plugin_to_jar_path_map = { + ['java-test'] = '*.jar', + ['java-debug-adapter'] = '*.jar', +} + +---Returns a list of .jar file paths for given list of jdtls plugins +---@param plugins string[] +---@return string[] # list of .jar file paths +function M.get_plugin_paths(plugins) + local plugin_paths = List:new() + + for _, plugin in ipairs(plugins) do + local relative_path = plugin_to_jar_path_map[plugin] + local plugin_shared_path = mason.get_shared_path(plugin) + local full_path = path.join(plugin_shared_path, relative_path) + local resolved_paths = file.get_file_list(full_path) + + plugin_paths:push(resolved_paths) + end + + return plugin_paths:flatten() +end + +return M diff --git a/lua/java-core/workspace.lua b/lua/java-core/ls/servers/jdtls/workspace.lua similarity index 100% rename from lua/java-core/workspace.lua rename to lua/java-core/ls/servers/jdtls/workspace.lua diff --git a/lua/java-core/server.lua b/lua/java-core/server.lua deleted file mode 100644 index 713046d..0000000 --- a/lua/java-core/server.lua +++ /dev/null @@ -1,123 +0,0 @@ -local util = require('lspconfig.util') -local system = require('java-core.utils.system') -local file = require('java-core.utils.file') -local mason = require('java-core.utils.mason') -local plugin = require('java-core.utils.plugin') -local log = require('java-core.utils.log') -local tbl = require('java-core.utils.table') -local workspace = require('java-core.workspace') - -local M = {} - ----@class JDTLSPluginPathRecord ----@field name string ----@field path string - ----@alias JDTLSPluginPaths JDTLSPluginPathRecord[] -M.plugins = { - { name = 'java-test', path = '/*.jar' }, - { name = 'java-debug-adapter', path = '/*.jar' }, -} - ----@class JavaGetConfigOptions ----@field root_markers string[] list of files to find the root dir of a project ----Ex:- { 'pom.xml', 'build.gradle', '.git' } - ----Returns a configuration for jdtls that you can pass into the setup of nvim-lspconfig ----@param opts JavaGetConfigOptions ----@return LSPSetupConfig # jdtls setup configuration -function M.get_config(opts) - log.info('generating jdtls config') - - local jdtls_path = mason.get_pkg_path('jdtls') - local curr_os = system.get_os() - local plugins = plugin.get_plugins(M.plugins) - local lombok_path = jdtls_path .. '/lombok.jar' - - local 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', - '-Xms1g', - '--add-modules=ALL-SYSTEM', - '--add-opens', - 'java.base/java.util=ALL-UNNAMED', - '--add-opens', - 'java.base/java.lang=ALL-UNNAMED', - '-jar', - vim.fn.glob(jdtls_path .. '/plugins/org.eclipse.equinox.launcher_*.jar'), - '-configuration', - jdtls_path .. '/config_' .. curr_os, - '-data', - workspace.get_default_workspace(), - } - - if file.is_file(lombok_path) then - tbl.insert(cmd, '-javaagent:' .. lombok_path) - end - - local conf = { - cmd = cmd, - init_options = { - bundles = plugins, - workspace = workspace.get_default_workspace(), - extendedClientCapabilities = { - classFileContentsSupport = true, - generateToStringPromptSupport = true, - hashCodeEqualsPromptSupport = true, - advancedExtractRefactoringSupport = true, - advancedOrganizeImportsSupport = true, - generateConstructorsPromptSupport = true, - generateDelegateMethodsPromptSupport = true, - moveRefactoringSupport = true, - overrideMethodsPromptSupport = true, - executeClientCommandSupport = true, - inferSelectionSupport = { - 'extractMethod', - 'extractVariable', - 'extractConstant', - 'extractVariableAllOccurrence', - }, - }, - }, - - root_dir = M.get_root_finder(opts.root_markers), - capabilities = vim.lsp.protocol.make_client_capabilities(), - - handlers = { - --@TODO - --overriding '$/progress' is necessary because by default it's using the - --lspconfig progress handler which prints the wrong value in the latest - --jdtls version (tested on 1.29.0). - --https://github.com/neovim/nvim-lspconfig/issues/2897 - ['$/progress'] = vim.lsp.handlers['$/progress'], - }, - } - - log.debug('generated config: ', conf) - - return conf -end - ----Returns a function that finds the java project root ----@private ----@param root_markers string[] list of files to find the root dir of a project ----@return function -function M.get_root_finder(root_markers) - return function(file_name) - log.info('finding the root_dir') - log.debug('root_markers: ', root_markers) - - local root = util.root_pattern(unpack(root_markers))(file_name) - - if root then - log.fmt_debug('root of: %s is: %s', file_name, root) - return root - end - end -end - -return M diff --git a/lua/java-core/settings.lua b/lua/java-core/settings.lua deleted file mode 100644 index d65965d..0000000 --- a/lua/java-core/settings.lua +++ /dev/null @@ -1,27 +0,0 @@ -local lsp = require('java-core.utils.lsp') -local log = require('java-core.utils.log') - -local M = {} - -function M.set_runtime() end - ----Change jdtls settings ----@param settings InitializationOptions settings to set ----@return boolean -function M.change_settings(settings) - log.info('changing settings') - - local client = lsp.get_jdtls_client() - - if not client then - local msg = 'jdtls client not found' - log.error(msg) - error(msg) - end - - return client.notify('workspace/didChangeConfiguration', { - settings = settings, - }) -end - -return M diff --git a/lua/java-core/utils/path.lua b/lua/java-core/utils/path.lua new file mode 100644 index 0000000..3db6c8b --- /dev/null +++ b/lua/java-core/utils/path.lua @@ -0,0 +1,10 @@ +local M = {} + +---Join a given list of paths to one path +---@param ... string paths to join +---@return string # joined path +function M.join(...) + return table.concat({ ... }, '/') +end + +return M diff --git a/lua/java-core/utils/plugin.lua b/lua/java-core/utils/plugin.lua deleted file mode 100644 index 9b92f32..0000000 --- a/lua/java-core/utils/plugin.lua +++ /dev/null @@ -1,39 +0,0 @@ -local log = require('java-core.utils.log') -local file = require('java-core.utils.file') -local mason = require('java-core.utils.mason') - -local M = {} - ----Returns a list of jar files of given plugin ----@param pkg_name string name of the mason package name ----@return string[] -function M.get_plugin_jars(pkg_name, path_to_jars) - if not mason.is_pkg_installed(pkg_name) then - log.fmt_debug('plugin %s is not installed', pkg_name) - return {} - end - - local path = mason.get_shared_path(pkg_name) - - log.fmt_info('looking for %s plugin files at %s', pkg_name, path) - - local files = file.get_file_list(path .. path_to_jars) - - log.fmt_debug('found %d files for %s plugin ', #files, pkg_name) - return files -end - ----Returns a list of jar files of all the plugins ----@param plugin_list JDTLSPluginPaths ----@return string[] -function M.get_plugins(plugin_list) - local plugins = {} - - for _, plugin in ipairs(plugin_list) do - vim.list_extend(plugins, M.get_plugin_jars(plugin.name, plugin.path)) - end - - return plugins -end - -return M diff --git a/lua/java-core/utils/table.lua b/lua/java-core/utils/table.lua deleted file mode 100644 index ea45af9..0000000 --- a/lua/java-core/utils/table.lua +++ /dev/null @@ -1,44 +0,0 @@ -local M = {} - ----Inserts a list of values to a given table ----@param tbl List[] ----@param ... any list of values to insert ----@return any -function M.insert(tbl, ...) - for _, value in ipairs({ ... }) do - table.insert(tbl, value) - end - - return tbl -end - ----Flatten a table ----@param tbl table ----@return table -function M.flatten(tbl) - local flatten_tbl = {} - - for _, v1 in ipairs(tbl) do - for _, v2 in ipairs(v1) do - table.insert(flatten_tbl, v2) - end - end - - return flatten_tbl -end - ----Map a given table ----@param tbl table ----@param mapper fun(value: any): any ----@return table -function M.map(tbl, mapper) - local mapped = {} - - for _, v in ipairs(tbl) do - table.insert(mapped, mapper(v)) - end - - return mapped -end - -return M