|
| 1 | +local runner = require('async.runner') |
| 2 | + |
1 | 3 | local M = {} |
2 | 4 |
|
3 | 5 | ---@param params nvim.CodeActionParamsResponse |
4 | 6 | function M.generate_constructor(params) |
5 | | - local JdtlsClient = require('java-core.ls.clients.jdtls-client') |
6 | | - local async = require('java-core.utils.async').sync |
7 | | - local get_client = require('java.utils.jdtls2') |
| 7 | + local instance = require('java.utils.instance_factory') |
8 | 8 | local get_error_handler = require('java.handlers.error') |
9 | 9 | local ui = require('java.utils.ui') |
10 | 10 |
|
11 | | - return async(function() |
12 | | - local jdtls = JdtlsClient(get_client()) |
| 11 | + return runner(function() |
| 12 | + local jdtls = instance.jdtls_client() |
13 | 13 | local status = jdtls:java_check_constructors_status(params.params) |
14 | 14 |
|
15 | 15 | if not status or not status.constructors then |
@@ -52,4 +52,49 @@ function M.generate_constructor(params) |
52 | 52 | .run() |
53 | 53 | end |
54 | 54 |
|
| 55 | +---@param params nvim.CodeActionParamsResponse |
| 56 | +function M.generate_to_string(params) |
| 57 | + local instance = require('java.utils.instance_factory') |
| 58 | + local get_error_handler = require('java.handlers.error') |
| 59 | + local ui = require('java.utils.ui') |
| 60 | + |
| 61 | + runner(function() |
| 62 | + local jdtls = instance.jdtls_client() |
| 63 | + local status = jdtls:java_check_to_string_status(params.params) |
| 64 | + |
| 65 | + if status.exists then |
| 66 | + local prompt = string.format( |
| 67 | + 'Method "toString()" already exists in the Class %s. Do you want to replace the implementation?', |
| 68 | + status.type |
| 69 | + ) |
| 70 | + local choice = ui.select(prompt, { 'Replace', 'Cancel' }) |
| 71 | + |
| 72 | + if choice ~= 'Replace' then |
| 73 | + return |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + local fields = ui.multi_select( |
| 78 | + 'Select the fields to include in the toString() method.', |
| 79 | + status.fields, |
| 80 | + function(field) |
| 81 | + return field.name |
| 82 | + end |
| 83 | + ) |
| 84 | + |
| 85 | + if not fields then |
| 86 | + return |
| 87 | + end |
| 88 | + |
| 89 | + local edit = jdtls:java_generate_to_string({ |
| 90 | + context = params.params, |
| 91 | + fields = fields, |
| 92 | + }) |
| 93 | + |
| 94 | + vim.lsp.util.apply_workspace_edit(edit, 'utf-8') |
| 95 | + end) |
| 96 | + .catch(get_error_handler('Generating to string failed')) |
| 97 | + .run() |
| 98 | +end |
| 99 | + |
55 | 100 | return M |
0 commit comments