|
1 | | -local JavaDap = require('java.dap.dapp') |
| 1 | +local log = require('java.utils.log') |
| 2 | +local get_error_handler = require('java.handlers.error') |
2 | 3 |
|
3 | | -local log = require('java-core.utils.log') |
4 | | -local notify = require('java-core.utils.notify') |
5 | | -local jdtls = require('java.jdtls') |
| 4 | +local Promise = require('java-core.utils.promise') |
| 5 | +local JavaCoreDap = require('java-core.dap') |
| 6 | +local JavaCoreTestApi = require('java-core.api.test') |
6 | 7 |
|
| 8 | +---@class JavaDap |
| 9 | +---@field private client LspClient |
| 10 | +---@field private dap JavaCoreDap |
| 11 | +---@field private test_api JavaCoreTestApi |
7 | 12 | local M = {} |
8 | 13 |
|
9 | | ----Setup dap config & adapter on jdtls attach event |
10 | | -function M.setup_dap_on_lsp_attach() |
11 | | - log.info('add LspAttach event handlers to setup dap adapter & config') |
| 14 | +---@param args { client: LspClient } |
| 15 | +---@return JavaDap |
| 16 | +function M:new(args) |
| 17 | + local o = { |
| 18 | + client = args.client, |
| 19 | + } |
12 | 20 |
|
13 | | - vim.api.nvim_create_autocmd('LspAttach', { |
14 | | - pattern = '*', |
15 | | - callback = M.on_jdtls_attach, |
16 | | - once = true, |
17 | | - group = vim.api.nvim_create_augroup('nvim-java-dap-config', {}), |
| 21 | + o.test_api = JavaCoreTestApi:new({ |
| 22 | + client = args.client, |
18 | 23 | }) |
19 | | -end |
20 | 24 |
|
21 | | ----Runs the current test class |
22 | | -function M.run_current_test_class() |
23 | | - return JavaDap:new(jdtls()):execute_current_test_class({ noDebug = true }) |
24 | | -end |
| 25 | + o.dap = JavaCoreDap:new({ |
| 26 | + client = args.client, |
| 27 | + }) |
25 | 28 |
|
26 | | -function M.debug_current_test_class() |
27 | | - return JavaDap:new(jdtls()):execute_current_test_class() |
| 29 | + setmetatable(o, self) |
| 30 | + self.__index = self |
| 31 | + return o |
28 | 32 | end |
29 | 33 |
|
30 | | ----Configures the dap |
31 | | -function M.config_dap() |
32 | | - return JavaDap:new(jdtls()) |
33 | | - :config_dap() |
34 | | - :thenCall(function() |
35 | | - notify.info('DAP configured') |
36 | | - end) |
37 | | - :catch(function(err) |
38 | | - notify.error('Failed to configure DAP', err) |
39 | | - end) |
| 34 | +---Run the current test class |
| 35 | +---@param config JavaCoreDapLauncherConfigOverridable |
| 36 | +function M:execute_current_test_class(config) |
| 37 | + log.info('running the current class') |
| 38 | + |
| 39 | + local buffer = vim.api.nvim_get_current_buf() |
| 40 | + |
| 41 | + return self.test_api |
| 42 | + :run_class_by_buffer(buffer, config) |
| 43 | + :catch(get_error_handler('failed to run current test class')) |
40 | 44 | end |
41 | 45 |
|
42 | | ----@private |
43 | | ----@param ev any |
44 | | -function M.on_jdtls_attach(ev) |
45 | | - local client = vim.lsp.get_client_by_id(ev.data.client_id) |
| 46 | +function M:config_dap() |
| 47 | + return Promise.resolve() |
| 48 | + :thenCall(function() |
| 49 | + log.debug('set dap adapter callback function') |
46 | 50 |
|
47 | | - if client.name == 'jdtls' then |
48 | | - log.info('setup java dap config & adapter') |
| 51 | + -- setting java adapter |
| 52 | + require('dap').adapters.java = function(callback) |
| 53 | + self.dap |
| 54 | + :get_dap_adapter() |
| 55 | + :thenCall(callback) |
| 56 | + :catch(get_error_handler('failed to set DAP adapter')) |
| 57 | + end |
49 | 58 |
|
50 | | - M.config_dap() |
51 | | - end |
| 59 | + -- setting java config |
| 60 | + return self.dap:get_dap_config() |
| 61 | + end) |
| 62 | + :thenCall(function(dap_config) |
| 63 | + log.debug('set dap config: ', dap_config) |
| 64 | + require('dap').configurations.java = dap_config |
| 65 | + end) |
| 66 | + :catch(get_error_handler('failed to set DAP configuration')) |
52 | 67 | end |
53 | 68 |
|
54 | 69 | return M |
0 commit comments