|
| 1 | +-- extract refactorings over the jdtls protocol (java/inferSelection + |
| 2 | +-- java/getRefactorEdit); nvim-jdtls is not required |
| 3 | +local lsp = require("jc.lsp") |
| 4 | + |
| 5 | +local M = {} |
| 6 | + |
| 7 | +local function code_action_params(client, visual) |
| 8 | + local encoding = client.offset_encoding |
| 9 | + local params |
| 10 | + if visual then |
| 11 | + params = vim.lsp.util.make_given_range_params(nil, nil, 0, encoding) |
| 12 | + else |
| 13 | + params = vim.lsp.util.make_range_params(0, encoding) |
| 14 | + end |
| 15 | + params.context = { diagnostics = {} } |
| 16 | + return params |
| 17 | +end |
| 18 | + |
| 19 | +local function apply_refactor_edit(err, result, ctx) |
| 20 | + if err then |
| 21 | + vim.notify("jc: refactor failed: " .. err.message, vim.log.levels.ERROR) |
| 22 | + return |
| 23 | + end |
| 24 | + if not result then |
| 25 | + return |
| 26 | + end |
| 27 | + if result.edit then |
| 28 | + local client = ctx and vim.lsp.get_client_by_id(ctx.client_id) |
| 29 | + vim.lsp.util.apply_workspace_edit(result.edit, client and client.offset_encoding or "utf-16") |
| 30 | + end |
| 31 | + -- jdtls may ask for a follow-up command (e.g. rename of the new symbol) |
| 32 | + if result.command then |
| 33 | + lsp.executeCommand(result.command, function() end) |
| 34 | + end |
| 35 | +end |
| 36 | + |
| 37 | +local function refactor(cmd, visual) |
| 38 | + local client = lsp.get_jdtls_client() |
| 39 | + if not client then |
| 40 | + vim.notify("jc: no jdtls client attached", vim.log.levels.ERROR) |
| 41 | + return |
| 42 | + end |
| 43 | + local action_params = code_action_params(client, visual) |
| 44 | + local params = { |
| 45 | + command = cmd, |
| 46 | + context = action_params, |
| 47 | + options = { |
| 48 | + tabSize = vim.lsp.util.get_effective_tabstop(), |
| 49 | + insertSpaces = vim.bo.expandtab, |
| 50 | + }, |
| 51 | + } |
| 52 | + local bufnr = vim.api.nvim_get_current_buf() |
| 53 | + local range = action_params.range |
| 54 | + local has_selection = range.start.character ~= range["end"].character or range.start.line ~= range["end"].line |
| 55 | + if has_selection then |
| 56 | + client:request("java/getRefactorEdit", params, apply_refactor_edit, bufnr) |
| 57 | + return |
| 58 | + end |
| 59 | + -- cursor position only: let jdtls infer what can be extracted here |
| 60 | + client:request("java/inferSelection", params, function(err, selections) |
| 61 | + if err or not selections or #selections == 0 then |
| 62 | + vim.notify("jc: nothing to extract at cursor", vim.log.levels.WARN) |
| 63 | + return |
| 64 | + end |
| 65 | + local function run(selection) |
| 66 | + params.commandArguments = { selection } |
| 67 | + client:request("java/getRefactorEdit", params, apply_refactor_edit, bufnr) |
| 68 | + end |
| 69 | + if #selections == 1 then |
| 70 | + run(selections[1]) |
| 71 | + else |
| 72 | + vim.ui.select(selections, { |
| 73 | + prompt = "Extract:", |
| 74 | + format_item = function(s) |
| 75 | + return s.name |
| 76 | + end, |
| 77 | + }, function(selection) |
| 78 | + if selection then |
| 79 | + run(selection) |
| 80 | + end |
| 81 | + end) |
| 82 | + end |
| 83 | + end, bufnr) |
| 84 | +end |
| 85 | + |
| 86 | +function M.extract_variable(visual) |
| 87 | + refactor("extractVariable", visual) |
| 88 | +end |
| 89 | + |
| 90 | +function M.extract_variable_all(visual) |
| 91 | + refactor("extractVariableAllOccurrence", visual) |
| 92 | +end |
| 93 | + |
| 94 | +function M.extract_constant(visual) |
| 95 | + refactor("extractConstant", visual) |
| 96 | +end |
| 97 | + |
| 98 | +function M.extract_method(visual) |
| 99 | + refactor("extractMethod", visual) |
| 100 | +end |
| 101 | + |
| 102 | +return M |
0 commit comments