This repository was archived by the owner on Nov 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjdtls-client.lua
More file actions
163 lines (138 loc) · 4.05 KB
/
jdtls-client.lua
File metadata and controls
163 lines (138 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
local log = require('java-core.utils.log')
local class = require('java-core.utils.class')
local async = require('java-core.utils.async')
local await = async.wait_handle_error
---@alias jdtls.RequestMethod
---| 'workspace/executeCommand'
---| 'java/inferSelection'
---| 'java/getRefactorEdit'
---@alias jdtls.CodeActionCommand
---| 'extractVariable'
---| 'assignVariable'
---| 'extractVariableAllOccurrence'
---| 'extractConstant'
---| 'extractMethod'
---| 'extractField'
---| 'extractInterface'
---| 'changeSignature'
---| 'assignField'
---| 'convertVariableToField'
---| 'invertVariable'
---| 'introduceParameter'
---| 'convertAnonymousClassToNestedCommand') {
---@class jdtls.RefactorWorkspaceEdit
---@field edit lsp.WorkspaceEdit
---@field command? lsp.Command
---@field errorMessage? string
---@class jdtls.SelectionInfo
---@field name string
---@field length number
---@field offset number
---@field params? string[]
---@class java-core.JdtlsClient
---@field client LspClient
local JdtlsClient = class()
function JdtlsClient:_init(client)
self.client = client
end
---@param args? { client: LspClient }
---@return java-core.JdtlsClient
function JdtlsClient:new(args)
local o = {
client = (args or {}).client,
}
setmetatable(o, self)
self.__index = self
return o
end
---Sends a LSP request
---@param method jdtls.RequestMethod
---@param params lsp.ExecuteCommandParams
---@param buffer? number
function JdtlsClient:request(method, params, buffer)
log.debug('sending LSP request: ' .. method)
return await(function(callback)
local on_response = function(err, result)
if err then
log.error(method .. ' failed! arguments: ', params, ' error: ', err)
else
log.debug(method .. ' success! response: ', result)
end
callback(err, result)
end
return self.client.request(method, params, on_response, buffer)
end)
end
---Executes a workspace/executeCommand and returns the result
---@param command string workspace command to execute
---@param params? lsp.LSPAny[]
---@param buffer? integer
---@return lsp.LSPAny
function JdtlsClient:workspace_execute_command(command, params, buffer)
return self:request('workspace/executeCommand', {
command = command,
arguments = params,
}, buffer)
end
---Returns more information about the object the cursor is on
---@param command jdtls.RequestMethod
---@param params lsp.CodeActionParams
---@param buffer? number
---@return jdtls.SelectionInfo[]
function JdtlsClient:java_infer_selection(command, params, buffer)
return self:request('java/inferSelection', {
command = command,
context = params,
}, buffer)
end
---Returns refactor details
---@param command jdtls.CodeActionCommand
---@param context lsp.CodeActionParams
---@param options lsp.FormattingOptions
---@param command_arguments jdtls.SelectionInfo[];
---@param buffer? number
---@return jdtls.RefactorWorkspaceEdit
function JdtlsClient:java_get_refactor_edit(
command,
context,
options,
command_arguments,
buffer
)
local params = {
command = command,
context = context,
options = options,
commandArguments = command_arguments,
}
return self:request('java/getRefactorEdit', params, buffer)
end
---Returns the decompiled class file content
---@param uri string uri of the class file
---@return string # decompiled file content
function JdtlsClient:java_decompile(uri)
---@type string
return self:workspace_execute_command('java.decompile', { uri })
end
function JdtlsClient:get_capability(...)
local capability = self.client.server_capabilities
for _, value in ipairs({ ... }) do
if type(capability) ~= 'table' then
log.fmt_warn('Looking for capability: %s in value %s', value, capability)
return nil
end
capability = capability[value]
end
return capability
end
---Returns true if the LS supports the given command
---@param command_name string name of the command
---@return boolean # true if the command is supported
function JdtlsClient:has_command(command_name)
local commands = self:get_capability('executeCommandProvider', 'commands')
if not commands then
return false
end
return vim.tbl_contains(commands, command_name)
end
return JdtlsClient