-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
jdtls.lua
1299 lines (1182 loc) · 39.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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---@mod jdtls LSP extensions for Neovim and eclipse.jdt.ls
local api = vim.api
local ui = require('jdtls.ui')
local util = require('jdtls.util')
local with_java_executable = util.with_java_executable
local with_classpaths = util.with_classpaths
local resolve_classname = util.resolve_classname
local execute_command = util.execute_command
local jdtls_dap = require('jdtls.dap')
local setup = require('jdtls.setup')
local offset_encoding = 'utf-16'
---@diagnostic disable-next-line: deprecated
local get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients
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,
setup = setup,
settings = {
jdt_uri_timeout_ms = 5000,
}
}
--- Start the language server (if not started), and attach the current buffer.
---
---@param config table<string, any> configuration. See |vim.lsp.start_client|
---@param opts? jdtls.start.opts
---@param start_opts? lsp.StartOpts options passed to vim.lsp.start
---@return integer|nil client_id
function M.start_or_attach(config, opts, start_opts)
return setup.start_or_attach(config, opts, start_opts)
end
local request = function(bufnr, method, params, handler)
local clients = get_clients({ bufnr = bufnr, name = "jdtls" })
local _, client = next(clients)
if not client then
vim.notify("No LSP client with name `jdtls` available", vim.log.levels.WARN)
return
end
local co
if not handler then
co = coroutine.running()
if co then
handler = function(err, result, ctx)
coroutine.resume(co, err, result, ctx)
end
end
end
client.request(method, params, handler, bufnr)
if co then
return coroutine.yield()
end
end
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, offset_encoding)
end
end
local function java_generate_to_string_prompt(_, outer_ctx)
local params = outer_ctx.params
local bufnr = assert(outer_ctx.bufnr, '`outer_ctx` must have bufnr property')
coroutine.wrap(function()
local err, result = request(bufnr, 'java/checkToStringStatus', params)
if err then
print("Could not execute java/checkToStringStatus: " .. err.message)
return
end
if not result then
return
end
if result.exists then
local prompt = string.format(
"Method 'toString()' already exists in '%s'. Do you want to replace it?",
result.type
)
local choice = ui.pick_one({"Replace", "Cancel"}, prompt, tostring)
if choice == "Cancel" 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)
local e, edit = request(bufnr, 'java/generateToString', { context = params; fields = fields; })
if e then
print("Could not execute java/generateToString: " .. e.message)
elseif edit then
vim.lsp.util.apply_workspace_edit(edit, offset_encoding)
end
end)()
end
local function java_generate_constructors_prompt(_, outer_ctx)
local bufnr = assert(outer_ctx.bufnr, '`outer_ctx` must have bufnr property')
coroutine.wrap(function()
local err0, status = request(bufnr, 'java/checkConstructorsStatus', outer_ctx.params)
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
local opts = {
is_selected = function(item)
return item.isSelected
end
}
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, opts)
end
local params = {
context = outer_ctx.params,
constructors = constructors,
fields = fields
}
local err1, edit = request(bufnr, 'java/generateConstructors', params)
if err1 then
print("Could not execute java/generateConstructors: " .. err1.message)
elseif edit then
vim.lsp.util.apply_workspace_edit(edit, offset_encoding)
end
end)()
end
local function java_generate_delegate_methods_prompt(_, outer_ctx)
local bufnr = assert(outer_ctx.bufnr, '`outer_ctx` must have bufnr property')
coroutine.wrap(function()
local err0, status = request(bufnr, 'java/checkDelegateMethodsStatus', outer_ctx.params)
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 = outer_ctx.params,
delegateEntries = vim.tbl_map(
function(x)
return {
field = field.field,
delegateMethod = x
}
end,
methods
),
}
local err1, workspace_edit = request(bufnr, 'java/generateDelegateMethods', params)
if err1 then
print('Could not execute java/generateDelegateMethods', err1.message)
elseif workspace_edit then
vim.lsp.util.apply_workspace_edit(workspace_edit, offset_encoding)
end
end)()
end
local function java_hash_code_equals_prompt(_, outer_ctx)
local bufnr = assert(outer_ctx.bufnr, '`outer_ctx` must have bufnr property')
local params = outer_ctx.params
coroutine.wrap(function()
local _, result = request(bufnr, 'java/checkHashCodeEqualsStatus', params)
if not result then
vim.notify("No result", vim.log.levels.INFO)
return
elseif not result.fields or #result.fields == 0 then
vim.notify(string.format("The operation is not applicable to the type %", result.type), vim.log.levels.WARN)
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)
local err, edit = request(bufnr, 'java/generateHashCodeEquals', { context = params; fields = fields; })
if err then
print("Could not execute java/generateHashCodeEquals: " .. err.message)
elseif edit then
vim.lsp.util.apply_workspace_edit(edit, offset_encoding)
end
end)()
end
local function handle_refactor_workspace_edit(err, result, ctx)
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, offset_encoding)
end
if result.command then
local command = result.command
local fn = M.commands[command.command]
if fn then
fn(command, ctx)
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, ctx)
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)
local name = x.project .. ' » ' .. (x.isParentOfSelectedFile and '* ' or '') .. x.displayName
local sourceset = string.match(x.path, "src/(%a+)/")
return (sourceset and sourceset or x.path) .. " » " .. name
end,
function(x)
local move_params = {
moveKind = 'moveResource',
sourceUris = { uri, },
params = code_action_params,
destination = x,
updateReferences = true
}
request(ctx.bufnr, '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, ctx)
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(ctx.bufnr, '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, ctx)
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,
function(x)
on_selection(x, ctx.bufnr)
end
)
end)
end
local function move_static_member(command, code_action_params)
local member = command.arguments[3]
search_symbols(
member.projectName,
member.enclosingTypeName,
function(picked, bufnr)
local move_params = {
moveKind = 'moveStaticMember',
sourceUris = { command.arguments[2].uri },
params = code_action_params,
destination = picked
}
request(bufnr, '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, bufnr)
local move_params = {
moveKind = 'moveTypeToClass',
sourceUris = { command.arguments[2].uri },
params = code_action_params,
destination = picked
}
request(bufnr, 'java/move', move_params, function(move_err, refactor_edit)
handle_refactor_workspace_edit(move_err, refactor_edit)
end)
end
)
end
end
)
end
---@return {tabSize: integer, insertSpaces: boolean}
local function format_opts()
return {
tabSize = vim.lsp.util.get_effective_tabstop(),
insertSpaces = vim.bo.expandtab,
}
end
---@param bufnr integer
---@param command table
---@param code_action_params table
local function change_signature(bufnr, command, code_action_params)
local cmd_name = command.arguments[1]
local signature = command.arguments[3]
local edit_buf = api.nvim_create_buf(false, true)
api.nvim_create_autocmd("BufUnload", {
buffer = edit_buf,
once = true,
callback = function(args)
local lines = api.nvim_buf_get_lines(args.buf, 0, -1, true)
local is_delegate = false
local access_type = signature.modifier
local method_name = signature.methodName
local return_type = signature.returnType
local preview = false
local expect_param_next = false
local parameters = {}
local new_param_idx = #signature.parameters
for _, line in ipairs(lines) do
if vim.startswith(line, "---") then
break
elseif expect_param_next and vim.startswith(line, "- ") then
local matches = { line:match("%- ((%d+):) ([^ ]+) (%w+)") }
if next(matches) then
table.insert(parameters, {
name = matches[4],
originalIndex = assert(tonumber(matches[2]), "Parameter must have originalIndex"),
type = matches[3],
})
else
matches = { line:match("%- (%w+) ([^ ]+) ?(.*)") }
if next(matches) then
table.insert(parameters, {
type = matches[1],
name = matches[2],
defaultValue = matches[3],
originalIndex = new_param_idx
})
new_param_idx = new_param_idx + 1
end
end
elseif vim.startswith(line, "Access type: ") then
access_type = line:sub(#"Access type: " + 1)
elseif vim.startswith(line, "Name: ") then
method_name = line:sub(#"Name: " + 1)
elseif vim.startswith(line, "Parameters:") then
expect_param_next = true
elseif vim.startswith(line, "Return type: ") then
return_type = line:sub(#"Return type: " + 1)
end
end
local params = {
command = cmd_name,
context = code_action_params,
options = format_opts(),
commandArguments = {
signature.methodIdentifier,
is_delegate,
method_name,
access_type,
return_type,
parameters,
signature.exceptions,
preview
},
}
request(bufnr, 'java/getRefactorEdit', params, handle_refactor_workspace_edit)
end,
})
vim.bo[edit_buf].bufhidden = "wipe"
local width = math.floor(vim.o.columns * 0.9)
local height = math.floor(vim.o.lines * 0.8)
local win_opts = {
relative = "editor",
style = "minimal",
row = math.floor((vim.o.lines - height) * 0.5),
col = math.floor((vim.o.columns - width) * 0.5),
width = width,
height = height,
border = "single",
}
api.nvim_open_win(edit_buf, true, win_opts)
local lines = {
"Access type: " .. signature.modifier,
"Name: " .. signature.methodName,
"Return type: " .. signature.returnType,
"Parameters:",
}
for _, param in ipairs(signature.parameters) do
table.insert(lines, string.format("- %d: %s %s",
param.originalIndex,
param.type,
param.name
))
end
local comment_start = #lines + 1
vim.list_extend(lines, {
"",
string.rep("-", math.max(width, 3)),
"Labels are used to parse the values. Keep them!",
"Accept change & close the window:",
" - `<Ctrl-w> q`",
" - `:bd`",
"",
"Parameters:",
" - Order sensitive",
" - New param format: '- <type> <name> [defaultValue]'",
" - Existing param format: '- <n>: <type> <name>'",
" - <n> marks the original index, don't add it for new entries, don't change for moved params",
})
api.nvim_buf_set_lines(edit_buf, 0, -1, true, lines)
local highlights = {
{0, "Access type:", "Identifier"},
{1, "Name:", "Identifier"},
{2, "Return type:", "Identifier"},
{3, "Parameters:", "Identifier"},
}
for _, hl in ipairs(highlights) do
api.nvim_buf_set_extmark(edit_buf, highlight_ns, hl[1], 0, {
end_row = hl[1],
end_col = #hl[2],
hl_group = hl[3],
})
end
api.nvim_buf_set_extmark(edit_buf, highlight_ns, comment_start, 0, {
hl_group = "Comment",
end_row = #lines
})
end
---@param after_refactor? function
local function java_apply_refactoring_command(command, outer_ctx, after_refactor)
local cmd = command.arguments[1]
local bufnr = outer_ctx.bufnr
local code_action_params = outer_ctx.params
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)
elseif cmd == "changeSignature" then
return change_signature(bufnr, command, code_action_params)
end
local params = {
command = cmd,
context = code_action_params,
options = format_opts(),
}
local apply_refactor = function(err, result, ctx)
handle_refactor_workspace_edit(err, result, ctx)
if after_refactor then
after_refactor()
end
end
if not vim.tbl_contains(setup.extendedClientCapabilities.inferSelectionSupport, cmd) then
request(bufnr, 'java/getRefactorEdit', params, apply_refactor)
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(bufnr, 'java/getRefactorEdit', params, apply_refactor)
return
end
request(bufnr, 'java/inferSelection', params, function(err, selection_info, ctx)
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(ctx.bufnr, 'java/getRefactorEdit', params, apply_refactor)
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(ctx.bufnr, 'java/getRefactorEdit', params, apply_refactor)
end
)
end
end)
end
local function java_action_rename(command, ctx)
local target = command.arguments[1]
local win = api.nvim_get_current_win()
local bufnr = api.nvim_win_get_buf(win)
if bufnr ~= ctx.bufnr then
return
end
local lines = vim.api.nvim_buf_get_lines(ctx.bufnr, 0, -1, true)
local content = table.concat(lines, '\n')
local byteidx = vim.fn.byteidx(content, target.offset)
local line = vim.fn.byte2line(byteidx)
local col = byteidx - vim.fn.line2byte(line)
api.nvim_win_set_cursor(win, { line, col + 1 })
end
local function java_action_organize_imports(_, ctx)
request(0, 'java/organizeImports', ctx.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, offset_encoding)
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
local function java_override_methods(_, context)
local bufnr = assert(context.bufnr, '`context` must have bufnr property')
coroutine.wrap(function()
local err1, result1 = request(bufnr, 'java/listOverridableMethods', context.params)
if err1 then
vim.notify("Error getting overridable methods: " .. err1.message, vim.log.levels.WARN)
return
end
if not result1 or not result1.methods then
vim.notify("No methods to override", vim.log.levels.INFO)
return
end
local fmt = function(method)
return string.format("%s(%s) class: %s", method.name, table.concat(method.parameters, ", "), method.declaringClass)
end
local selected = ui.pick_many(result1.methods, "Method to override", fmt)
if #selected < 1 then
return
end
local params = {
context = context.params,
overridableMethods = selected
}
local err2, result2 = request(context.bufnr, 'java/addOverridableMethods', params)
if err2 ~= nil then
print("Error getting workspace edits: " .. err2.message)
return
end
if result2 then
vim.lsp.util.apply_workspace_edit(result2, offset_encoding)
end
end)()
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;
['java.action.overrideMethodsPrompt'] = java_override_methods;
['_java.test.askClientForChoice'] = function(args)
local prompt = args[1]
local choices = args[2]
local pick_many = args[3]
return require("jdtls.tests")._ask_client_for_choice(prompt, choices, pick_many)
end,
['_java.test.advancedAskClientForChoice'] = function(args)
local prompt = args[1]
local choices = args[2]
-- local advanced_action = args[3]
local pick_many = args[4]
return require("jdtls.tests")._ask_client_for_choice(prompt, choices, pick_many)
end,
['_java.test.askClientForInput'] = function(args)
local prompt = args[1]
local default = args[2]
local result = vim.fn.input({
prompt = prompt .. ': ',
default = default
})
return result and result or vim.NIL
end,
}
if vim.lsp.commands then
for k, v in pairs(M.commands) do
vim.lsp.commands[k] = v -- luacheck: ignore 122
end
end
if not vim.lsp.handlers['workspace/executeClientCommand'] then
vim.lsp.handlers['workspace/executeClientCommand'] = function(_, params, ctx) -- luacheck: ignore 122
local client = vim.lsp.get_client_by_id(ctx.client_id) or {}
local commands = client.commands or {}
local global_commands = vim.lsp.commands or M.commands
local fn = commands[params.command] or global_commands[params.command]
if fn then
local ok, result = pcall(fn, params.arguments, ctx)
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 make_code_action_params(from_selection)
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 = vim.lsp.diagnostic.get_line_diagnostics(bufnr),
}
return params
end
--- Organize the imports in the current buffer
function M.organize_imports()
java_action_organize_imports(nil, { params = make_code_action_params(false) })
end
---@private
function M._complete_compile()
return 'full\nincremental'
end
local function on_build_result(err, result, ctx)
local CompileWorkspaceStatus = {
FAILED = 0,
SUCCEED = 1,
WITHERROR = 2,
CANCELLED = 3,
}
assert(not err, 'Error trying to build project(s): ' .. vim.inspect(err))
if result == CompileWorkspaceStatus.SUCCEED then
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = {} })
print('Compile successful')
else
vim.tbl_add_reverse_lookup(CompileWorkspaceStatus)
local project_config_errors = {}
local compile_errors = {}
local ns = vim.lsp.diagnostic.get_namespace(ctx.client_id)
for _, d in pairs(vim.diagnostic.get(nil, { namespace = ns })) do
local fname = api.nvim_buf_get_name(d.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
if d.severity == vim.diagnostic.severity.ERROR and items then
table.insert(items, d)
end
end
local items = #project_config_errors > 0 and project_config_errors or compile_errors
vim.fn.setqflist({}, 'r', { title = 'jdtls'; items = vim.diagnostic.toqflist(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."
.. " Save all pending changes and try running compile again."
.. " If you used incremental mode, try a full rebuild.")
end
end
end
--- Compile the Java workspace
--- If there are compile errors they'll be shown in the quickfix list.
---@param type string|nil
---|"full"
---|"incremental"
function M.compile(type)
request(0, 'java/buildWorkspace', type == 'full', on_build_result)
end
---@param mode nil|"prompt"|"all"
local function pick_projects(mode)
local command = {
command = 'java.project.getAll',
}
local bufnr = api.nvim_get_current_buf()
assert(coroutine.running(), '`pick_projects` must be called within coroutine')
local err, projects = util.execute_command(command, nil, bufnr)
if err then
error(err.message or vim.inspect(err))
end
local selection
if mode == "all" then
selection = projects
elseif #projects == 1 then
selection = projects
else
selection = ui.pick_many(
projects,
'Projects> ',
function(project)
return project
end
)
end
return selection
end
--- Trigger a rebuild of one or more projects.
---
---@param opts JdtBuildProjectOpts|nil optional configuration options
function M.build_projects(opts)
opts = opts or {}
local bufnr = api.nvim_get_current_buf()
coroutine.wrap(function()
local selection = pick_projects(opts.select_mode or "prompt")
if selection and next(selection) then
local params = {
identifiers = vim.tbl_map(function(project) return { uri = project } end, selection),
isFullBuild = opts.full_build == nil and true or opts.full_build
}
request(bufnr, 'java/buildProjects', params, on_build_result)
end
end)()
end
---@class JdtBuildProjectOpts
---@field select_mode? JdtProjectSelectMode Show prompt to select projects or select all. Defaults to "prompt"
---@field full_build? boolean full rebuild or incremental build. Defaults to true (full build)
--- Update the project configuration (from Gradle or Maven).
--- In a multi-module project this will only update the configuration of the
--- module of the current buffer.
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)
end
end)
end
--- Process changes made to the Gradle or Maven configuration of one or more projects.
--- Requires eclipse.jdt.ls >= 1.13.0
---
---@param opts JdtUpdateProjectsOpts|nil configuration options
function M.update_projects_config(opts)
opts = opts or {}
coroutine.wrap(function()
local bufnr = api.nvim_get_current_buf()
local selection = pick_projects(opts.select_mode or "prompt")
if selection and next(selection) then
local params = {
identifiers = vim.tbl_map(function(project) return { uri = project } end, selection)
}
vim.lsp.buf_notify(bufnr, 'java/projectConfigurationsUpdate', params)
end