|
1 | 1 | local log = require('java-core.utils.log') |
2 | 2 | local lspconfig = require('lspconfig') |
3 | 3 | local server = require('java-core.server') |
| 4 | +local jdtls = require('java.jdtls') |
| 5 | +local get_error_handler = require('java.handlers.error') |
| 6 | + |
| 7 | +local JavaCoreJdtlsClient = require('java-core.ls.clients.jdtls-client') |
4 | 8 |
|
5 | 9 | local M = {} |
6 | 10 |
|
@@ -30,4 +34,49 @@ function M.wrap_lspconfig_setup() |
30 | 34 | end |
31 | 35 | end |
32 | 36 |
|
| 37 | +---@class BufReadCmdCallbackArgs |
| 38 | +---@field buf integer buffer number |
| 39 | +---@field event string name of the event |
| 40 | +---@field file string name of the file |
| 41 | +---@field id integer event id? |
| 42 | +---@field match string matched pattern in autocmd match |
| 43 | +function M.register_class_file_decomplier() |
| 44 | + vim.api.nvim_create_autocmd('BufReadCmd', { |
| 45 | + pattern = 'jdt://*', |
| 46 | + ---@param opts BufReadCmdCallbackArgs |
| 47 | + callback = function(opts) |
| 48 | + ---@type boolean |
| 49 | + local done = false |
| 50 | + local client_obj = jdtls() |
| 51 | + local buffer = opts.buf |
| 52 | + |
| 53 | + local function handle_file_content(text) |
| 54 | + local lines = vim.split(text, '\n') |
| 55 | + vim.api.nvim_buf_set_lines(buffer, 0, -1, true, lines) |
| 56 | + |
| 57 | + vim.bo[buffer].swapfile = false |
| 58 | + vim.bo[buffer].filetype = 'java' |
| 59 | + vim.bo[buffer].modifiable = false |
| 60 | + |
| 61 | + if not vim.lsp.buf_is_attached(buffer, client_obj.client.id) then |
| 62 | + vim.lsp.buf_attach_client(buffer, client_obj.client.id) |
| 63 | + end |
| 64 | + |
| 65 | + done = true |
| 66 | + end |
| 67 | + |
| 68 | + JavaCoreJdtlsClient:new(client_obj) |
| 69 | + :java_decompile(opts.file) |
| 70 | + :thenCall(handle_file_content) |
| 71 | + :catch( |
| 72 | + get_error_handler('failed to decompile the class at %s', opts.file) |
| 73 | + ) |
| 74 | + |
| 75 | + vim.wait(10000, function() |
| 76 | + return done |
| 77 | + end) |
| 78 | + end, |
| 79 | + }) |
| 80 | +end |
| 81 | + |
33 | 82 | return M |
0 commit comments