-
Notifications
You must be signed in to change notification settings - Fork 111
/
lsp.kak
1949 lines (1731 loc) · 73.2 KB
/
lsp.kak
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
### Options and faces ###
# Feel free to update path and arguments according to your setup when sourcing lsp.kak directly.
# Sourcing via `kak-lsp --kakoune` does it automatically.
declare-option -docstring "Command with which lsp is run" str lsp_cmd "kak-lsp -s %val{session}"
# Faces
# Faces used by inline diagnostics.
set-face global DiagnosticError red
set-face global DiagnosticHint default
set-face global DiagnosticInfo default
set-face global DiagnosticWarning yellow
# Faces used by inlay diagnostics.
set-face global InlayDiagnosticError DiagnosticError
set-face global InlayDiagnosticHint DiagnosticHint
set-face global InlayDiagnosticInfo DiagnosticInfo
set-face global InlayDiagnosticWarning DiagnosticWarning
# Faces used by line flags
set-face global LineFlagError red
set-face global LineFlagHint default
set-face global LineFlagInfo default
set-face global LineFlagWarning yellow
# Face for highlighting references.
set-face global Reference MatchingChar
set-face global ReferenceBind +u@Reference
# Face for inlay hints.
set-face global InlayHint cyan+d
# Options for tuning kak-lsp behaviour.
# Display hover info anchored to the hovered position.
declare-option -docstring "Display hover info anchored to the hovered position" bool lsp_hover_anchor false
# Completions request is sent only when this expression doesn't fail.
# By default, it ensures that preceding character is not a whitespace.
declare-option -docstring "Completion request is sent only when this expression does not fail" str lsp_completion_trigger %{execute-keys '<a-h><a-k>\S.\z<ret>'}
# Kakoune requires completions to point fragment start rather than cursor position.
# This variable provides a way to customise how fragment start is detected.
# By default, it tracks back to the first punctuation or whitespace.
declare-option -docstring "Select from cursor to the start of the term being completed" str lsp_completion_fragment_start %{execute-keys "<esc><a-h>s\$?[\w%opt{lsp_extra_word_chars}]+.\z<ret>"}
declare-option -hidden str lsp_extra_word_chars
hook global WinSetOption extra_word_chars=.* %{
set-option window lsp_extra_word_chars %sh{
eval set -- $kak_quoted_opt_extra_word_chars
for char; do
case "$char" in
(-) printf '\\-' ;;
(\<) printf '<lt>' ;;
(\\) printf '\\\\' ;;
(]) printf '\\]' ;;
(^) printf '\\^' ;;
(*) printf %s "$char" ;;
esac
done
}
}
# If hover in insert mode is enabled then request is made only when this expression doesn't fail and
# for position at which it moves cursor; by default, it ensures that cursor is after opening parens
# and then moves cursor to opening parens to request hover info for current function; note that it
# doesn't handle well nested function calls.
declare-option -docstring "If hover in insert mode is enabled then request is made only when this expression does not fail and for position at which it moves cursor" \
str lsp_hover_insert_mode_trigger %{execute-keys '<a-f>(s\A[^)]+[)]?\z<ret>'}
# Formatting: prefer spaces over tabs.
declare-option -docstring "Prefer spaces over tabs" bool lsp_insert_spaces true
# Set to true to automatically highlight references with Reference face.
declare-option -docstring "Automatically highlight references with Reference face" bool lsp_auto_highlight_references false
# Set to true to highlight when code actions are available.
declare-option -docstring "Show available code actions (default: a 💡 in the modeline)" bool lsp_auto_show_code_actions false
# Set it to a positive number to limit the size of the lsp-hover output.
# (e.g. `set global lsp_hover_max_lines 40` would cut hover down to 40 lines)
declare-option -docstring "Set it to a positive number to limit the size of the lsp hover output" int lsp_hover_max_lines 0
declare-option -docstring "Dynamic TOML configuration string. Currently supports
- [language.<filetype>.settings]
" str lsp_config
# Highlight TOML keys in kakrc if they are supported by dynamic configuration.
try %{
add-highlighter shared/kakrc/code/lsp_keywords regex \[(language\.[a-z_]+\.settings(?:\.[^\]]*)?)\] 1:title
} catch %{
hook global -once ModuleLoaded kak %{
add-highlighter shared/kakrc/code/lsp_keywords regex \[(language\.[a-z_]+\.settings(?:\.[^\]]*)?)\] 1:title
}
}
declare-option -docstring "DEPRECATED, use %opt{lsp_config}. Configuration to send in workspace/didChangeConfiguration messages" str-to-str-map lsp_server_configuration
declare-option -docstring "DEPRECATED, use %opt{lsp_config}. Configuration to send in initializationOptions of Initialize messages." str-to-str-map lsp_server_initialization_options
# Line flags for inline diagnostics.
declare-option -docstring "Character to signal an error in the gutter" str lsp_diagnostic_line_error_sign '*'
declare-option -docstring "Character to signal a hint in the gutter" str lsp_diagnostic_line_hint_sign '-'
declare-option -docstring "Character to signal an info in the gutter" str lsp_diagnostic_line_info_sign 'i'
declare-option -docstring "Character to signal a warning in the gutter" str lsp_diagnostic_line_warning_sign '!'
# Visual settings for inlay diagnostics
declare-option -docstring "Character to represent a single inlay diagnostic of many on a line. May not contain '|'" str lsp_inlay_diagnostic_sign '■'
declare-option -docstring "Character(s) to separate the actual line contents from the inlay diagnostics. May not contain '|'" str lsp_inlay_diagnostic_gap ' '
# Another good default:
# set-option global lsp_diagnostic_line_error_sign '▓'
# set-option global lsp_diagnostic_line_warning_sign '▒'
# This is used to render lsp-hover responses.
# By default it shows both hover info and diagnostics.
# The string is `eval`ed to produce the content to display, so anything sent to stdout will
# show up in the info box.
declare-option -docstring "Format hover info" str lsp_show_hover_format %{
printf "%s\n\n" "${lsp_info}"
if [ -n "${lsp_diagnostics}" ]; then
printf "{+b@InfoDefault}Diagnostics:{InfoDefault}\n%s" "${lsp_diagnostics}"
fi
}
# If you want to see only hover info, try
# set-option global lsp_show_hover_format 'printf %s "${lsp_info}"'
declare-option -docstring %{Defines location patterns for lsp-next-location and lsp-previous-location.
Default locations look like "file:line[:column][:message]"
Capture groups must be:
1: filename
2: line number
3: optional column
4: optional message
} regex lsp_location_format ^\h*\K([^:\n]+):(\d+)\b(?::(\d+)\b)?(?::([^\n]+))
# Callback functions. Override these to tune kak-lsp's behavior.
define-command -hidden lsp-show-code-actions -params 1.. -docstring "Called when code actions are available for the main cursor position" %{
set-option buffer lsp_modeline "💡 "
}
define-command -hidden lsp-hide-code-actions -docstring "Called when no code action is available for the main cursor position" %{
set-option buffer lsp_modeline ""
}
define-command -hidden lsp-perform-code-action -params 1.. -docstring "Called on :lsp-code-actions" %{
menu %arg{@}
}
# Options for information exposed by kak-lsp.
# Count of diagnostics published for the current buffer.
declare-option -docstring "Number of errors" int lsp_diagnostic_error_count 0
declare-option -docstring "Number of hints" int lsp_diagnostic_hint_count 0
declare-option -docstring "Number of infos" int lsp_diagnostic_info_count 0
declare-option -docstring "Number of warnings" int lsp_diagnostic_warning_count 0
# Internal variables.
declare-option -hidden completions lsp_completions
declare-option -hidden range-specs lsp_errors
declare-option -hidden line-specs lsp_error_lines 0 '0| '
declare-option -hidden range-specs cquery_semhl
declare-option -hidden int lsp_timestamp -1
declare-option -hidden range-specs lsp_references
declare-option -hidden range-specs lsp_semantic_tokens
declare-option -hidden range-specs rust_analyzer_inlay_hints
declare-option -hidden range-specs lsp_diagnostics
declare-option -hidden str lsp_project_root
declare-option -hidden str lsp_modeline
set-option global modelinefmt "%%opt{lsp_modeline}%opt{modelinefmt}"
### Requests ###
define-command lsp-start -docstring "Start kak-lsp session" %{ nop %sh{ (eval "${kak_opt_lsp_cmd}") > /dev/null 2>&1 < /dev/null & } }
define-command -hidden lsp-did-change -docstring "Notify language server about buffer change" %{
lsp-did-change-and-then nop
}
define-command -hidden lsp-did-change-and-then -params 1 -docstring %{
Notify language server about buffer change and eval another command afterwards.
} %{ try %{
evaluate-commands %sh{
if [ $kak_opt_lsp_timestamp -eq $kak_timestamp ]; then
echo "fail"
fi
}
declare-option -hidden str lsp_callback "evaluate-commands -client %val{client} %arg{1}"
set-option buffer lsp_timestamp %val{timestamp}
evaluate-commands -save-regs '|' %{
set-register '|' %{
# dump stdin synchronously
# append a . to the end, otherwise the subshell strips trailing newlines
lsp_draft=$(cat; printf '.')
# and process it asynchronously
(
# replace \ with \\
# " with \"
# <tab> with \t
lsp_draft=$(printf '%s' "$lsp_draft" | sed 's/\\/\\\\/g ; s/"/\\"/g ; s/'"$(printf '\t')"'/\\t/g')
# remove the trailing . we added earlier
lsp_draft=${lsp_draft%.}
printf '
session = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/didChange"
[params]
draft = """
%s"""
' "${kak_session}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "${lsp_draft}" | eval "${kak_opt_lsp_cmd} --request"
printf %s\\n "${kak_opt_lsp_callback}" | kak -p "${kak_session}"
) > /dev/null 2>&1 < /dev/null & }
execute-keys -draft '%<a-|><ret>'
}
} catch %{
evaluate-commands %arg{1}
}}
define-command -hidden lsp-completion -docstring "Request completions for the main cursor position" %{
lsp-did-change-and-then lsp-completion-request
}
declare-option -hidden bool lsp_have_kakoune_feature_filtertext
declare-option -hidden completions lsp_have_kakoune_feature_filtertext_tmp
try %{
set-option global lsp_have_kakoune_feature_filtertext_tmp 1.1@0 insert_text|filter_text|on_select|menu
set-option global lsp_have_kakoune_feature_filtertext true
}
define-command -hidden lsp-completion-request -docstring "Request completions for the main cursor position" %{
try %{
# Fail if preceding character is a whitespace (by default; the trigger could be customized).
evaluate-commands -draft %opt{lsp_completion_trigger}
# Kakoune requires completions to point fragment start rather than cursor position.
# We try to detect it and put into lsp_completion_offset and then pass via completion.offset
# parameter to the kak-lsp server so it can use it when sending completions back.
declare-option -hidden str lsp_completion_offset
set-option window lsp_completion_offset %val{cursor_column}
evaluate-commands -draft %{
try %{
evaluate-commands %opt{lsp_completion_fragment_start}
set-option window lsp_completion_offset %val{cursor_column}
}
}
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/completion"
[params.position]
line = %d
column = %d
[params.completion]
offset = %d
[params]
have_kakoune_feature_filtertext = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} ${kak_opt_lsp_completion_offset} ${kak_opt_lsp_have_kakoune_feature_filtertext} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}}
define-command lsp-hover -docstring "Request hover info for the main cursor position" %{
lsp-did-change-and-then lsp-hover-request
}
define-command -hidden lsp-hover-request -docstring "Request hover info for the main cursor position" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/hover"
[params]
position.line = %d
position.column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
declare-option -hidden str lsp_symbol_kind_completion %{
symbol_kinds="\
File Module Namespace Package Class Method Property Field Constructor Enum Interface
Function Variable Constant String Number Boolean Array Object Key Null EnumMember Struct
Event Operator TypeParameter"
printf '%s\n' ${symbol_kinds}
}
define-command lsp-previous-symbol -params 0.. -shell-script-candidates %opt{lsp_symbol_kind_completion} \
-docstring "lsp-previous-symbol [<symbol-kinds>...]: goto the buffer's previous symbol of a type in <symbol-kinds>, or of any type" %{
lsp-did-change-and-then "lsp-next-or-previous-symbol previous goto %arg{@}"
}
define-command lsp-next-symbol -params 0.. -shell-script-candidates %opt{lsp_symbol_kind_completion} \
-docstring "lsp-next-symbol [<symbol-kinds>...]: goto the buffer's next symbol of a type in <symbol-kinds>, or of any type" %{
lsp-did-change-and-then "lsp-next-or-previous-symbol next goto %arg{@}"
}
define-command lsp-hover-previous-symbol -params 0.. -shell-script-candidates %opt{lsp_symbol_kind_completion} \
-docstring "lsp-hover-previous-symbol [<symbol-kinds>...]: show hover of the buffer's current or previous symbol of a type in <symbol-kinds>, or of any type" %{
lsp-did-change-and-then "lsp-next-or-previous-symbol previous hover %arg{@}"
}
define-command lsp-hover-next-symbol -params 0.. -shell-script-candidates %opt{lsp_symbol_kind_completion} \
-docstring "lsp-hover-next-symbol [<symbol-kinds>...]: show hover of the buffer's next symbol of a type in <symbol-kinds>, or of any type" %{
lsp-did-change-and-then "lsp-next-or-previous-symbol next hover %arg{@}"
}
# Requests for hover/goto next/previous symbol are funneled through this command
define-command lsp-next-or-previous-symbol -hidden -params 2.. %{
nop %sh{
forward="false"
if [ "$1" = "next" ]; then
forward="true"
fi
shift
hover="true"
if [ "$1" = "goto" ]; then
hover="false"
fi
shift
symbol_kinds="[ $( [ $# -gt 0 ] && printf '"%s",' "$@" ) ]"
(printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "kak-lsp/next-or-previous-symbol"
[params]
position.line = %d
position.column = %d
symbolKinds = %s
searchNext = %s
hover = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} "$symbol_kinds" "$forward" "$hover" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
} -shell-script-candidates %{
case $# in
# Search forward or backward?
(1) printf '%s\n' previous next ;;
# Show hover info or goto symbol?
(2) printf '%s\n' hover goto ;;
# Which symbol types?
(*) eval "$kak_opt_lsp_symbol_kind_completion" ;;
esac
}
## Convenience methods
define-command lsp-hover-next-function -docstring "Show hover of the next function in the buffer" %{
lsp-hover-next-symbol Method Function
}
define-command lsp-hover-previous-function -docstring "Show hover of the current or previous function in the buffer" %{
lsp-hover-previous-symbol Method Function
}
define-command lsp-next-function -docstring "Goto the next function in the buffer" %{
lsp-next-symbol Method Function
}
define-command lsp-previous-function -docstring "Goto the current or previous function in the buffer" %{
lsp-previous-symbol Method Function
}
define-command lsp-definition -docstring "Go to definition" %{
lsp-did-change-and-then lsp-definition-request
}
define-command -hidden lsp-definition-request -docstring "Go to definition" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/definition"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-implementation -docstring "Go to implementation" %{
lsp-did-change-and-then lsp-implementation-request
}
define-command -hidden lsp-implementation-request -docstring "Go to implementation" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/implementation"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-type-definition -docstring "Go to type-definition" %{
lsp-did-change-and-then lsp-type-definition-request
}
define-command -hidden lsp-type-definition-request -docstring "Go to type definition" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/typeDefinition"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-code-actions -docstring "Perform code actions for the main cursor position" %{
lsp-did-change-and-then 'lsp-code-actions-request true'
}
define-command -hidden lsp-code-actions-request -params 1 -docstring "Request code actions for the main cursor position" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/codeAction"
[params]
position.line = %d
position.column = %d
performCodeAction = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} "$1" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command -hidden lsp-execute-command -params 2 -docstring "Execute a command" %{
declare-option -hidden str lsp_execute_command_command %arg{1}
declare-option -hidden str lsp_execute_command_arguments %arg{2}
lsp-did-change-and-then %{lsp-execute-command-request %opt{lsp_execute_command_command} %opt{lsp_execute_command_arguments}}
}
define-command -hidden lsp-execute-command-request -params 2 -docstring "Execute a command" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "workspace/executeCommand"
[params]
command = "%s"
arguments = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "$1" "$2" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-references -docstring "Open buffer with symbol references" %{
lsp-did-change-and-then lsp-references-request
}
define-command -hidden lsp-references-request -docstring "Open buffer with symbol references" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/references"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-highlight-references -docstring "Highlight symbol references" %{
lsp-did-change-and-then lsp-highlight-references-request
}
define-command -hidden lsp-highlight-references-request -docstring "Highlight symbol references" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/documentHighlight"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-rename -params 1 -docstring "lsp-rename <new-name>: rename symbol under the main cursor" %{
lsp-did-change-and-then "lsp-rename-request ""%arg{1}"""
}
define-command -hidden lsp-rename-request -params 1 -docstring "Rename symbol under the main cursor" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/rename"
[params]
newName = "%s"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "$1" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-rename-prompt -docstring "Rename symbol under the main cursor (prompt for a new name)" %{
evaluate-commands -save-regs ^s %{
execute-keys -save-regs "" Z
try %{
execute-keys <space><a-i>w
# include a leading single-quote for Rust lifetime specifiers
execute-keys <a-semicolon>Hs'?\w+<ret><a-semicolon>
} catch %{
fail "lsp-rename-propt: no identifier at cursor"
}
set-register s %val{selection}
execute-keys z
prompt -init %reg{s} 'New name: ' %{ lsp-rename %val{text} }
}
}
define-command lsp-signature-help -docstring "Request signature help for the main cursor position" %{
lsp-did-change-and-then lsp-signature-help-request
}
define-command -hidden lsp-signature-help-request -docstring "Request signature help for the main cursor position" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/signatureHelp"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-diagnostics -docstring "Open buffer with project-wide diagnostics for current filetype" %{
lsp-did-change-and-then lsp-diagnostics-request
}
define-command -hidden lsp-diagnostics-request -docstring "Open buffer with project-wide diagnostics for current filetype" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/diagnostics"
[params]
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-document-symbol -docstring "Open buffer with document symbols" %{
lsp-did-change-and-then lsp-document-symbol-request
}
define-command -hidden lsp-document-symbol-request -docstring "Open buffer with document symbols" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/documentSymbol"
[params]
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command -hidden lsp-workspace-symbol-buffer -params 4 -docstring %{
buffile filetype timestamp query
Open buffer with a list of project-wide symbols matching the query
on behalf of the buffile at timestamp
} %{
lsp-did-change-and-then "lsp-workspace-symbol-buffer-request '%arg{1}' '%arg{2}' '%arg{3}' '%arg{4}'"
}
define-command -hidden lsp-workspace-symbol-buffer-request -params 4 -docstring %{
buffile filetype timestamp query
Open buffer with a list of project-wide symbols matching the query
on behalf of the buffile at timestamp
} %{ try %{
evaluate-commands %sh{
if [ -z "${4}" ];
then echo "fail"
else echo "nop"
fi
}
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "workspace/symbol"
[params]
query = "%s"
' "${kak_session}" "${kak_client}" "${1}" "${2}" "${3}" "${4}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}}
define-command lsp-capabilities -docstring "List available commands for current filetype" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "capabilities"
[params]
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command -hidden lsp-did-open %{
# see lsp-did-change
set-option buffer lsp_timestamp %val{timestamp}
evaluate-commands -save-regs '|' %{
set-register '|' %{
lsp_draft=$(cat; printf '.')
(
lsp_draft=$(printf '%s' "$lsp_draft" | sed 's/\\/\\\\/g ; s/"/\\"/g ; s/'"$(printf '\t')"'/\\t/g')
lsp_draft=${lsp_draft%.}
printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/didOpen"
[params]
draft = """
%s"""
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "${lsp_draft}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
execute-keys -draft '%<a-|><ret>'
}
}
define-command -hidden lsp-did-close %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/didClose"
[params]
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command -hidden lsp-did-save %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/didSave"
[params]
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command -hidden lsp-did-change-config %{
echo -debug "kak-lsp: config-change detected:" %opt{lsp_config}
nop %sh{ ((printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "workspace/didChangeConfiguration"
[params.settings]
lsp_config = """%s"""
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" \
"$(printf %s "${kak_opt_lsp_config}" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')"
eval "set -- $kak_quoted_opt_lsp_server_configuration"
while [ $# -gt 0 ]; do
key=${1%%=*}
value=${1#*=}
value="$(printf %s "$value"|sed -e 's/\\=/=/g')"
quotedkey='"'$(printf %s "$key"|sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')'"'
printf '%s = %s\n' "$quotedkey" "$value"
shift
done
) | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command -hidden lsp-exit-editor-session -docstring "Shutdown language servers associated with current editor session but keep kak-lsp session running" %{
remove-hooks global lsp
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "exit"
[params]
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-cancel-progress -params 1 -docstring "lsp-cancel-progress <token>: cancel a cancelable progress item." %{
remove-hooks global lsp
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "window/workDoneProgress/cancel"
[params]
token = "%s"
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "$1" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-apply-workspace-edit -params 1 -hidden %{
lsp-did-change-and-then %sh{
printf "lsp-apply-workspace-edit-request '%s'" "$(printf %s "$1" | sed "s/'/''/g")"
}
}
define-command lsp-apply-workspace-edit-request -params 1 -hidden %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "apply-workspace-edit"
[params]
edit = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "$1" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-apply-text-edits -params 1 -hidden %{
lsp-did-change-and-then "lsp-apply-text-edits-request '%arg{1}'"
}
define-command lsp-apply-text-edits-request -params 1 -hidden %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "apply-text-edits"
[params]
edit = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "$1" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-stop -docstring "Stop kak-lsp session" %{
remove-hooks global lsp
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "stop"
[params]
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command lsp-formatting -docstring "Format document" %{
lsp-did-change-and-then lsp-formatting-request
}
define-command -hidden lsp-formatting-request -docstring "Format document" %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/formatting"
[params]
tabSize = %d
insertSpaces = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "${kak_opt_tabstop}" "${kak_opt_lsp_insert_spaces}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null }
}
define-command lsp-range-formatting -docstring "Format selections" %{
lsp-did-change-and-then lsp-range-formatting-request
}
define-command -hidden lsp-range-formatting-request -docstring "Format selections" %{
nop %sh{
ranges_str="$(for range in ${kak_selections_char_desc}; do
start=${range%,*}
end=${range#*,}
startline=${start%.*}
startcolumn=${start#*.}
endline=${end%.*}
endcolumn=${end#*.}
printf '
[[ranges]]
[ranges.start]
line = %d
character = %d
[ranges.end]
line = %d
character = %d
' $((startline - 1)) $((startcolumn - 1)) $((endline - 1)) $((endcolumn - 1))
done)"
(printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/rangeFormatting"
[params]
tabSize = %d
insertSpaces = %s
%s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "${kak_opt_tabstop}" "${kak_opt_lsp_insert_spaces}" "${ranges_str}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null
}}
define-command lsp-formatting-sync -docstring "Format document, blocking Kakoune session until done" %{
lsp-did-change-and-then lsp-formatting-sync-request
}
define-command -hidden lsp-formatting-sync-request -docstring "Format document, blocking Kakoune session until done" %{
evaluate-commands -no-hooks %sh{
tmp=$(mktemp -q -d -t 'lsp-formatting.XXXXXX' 2>/dev/null || mktemp -q -d)
pipe=${tmp}/fifo
mkfifo ${pipe}
(printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
fifo = "%s"
method = "textDocument/formatting"
[params]
tabSize = %d
insertSpaces = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${pipe} "${kak_opt_tabstop}" "${kak_opt_lsp_insert_spaces}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null
cat ${pipe}
rm -rf ${tmp}
}}
define-command lsp-range-formatting-sync -docstring "Format selections, blocking Kakoune session until done" %{
lsp-did-change-and-then lsp-range-formatting-sync-request
}
define-command -hidden lsp-range-formatting-sync-request -docstring "Format selections, blocking Kakoune session until done" %{
evaluate-commands -no-hooks %sh{
range=${kak_selection_desc}
tmp=$(mktemp -q -d -t 'lsp-formatting.XXXXXX' 2>/dev/null || mktemp -q -d)
pipe=${tmp}/fifo
mkfifo ${pipe}
ranges_str="$(for range in ${kak_selections_char_desc}; do
start=${range%,*}
end=${range#*,}
startline=${start%.*}
startcolumn=${start#*.}
endline=${end%.*}
endcolumn=${end#*.}
printf '
[[ranges]]
[ranges.start]
line = %d
character = %d
[ranges.end]
line = %d
character = %d
' $((startline - 1)) $((startcolumn - 1)) $((endline - 1)) $((endcolumn - 1))
done)"
(printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
fifo = "%s"
method = "textDocument/rangeFormatting"
[params]
tabSize = %d
insertSpaces = %s
%s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${pipe} "${kak_opt_tabstop}" "${kak_opt_lsp_insert_spaces}" "${ranges_str}" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null
cat ${pipe} | tee /tmp/pipe
rm -rf ${tmp}
}}
define-command lsp-incoming-calls -docstring "Open buffer with calls to the function at the main cursor position" %{
lsp-call-hierarchy-request true
}
define-command lsp-outgoing-calls -docstring "Open buffer with calls by the function at the main cursor position" %{
lsp-call-hierarchy-request false
}
define-command -hidden lsp-call-hierarchy-request -params 1 %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "textDocument/prepareCallHierarchy"
[params]
position.line = %d
position.column = %d
incomingOrOutgoing = %s
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "${kak_cursor_line}" "${kak_cursor_column}" "$1" | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
# CCLS Extension
define-command ccls-navigate -docstring "Navigate C/C++/ObjectiveC file" -params 1 %{
lsp-did-change-and-then "ccls-navigate-request '%arg{1}'"
}
define-command -hidden ccls-navigate-request -docstring "Navigate C/C++/ObjectiveC file" -params 1 %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "$ccls/navigate"
[params]
direction = "%s"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "$1" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command ccls-vars -docstring "ccls-vars: Find instances of symbol at point." %{
lsp-did-change-and-then ccls-vars-request
}
define-command -hidden ccls-vars-request -docstring "ccls-vars: Find instances of symbol at point." %{
nop %sh{ (printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "$ccls/vars"
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command ccls-inheritance -params 1..2 -docstring "ccls-inheritance <derived|base> [levels]: Find base- or derived classes of symbol at point." %{
lsp-did-change-and-then "ccls-inheritance-request '%arg{1}' '%arg{2}'"
}
define-command -hidden ccls-inheritance-request -params 1..2 -docstring "ccls-inheritance <derived|base> [levels]: Find base- or derived classes of symbol at point." %{
nop %sh{
derived="false"
if [ "$1" = "derived" ]; then
derived="true"
fi
levels="${2:-null}"
(printf '
session = "%s"
client = "%s"
buffile = "%s"
filetype = "%s"
version = %d
method = "$ccls/inheritance"
[params]
derived = %s
levels = %d
[params.position]
line = %d
column = %d
' "${kak_session}" "${kak_client}" "${kak_buffile}" "${kak_opt_filetype}" "${kak_timestamp}" "$derived" "$levels" ${kak_cursor_line} ${kak_cursor_column} | eval "${kak_opt_lsp_cmd} --request") > /dev/null 2>&1 < /dev/null & }
}
define-command ccls-call -params 1 -docstring "ccls-call <caller|callee>: Find callers or callees of symbol at point." %{
lsp-did-change-and-then "ccls-call-request '%arg{1}'"
}
define-command -hidden ccls-call-request -params 1 -docstring "ccls-call <caller|callee>: Find callers or callees of symbol at point." %{
nop %sh{
callee="false"
if [ "$1" = "callee" ]; then
callee="true"
fi
(printf '
session = "%s"
client = "%s"