|
| 1 | +local log = require('java.utils.log') |
| 2 | +local lsp = require('java.utils.lsp') |
| 3 | +local JavaDebug = require('java.ls.clients.java-debug-client') |
| 4 | +local Promise = require('java.utils.promise') |
| 5 | + |
| 6 | +local M = {} |
| 7 | + |
| 8 | +function M.setup_dap_on_attach() |
| 9 | + vim.api.nvim_create_autocmd('LspAttach', { |
| 10 | + pattern = '*', |
| 11 | + callback = function(a) |
| 12 | + local client = vim.lsp.get_client_by_id(a.data.client_id) |
| 13 | + |
| 14 | + if client.name == 'jdtls' then |
| 15 | + Promise.all({ |
| 16 | + M.set_dap_config(), |
| 17 | + M.set_dap_adapter(), |
| 18 | + }):catch(function(err) |
| 19 | + local msg = [[Faild to set DAP configuration & adapter]] |
| 20 | + |
| 21 | + error(msg, err) |
| 22 | + log.error(msg, err) |
| 23 | + end) |
| 24 | + end |
| 25 | + end, |
| 26 | + }) |
| 27 | +end |
| 28 | + |
| 29 | +function M.set_dap_config() |
| 30 | + log.info('setting dap configurations for java') |
| 31 | + |
| 32 | + local dap = require('dap') |
| 33 | + |
| 34 | + return M.get_dap_config():thenCall( |
| 35 | + ---@type JavaDapConfigurationList |
| 36 | + function(res) |
| 37 | + dap.configurations.java = res |
| 38 | + end |
| 39 | + ) |
| 40 | +end |
| 41 | + |
| 42 | +function M.set_dap_adapter() |
| 43 | + log.info('setting dap adapter for java') |
| 44 | + |
| 45 | + local dap = require('dap') |
| 46 | + |
| 47 | + return M.get_dap_adapter():thenCall( |
| 48 | + ---@type JavaDapAdapter |
| 49 | + function(res) |
| 50 | + log.debug('adapter settings: ', res) |
| 51 | + dap.adapters.java = res |
| 52 | + end |
| 53 | + ) |
| 54 | +end |
| 55 | + |
| 56 | +---@class JavaDapAdapter |
| 57 | +---@field type string |
| 58 | +---@field host string |
| 59 | +---@field port integer |
| 60 | + |
| 61 | +---Returns the dap adapter config |
| 62 | +---@return Promise |
| 63 | +function M.get_dap_adapter() |
| 64 | + log.info('creating dap adapter for java') |
| 65 | + |
| 66 | + local client = lsp.get_jdtls_client() |
| 67 | + |
| 68 | + --@TODO when thrown from the function instead of the promise, |
| 69 | + --error handling has to be done in two places |
| 70 | + if not client then |
| 71 | + local msg = 'no active jdtls client was found' |
| 72 | + |
| 73 | + log.error(msg) |
| 74 | + error(msg) |
| 75 | + end |
| 76 | + |
| 77 | + local jdtlsClient = JavaDebug:new({ |
| 78 | + client = client, |
| 79 | + }) |
| 80 | + |
| 81 | + return jdtlsClient:start_debug_session():thenCall( |
| 82 | + ---@param port JavaDebugStartDebugSessionResponse |
| 83 | + function(port) |
| 84 | + return { |
| 85 | + type = 'server', |
| 86 | + host = '127.0.0.1', |
| 87 | + port = port, |
| 88 | + } |
| 89 | + end |
| 90 | + ) |
| 91 | +end |
| 92 | + |
| 93 | +---@class JavaDapConfiguration |
| 94 | +---@field name string |
| 95 | +---@field projectName string |
| 96 | +---@field mainClass string |
| 97 | +---@field javaExec string |
| 98 | +---@field modulePaths string[] |
| 99 | +---@field classPaths string[] |
| 100 | +---@field request string |
| 101 | + |
| 102 | +---@alias JavaDapConfigurationList JavaDapConfiguration[] |
| 103 | + |
| 104 | +---Returns the dap configuration for the current project |
| 105 | +---@return Promise |
| 106 | +function M.get_dap_config() |
| 107 | + log.info('creating dap configuration for java') |
| 108 | + |
| 109 | + local client = lsp.get_jdtls_client() |
| 110 | + |
| 111 | + if not client then |
| 112 | + local msg = 'no active jdtls client was found' |
| 113 | + |
| 114 | + log.error(msg) |
| 115 | + error(msg) |
| 116 | + end |
| 117 | + |
| 118 | + local jdtlsClient = JavaDebug:new({ |
| 119 | + client = client, |
| 120 | + }) |
| 121 | + |
| 122 | + ---@type JavaDebugResolveMainClassResponse |
| 123 | + local main_classes_info_list |
| 124 | + |
| 125 | + return jdtlsClient |
| 126 | + :resolve_main_class() |
| 127 | + :thenCall( |
| 128 | + ---@param main_classes_info JavaDebugResolveMainClassResponse |
| 129 | + function(main_classes_info) |
| 130 | + main_classes_info_list = main_classes_info |
| 131 | + |
| 132 | + ---@type Promise[] |
| 133 | + local classpath_promises = {} |
| 134 | + ---@type Promise[] |
| 135 | + local java_exec_promises = {} |
| 136 | + |
| 137 | + for _, single_class_info in ipairs(main_classes_info) do |
| 138 | + table.insert( |
| 139 | + classpath_promises, |
| 140 | + jdtlsClient:resolve_classpath( |
| 141 | + single_class_info.mainClass, |
| 142 | + single_class_info.projectName |
| 143 | + ) |
| 144 | + ) |
| 145 | + |
| 146 | + table.insert( |
| 147 | + java_exec_promises, |
| 148 | + jdtlsClient:resolve_java_executable( |
| 149 | + single_class_info.mainClass, |
| 150 | + single_class_info.projectName |
| 151 | + ) |
| 152 | + ) |
| 153 | + end |
| 154 | + |
| 155 | + return Promise.all({ |
| 156 | + Promise.all(classpath_promises), |
| 157 | + Promise.all(java_exec_promises), |
| 158 | + }) |
| 159 | + end |
| 160 | + ) |
| 161 | + :thenCall(function(result) |
| 162 | + return M.__get_dap_java_config( |
| 163 | + main_classes_info_list, |
| 164 | + result[1], |
| 165 | + result[2] |
| 166 | + ) |
| 167 | + end) |
| 168 | +end |
| 169 | + |
| 170 | +function M.__get_dap_java_config(main_classes, classpaths, java_execs) |
| 171 | + local len = #main_classes |
| 172 | + local dap_config_list = {} |
| 173 | + |
| 174 | + for i = 1, len do |
| 175 | + local main_class = main_classes[i].mainClass |
| 176 | + local project_name = main_classes[i].projectName |
| 177 | + local module_paths = classpaths[i][1] |
| 178 | + local class_paths = classpaths[i][2] |
| 179 | + |
| 180 | + table.insert(dap_config_list, { |
| 181 | + name = string.format('%s -> %s', project_name, main_class), |
| 182 | + projectName = project_name, |
| 183 | + mainClass = main_class, |
| 184 | + javaExec = java_execs[i], |
| 185 | + request = 'launch', |
| 186 | + type = 'java', |
| 187 | + modulePaths = module_paths, |
| 188 | + classPaths = class_paths, |
| 189 | + }) |
| 190 | + end |
| 191 | + |
| 192 | + return dap_config_list |
| 193 | +end |
| 194 | + |
| 195 | +return M |
0 commit comments