/
jdtls.lua
924 lines (839 loc) · 27.3 KB
/
jdtls.lua
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
local api = vim.api
local ui = require('jdtls.ui')
local util = require('jdtls.util')
local with_java_executable = util.with_java_executable
local resolve_classname = util.resolve_classname
local execute_command = util.execute_command
local jdtls_dap = require('jdtls.dap')
local setup = require('jdtls.setup')
local M = {
setup_dap = jdtls_dap.setup_dap,
test_class = jdtls_dap.test_class,
test_nearest_method = jdtls_dap.test_nearest_method,
pick_test = jdtls_dap.pick_test,
extendedClientCapabilities = setup.extendedClientCapabilities,
start_or_attach = setup.start_or_attach,
setup = setup,
settings = {
jdt_uri_timeout_ms = 5000,
}
}
local request = vim.lsp.buf_request
local highlight_ns = api.nvim_create_namespace('jdtls_hl')
M.jol_path = nil
local function java_apply_workspace_edit(command)
for _, argument in ipairs(command.arguments) do
vim.lsp.util.apply_workspace_edit(argument)
end
end
local function java_generate_to_string_prompt(_, params)
request(0, 'java/checkToStringStatus', params, function(err, _, result)
if err then
print("Could not execute java/checkToStringStatus: " .. err.message)
return
end
if not result then return end
if result.exists then
local choice = vim.fn.inputlist({
string.format("Method 'toString()' already exists in '%s'. Do you want to replace it?", result.type),
"1. Replace",
"2. Cancel"
})
if choice < 1 or choice == 2 then
return
end
end
local fields = ui.pick_many(result.fields, 'Include item in toString?', function(x)
return string.format('%s: %s', x.name, x.type)
end)
request(0, 'java/generateToString', { context = params; fields = fields; }, function(e, _, edit)
if e then
print("Could not execute java/generateToString: " .. e.message)
return
end
if edit then
vim.lsp.util.apply_workspace_edit(edit)
end
end)
end)
end
local function java_generate_constructors_prompt(_, code_action_params)
request(0, 'java/checkConstructorsStatus', code_action_params, function(err0, _, status)
if err0 then
print("Could not execute java/checkConstructorsStatus: " .. err0.message)
return
end
if not status or not status.constructors or #status.constructors == 0 then
return
end
local constructors = status.constructors
if #status.constructors > 1 then
constructors = ui.pick_many(status.constructors, 'Include super class constructor(s): ', function(x)
return string.format('%s(%s)', x.name, table.concat(x.parameters, ','))
end)
if not constructors or #constructors == 0 then
return
end
end
local fields = status.fields
if fields then
fields = ui.pick_many(status.fields, 'Include field to initialize by constructor(s): ', function(x)
return string.format('%s: %s', x.name, x.type)
end)
if not fields or #fields == 0 then
return
end
end
local params = {
context = code_action_params,
constructors = constructors,
fields = fields
}
request(0, 'java/generateConstructors', params, function(err1, _, edit)
if err1 then
print("Could not execute java/generateConstructors: " .. err1.message)
elseif edit then
vim.lsp.util.apply_workspace_edit(edit)
end
end)
end)
end
local function java_generate_delegate_methods_prompt(_, code_action_params)
request(0, 'java/checkDelegateMethodsStatus', code_action_params, function(err0, _, status)
if err0 then
print('Could not execute java/checkDelegateMethodsStatus: ', err0.message)
return
end
if not status or not status.delegateFields or #status.delegateFields == 0 then
print('All delegatable methods are already implemented.')
return
end
local field = #status.delegateFields == 1 and status.delegateFields[1] or ui.pick_one(
status.delegateFields,
'Select target to generate delegates for.',
function(x) return string.format('%s: %s', x.field.name, x.field.type) end
)
if not field then
return
end
if #field.delegateMethods == 0 then
print('All delegatable methods are already implemented.')
return
end
local methods = ui.pick_many(field.delegateMethods, 'Generate delegate for method:', function(x)
return string.format('%s(%s)', x.name, table.concat(x.parameters, ','))
end)
if not methods or #methods == 0 then
return
end
local params = {
context = code_action_params,
delegateEntries = vim.tbl_map(
function(x)
return {
field = field.field,
delegateMethod = x
}
end,
methods
),
}
request(0, 'java/generateDelegateMethods', params, function(err1, _, workspace_edit)
if err1 then
print('Could not execute java/generateDelegateMethods', err1.message)
elseif workspace_edit then
vim.lsp.util.apply_workspace_edit(workspace_edit)
end
end)
end)
end
local function java_hash_code_equals_prompt(_, params)
request(0, 'java/checkHashCodeEqualsStatus', params, function(_, _, result)
if not result or not result.fields or #result.fields == 0 then
print(string.format("The operation is not applicable to the type %", result.type))
return
end
local fields = ui.pick_many(result.fields, 'Include item in equals/hashCode?', function(x)
return string.format('%s: %s', x.name, x.type)
end)
request(0, 'java/generateHashCodeEquals', { context = params; fields = fields; }, function(e, _, edit)
if e then
print("Could not execute java/generateHashCodeEquals: " .. e.message)
end
if edit then
vim.lsp.util.apply_workspace_edit(edit)
end
end)
end)
end
local function handle_refactor_workspace_edit(err, _, result)
if err then
print('Error getting refactoring edit: ' .. err.message)
return
end
if not result then
return
end
if result.edit then
vim.lsp.util.apply_workspace_edit(result.edit)
end
if result.command then
local command = result.command
local fn = M.commands[command.command]
if fn then
fn(command, {})
else
execute_command(command)
end
end
end
local function move_file(command, code_action_params)
local uri = command.arguments[3].uri
local params = {
moveKind = 'moveResource';
sourceUris = { uri, },
params = vim.NIL
}
request(0, 'java/getMoveDestinations', params, function(err, _, result)
assert(not err, err and err.message or vim.inspect(err))
if result and result.errorMessage then
print(result.errorMessage)
return
end
if not result or not result.destinations or #result.destinations == 0 then
print("Couldn't find any destination packages")
return
end
local destinations = vim.tbl_filter(
function(x) return not x.isDefaultPackage end,
result.destinations
)
ui.pick_one_async(
destinations,
'Target package> ',
function(x) return x.project .. ' » ' .. (x.isParentOfSelectedFile and '* ' or '') .. x.displayName end,
function(x)
local move_params = {
moveKind = 'moveResource',
sourceUris = { uri, },
params = code_action_params,
destination = x,
updateReferences = true
}
request(0, 'java/move', move_params, function(move_err, _, refactor_edit)
handle_refactor_workspace_edit(move_err, _, refactor_edit)
end)
end
)
end)
end
local function move_instance_method(command, code_action_params)
local params = {
moveKind = 'moveInstanceMethod';
sourceUris = { command.arguments[2].textDocument.uri, };
params = code_action_params
}
request(0, 'java/getMoveDestinations', params, function(err, _, result)
assert(not err, err and err.message or vim.inspect(err))
if result and result.errorMessage then
print(result.errorMessage)
return
end
if not result or not result.destinations or #result.destinations == 0 then
print("Couldn't find any destinations")
return
end
ui.pick_one_async(
result.destinations,
'Destination> ',
function(x)
local prefix
if x.isField then
prefix = '[Field] '
else
prefix = '[Method Parameter] '
end
return prefix .. x.type .. ' ' .. x.name
end,
function(x)
params.destination = x
params.updateReferences = true
request(0, 'java/move', params, function(move_err, _, refactor_edit)
handle_refactor_workspace_edit(move_err, _, refactor_edit)
end)
end
)
end)
end
local function search_symbols(project, enclosing_type_name, on_selection)
local params = {
query = '*',
projectName = project,
sourceOnly = true,
}
request(0, 'java/searchSymbols', params, function(err, _, result)
assert(not err, err and err.message or vim.inspect(err))
if not result or #result == 0 then
print("Couldn't find any destinations")
return
end
if enclosing_type_name then
result = vim.tbl_filter(
function(x)
if x.containerName then
return enclosing_type_name == x.containerName .. '.' .. x.name
else
return enclosing_type_name == x.name
end
end,
result
)
end
ui.pick_one_async(
result,
'Destination> ',
function(x) return x.containerName .. ' » ' .. x.name end,
on_selection
)
end)
end
local function move_static_member(command, code_action_params)
local member = command.arguments[3]
search_symbols(
member.projectName,
member.enclosingTypeName,
function(picked)
local move_params = {
moveKind = 'moveStaticMember',
sourceUris = { command.arguments[2].uri },
params = code_action_params,
destination = picked
}
request(0, 'java/move', move_params, function(move_err, _, refactor_edit)
handle_refactor_workspace_edit(move_err, _, refactor_edit)
end)
end
)
end
local function move_type(command, code_action_params)
local info = command.arguments[3]
if not info.supportedDestinationKinds or #info.supportedDestinationKinds == 0 then
print('No available destinations')
return
end
ui.pick_one_async(
info.supportedDestinationKinds,
'Action> ',
function(x)
if x == 'newFile' then
return string.format('Move type `%s` to new file', info.displayName)
else
return string.format('Move type `%s` to another class', info.displayName)
end
end,
function(x)
if x == 'newFile' then
local move_params = {
moveKind = 'moveTypeToNewFile',
sourceUris = { command.arguments[2].textDocument.uri },
params = code_action_params
}
request(0, 'java/move', move_params, function(move_err, _, refactor_edit)
handle_refactor_workspace_edit(move_err, _, refactor_edit)
end)
else
search_symbols(
info.projectName,
info.enclosingTypeName,
function(picked)
local move_params = {
moveKind = 'moveTypeToClass',
sourceUris = { command.arguments[2].uri },
params = code_action_params,
destination = picked
}
request(0, 'java/move', move_params, function(move_err, _, refactor_edit)
handle_refactor_workspace_edit(move_err, _, refactor_edit)
end)
end
)
end
end
)
end
local function java_apply_refactoring_command(command, code_action_params)
local cmd = command.arguments[1]
local params = {
command = cmd,
context = code_action_params,
options = {
tabSize = vim.lsp.util.get_effective_tabstop(),
insertSpaces = vim.bo.expandtab,
}
}
if cmd == 'moveFile' then
return move_file(command, code_action_params)
elseif cmd == 'moveInstanceMethod' then
return move_instance_method(command, code_action_params)
elseif cmd == 'moveStaticMember' then
return move_static_member(command, code_action_params)
elseif cmd == 'moveType' then
return move_type(command, code_action_params)
end
if not vim.tbl_contains(setup.extendedClientCapabilities.inferSelectionSupport, cmd) then
request(0, 'java/getRefactorEdit', params, handle_refactor_workspace_edit)
return
end
local range = code_action_params.range
if not (range.start.character == range['end'].character and range.start.line == range['end'].line) then
request(0, 'java/getRefactorEdit', params, handle_refactor_workspace_edit)
return
end
request(0, 'java/inferSelection', params, function(err, _, selection_info)
assert(not err, vim.inspect(err))
if not selection_info or #selection_info == 0 then
print('No selection found that could be extracted')
return
end
if #selection_info == 1 then
params.commandArguments = selection_info
request(0, 'java/getRefactorEdit', params, handle_refactor_workspace_edit)
else
ui.pick_one_async(
selection_info,
'Choices:',
function(x) return x.name end,
function(selection)
if not selection then return end
params.commandArguments = {selection}
request(0, 'java/getRefactorEdit', params, handle_refactor_workspace_edit)
end
)
end
end)
end
local function java_action_rename()
-- Ignored for now
end
local function java_action_organize_imports(_, code_action_params)
request(0, 'java/organizeImports', code_action_params, function(err, _, resp)
if err then
print('Error on organize imports: ' .. err.message)
return
end
if resp then
vim.lsp.util.apply_workspace_edit(resp)
end
end)
end
local function find_last(str, pattern)
local idx = nil
while true do
local i = string.find(str, pattern, (idx or 0) + 1)
if i == nil then
break
else
idx = i
end
end
return idx
end
local function java_choose_imports(resp)
local uri = resp[1]
local selections = resp[2]
local choices = {}
for _, selection in ipairs(selections) do
local start = selection.range.start
local buf = vim.uri_to_bufnr(uri)
api.nvim_win_set_buf(0, buf)
api.nvim_win_set_cursor(0, {start.line + 1, start.character})
api.nvim_command('normal! zvzz')
api.nvim_buf_add_highlight(
0, highlight_ns, 'IncSearch', start.line, start.character, selection.range['end'].character)
api.nvim_command("redraw")
local candidates = selection.candidates
local fqn = candidates[1].fullyQualifiedName
local type_name = fqn:sub(find_last(fqn, '%.') + 1)
local choice = #candidates == 1 and candidates[1] or ui.pick_one(
candidates,
'Choose type ' .. type_name .. ' to import',
function(x) return x.fullyQualifiedName end
)
api.nvim_buf_clear_namespace(0, highlight_ns, 0, -1)
table.insert(choices, choice)
end
return choices
end
M.commands = {
['java.apply.workspaceEdit'] = java_apply_workspace_edit;
['java.action.generateToStringPrompt'] = java_generate_to_string_prompt;
['java.action.hashCodeEqualsPrompt'] = java_hash_code_equals_prompt;
['java.action.applyRefactoringCommand'] = java_apply_refactoring_command;
['java.action.rename'] = java_action_rename;
['java.action.organizeImports'] = java_action_organize_imports;
['java.action.organizeImports.chooseImports'] = java_choose_imports;
['java.action.generateConstructorsPrompt'] = java_generate_constructors_prompt;
['java.action.generateDelegateMethodsPrompt'] = java_generate_delegate_methods_prompt;
}
if not vim.lsp.handlers['workspace/executeClientCommand'] then
vim.lsp.handlers['workspace/executeClientCommand'] = function(_, _, params) -- luacheck: ignore 122
local fn = M.commands[params.command]
if fn then
local ok, result = pcall(fn, params.arguments)
if ok then
return result
else
return vim.lsp.rpc_response_error(vim.lsp.protocol.ErrorCodes.InternalError, result)
end
else
return vim.lsp.rpc_response_error(
vim.lsp.protocol.ErrorCodes.MethodNotFound,
'Command ' .. params.command .. ' not supported on client'
)
end
end
end
local function within_range(outer, inner)
local o1y = outer.start.line
local o1x = outer.start.character
local o2y = outer['end'].line
local o2x = outer['end'].character
assert(o1y <= o2y, "Start must be before end: " .. vim.inspect(outer))
local i1y = inner.start.line
local i1x = inner.start.character
local i2y = inner['end'].line
local i2x = inner['end'].character
assert(i1y <= i2y, "Start must be before end: " .. vim.inspect(inner))
-- Outer: {}
-- Inner: []
-------------
-------------
-- { [ ]
-- }
-------------
-- {
-- [] }
-------------
if o1y < i1y then
-- {
-- [ ]
-- }
if o2y > i2y then
return true
end
-- {
-- [ } ]
return o2y == i2y and o2x >= i2x
elseif o1y == i1y then
if o2y > i2y then
-- { []
-- }
return true
else
-- { [ ] }
-- [ { ] }
return o2y == i2y and o1x <= i1x and o2x >= i2x
end
else
return false
end
end
local function get_diagnostics_for_range(bufnr, range)
local diagnostics = vim.lsp.diagnostic.get(bufnr)
if not diagnostics then return {} end
local line_diagnostics = {}
for _, diagnostic in ipairs(diagnostics) do
if within_range(diagnostic.range, range) then
table.insert(line_diagnostics, diagnostic)
end
end
if #line_diagnostics == 0 then
-- If there is no diagnostics at the cursor position,
-- see if there is at least something on the same line
for _, diagnostic in ipairs(diagnostics) do
if diagnostic.range.start.line == range.start.line then
table.insert(line_diagnostics, diagnostic)
end
end
end
return line_diagnostics
end
local function make_code_action_params(from_selection, kind)
local params
if from_selection then
params = vim.lsp.util.make_given_range_params()
else
params = vim.lsp.util.make_range_params()
end
local bufnr = api.nvim_get_current_buf()
params.context = {
diagnostics = get_diagnostics_for_range(bufnr, params.range),
only = kind,
}
return params
end
-- Similar to https://github.com/neovim/neovim/pull/11607, but with extensible commands
function M.code_action(from_selection, kind)
local code_action_params = make_code_action_params(from_selection or false, kind)
local function apply_command(action)
local command
if type(action.command) == "table" then
command = action.command
else
command = action
end
local fn = M.commands[command.command]
if fn then
fn(command, code_action_params)
else
execute_command(command)
end
end
request(0, 'textDocument/codeAction', code_action_params, function(err, _, actions, client_id)
local client = vim.lsp.get_client_by_id(client_id)
assert(not err, vim.inspect(err))
-- actions is (Command | CodeAction)[] | null
-- CodeAction
-- title: String
-- kind?: CodeActionKind
-- diagnostics?: Diagnostic[]
-- isPreferred?: boolean
-- edit?: WorkspaceEdit
-- command?: Command
--
-- Command
-- title: String
-- command: String
-- arguments?: any[]
if not actions or #actions == 0 then
print("No code actions available")
return
end
ui.pick_one_async(
actions,
'Code Actions:',
function(x)
return (x.title:gsub('\r\n', '\\r\\n')):gsub('\n', '\\n')
end,
function(action)
if not action then
return
end
if action.edit then
vim.lsp.util.apply_workspace_edit(action.edit)
elseif client
and type(client.resolved_capabilities.code_action) == 'table'
and client.resolved_capabilities.code_action.resolveProvider then
client.request('codeAction/resolve', action, function(err1, _, result)
assert(not err1, vim.inspect(err1))
if result.edit then
vim.lsp.util.apply_workspace_edit(result.edit)
end
apply_command(result)
end)
else
apply_command(action)
end
end
)
end)
end
function M.organize_imports()
java_action_organize_imports(nil, make_code_action_params(false))
end
function M._complete_compile()
return 'full\nincremental'
end
function M.compile(type)
local CompileWorkspaceStatus = {
FAILED = 0,
SUCCEED = 1,
WITHERROR = 2,
CANCELLED = 3,
}
request(0, 'java/buildWorkspace', type == 'full', function(err, _, result)
assert(not err, 'Error on `java/buildWorkspace`: ' .. vim.inspect(err))
if result == CompileWorkspaceStatus.SUCCEED then
print('Compile successfull')
else
vim.tbl_add_reverse_lookup(CompileWorkspaceStatus)
local diagnostics_by_buf = vim.lsp.diagnostic.get_all()
local project_config_errors = {}
local compile_errors = {}
for bufnr, diagnostics in pairs(diagnostics_by_buf) do
local fname = vim.uri_to_fname(vim.uri_from_bufnr(bufnr))
local stat = vim.loop.fs_stat(fname)
local items
if (vim.endswith(fname, 'build.gradle')
or vim.endswith(fname, 'pom.xml')
or (stat and stat.type == 'directory')) then
items = project_config_errors
elseif vim.fn.fnamemodify(fname, ':e') == 'java' then
items = compile_errors
end
for _, d in pairs(diagnostics) do
if d.severity == vim.lsp.protocol.DiagnosticSeverity.Error and items then
table.insert(items, {
bufnr = bufnr,
lnum = d.range.start.line + 1,
col = d.range.start.character + 1,
text = d.message,
vcol = 1
})
end
end
end
local items = #project_config_errors > 0 and project_config_errors or compile_errors
table.sort(items, function(a, b) return a.lnum < b.lnum end)
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = items })
if #items > 0 then
print(string.format('Compile error. (%s)', CompileWorkspaceStatus[result]))
vim.cmd('copen')
else
print('Compile error, but no error diagnostics available. Try running compile again.')
end
end
end)
end
function M.update_project_config()
local params = { uri = vim.uri_from_bufnr(0) }
request(0, 'java/projectConfigurationUpdate', params, function(err)
if err then
print('Could not update project configuration: ' .. err.message)
return
end
end)
end
local function mk_extract(entity)
return function(from_selection)
local params = make_code_action_params(from_selection or false)
java_apply_refactoring_command({ arguments = { entity }, }, params)
end
end
M.extract_constant = mk_extract('extractConstant')
M.extract_variable = mk_extract('extractVariable')
M.extract_method = mk_extract('extractMethod')
local function with_classpaths(fn)
local is_test_file_cmd = {
command = 'java.project.isTestFile',
arguments = { vim.uri_from_bufnr(0) }
};
execute_command(is_test_file_cmd, function(err, is_test_file)
assert(not err, vim.inspect(err))
local options = vim.fn.json_encode({
scope = is_test_file and 'test' or 'runtime';
})
local cmd = {
command = 'java.project.getClasspaths';
arguments = { vim.uri_from_bufnr(0), options };
}
execute_command(cmd, function(err1, resp)
if err1 then
print('Error executing java.project.getClasspaths: ' .. err1.message)
else
fn(resp)
end
end)
end)
end
function M.javap()
with_classpaths(function(resp)
local classname = resolve_classname()
local cp = table.concat(resp.classpaths, ':')
local buf = api.nvim_create_buf(false, true)
api.nvim_win_set_buf(0, buf)
vim.fn.termopen({'javap', '-c', '--class-path', cp, classname})
end)
end
function M.jshell()
with_classpaths(function(result)
local buf = api.nvim_create_buf(false, true)
local classpaths = {}
for _, path in pairs(result.classpaths) do
if vim.fn.filereadable(path) == 1 or vim.fn.isdirectory(path) == 1 then
table.insert(classpaths, path)
end
end
local cp = table.concat(classpaths, ':')
with_java_executable(resolve_classname(), '', function(java_exec)
api.nvim_win_set_buf(0, buf)
local jshell = vim.fn.fnamemodify(java_exec, ':h') .. '/jshell'
vim.fn.termopen({jshell, '--class-path', cp})
end)
end)
end
function M.jol(mode, classname)
mode = mode or 'estimates'
local jol = assert(M.jol_path, [[Path to jol must be set using `lua require('jdtls').jol_path = 'path/to/jol.jar'`]])
with_classpaths(function(resp)
local resolved_classname = resolve_classname()
local cp = table.concat(resp.classpaths, ':')
with_java_executable(resolved_classname, '', function(java_exec)
local buf = api.nvim_create_buf(false, true)
api.nvim_win_set_buf(0, buf)
vim.fn.termopen({
java_exec, '-Djdk.attach.allowAttachSelf', '-jar', jol, mode, '-cp', cp, classname or resolved_classname})
end)
end)
end
--- Reads the uri into the current buffer
--
-- This requires at least one open buffer that is connected to the jdtls
-- language server.
--
--@param uri expected to be a `jdt://` uri
function M.open_jdt_link(uri)
local client
for _, c in ipairs(vim.lsp.get_active_clients()) do
if c.config.init_options
and c.config.init_options.extendedClientCapabilities
and c.config.init_options.extendedClientCapabilities.classFileContentsSupport then
client = c
break
end
end
assert(client, 'Must have a buffer open with a language client connected to eclipse.jdt.ls to load JDT URI')
local buf = api.nvim_get_current_buf()
local params = {
uri = uri
}
local response = nil
local cb = function(err, _, result)
response = {err, result}
end
local ok, request_id = client.request('java/classFileContents', params, cb, buf)
assert(ok, 'Request to `java/classFileContents` must succeed to open JDT URI. Client shutdown?')
local timeout_ms = M.settings.jdt_uri_timeout_ms
local wait_ok, reason = vim.wait(timeout_ms, function() return response end)
local log_path = require('jdtls.path').join(vim.fn.stdpath('cache'), 'lsp.log')
local buf_content
if wait_ok and #response == 2 and response[2] then
local content = response[2]
if content == "" then
buf_content = {
'Received response from server, but it was empty. Check the log file for errors', log_path}
else
buf_content = vim.split(response[2], '\n', true)
end
else
local error_msg
if not wait_ok then
client.cancel_request(request_id)
local wait_failure = {
[-1] = 'timeout';
[-2] = 'interrupted';
[-3] = 'error'
}
error_msg = wait_failure[reason]
else
error_msg = response[1]
end
buf_content = {
'Failed to load content for uri', uri, '', 'Error was: ', error_msg, '', 'Check the log file for errors', log_path}
end
api.nvim_buf_set_option(buf, 'modifiable', true)
api.nvim_buf_set_lines(buf, 0, -1, false, buf_content)
api.nvim_buf_set_option(0, 'filetype', 'java')
api.nvim_buf_set_option(buf, 'modifiable', false)
end
return M