|
1 | | -local jdtls = require('java.utils.jdtls2') |
2 | | -local get_error_handler = require('java.handlers.error') |
3 | | - |
4 | | -local async = require('java-core.utils.async').sync |
5 | | - |
6 | 1 | local M = {} |
7 | 2 |
|
8 | 3 | function M.extract_variable() |
9 | | - M.extract('extractVariable') |
| 4 | + M.run_code_action('refactor.extract.variable', 'extractVariable') |
10 | 5 | end |
11 | 6 |
|
12 | 7 | function M.extract_variable_all_occurrence() |
13 | | - M.extract('extractVariableAllOccurrence') |
| 8 | + M.run_code_action('refactor.extract.variable', 'extractVariableAllOccurrence') |
14 | 9 | end |
15 | 10 |
|
16 | 11 | function M.extract_constant() |
17 | | - M.extract('extractConstant') |
| 12 | + M.run_code_action('refactor.extract.constant') |
18 | 13 | end |
19 | 14 |
|
20 | 15 | function M.extract_method() |
21 | | - M.extract('extractMethod') |
| 16 | + M.run_code_action('refactor.extract.function') |
22 | 17 | end |
23 | 18 |
|
24 | 19 | function M.extract_field() |
25 | | - M.extract('extractField') |
26 | | -end |
27 | | - |
28 | | -function M.convert_variable_to_field() |
29 | | - M.extract('convertVariableToField') |
| 20 | + M.run_code_action('refactor.extract.field') |
30 | 21 | end |
31 | 22 |
|
32 | | ----@param refactor_command jdtls.CodeActionCommand |
33 | | -function M.extract(refactor_command) |
34 | | - return async(function() |
35 | | - local RefactorCommands = require('java-refactor.refactor-commands') |
36 | | - local refactor_commands = RefactorCommands(jdtls()) |
37 | | - refactor_commands:refactor(refactor_command) |
38 | | - end) |
39 | | - .catch(get_error_handler('failed to refactor variable')) |
40 | | - .run() |
| 23 | +---@private |
| 24 | +---@param action_type string |
| 25 | +---@param filter? string |
| 26 | +function M.run_code_action(action_type, filter) |
| 27 | + vim.lsp.buf.code_action({ |
| 28 | + apply = true, |
| 29 | + context = { |
| 30 | + diagnostics = vim.lsp.diagnostic.get_line_diagnostics(0), |
| 31 | + only = { action_type }, |
| 32 | + }, |
| 33 | + filter = filter and function(refactor) |
| 34 | + return refactor.command.arguments[1] == filter |
| 35 | + end or nil, |
| 36 | + }) |
41 | 37 | end |
42 | 38 |
|
43 | 39 | return M |
0 commit comments