-
-
Notifications
You must be signed in to change notification settings - Fork 860
/
lsp-rust.el
1781 lines (1529 loc) · 71.5 KB
/
lsp-rust.el
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
;;; lsp-rust.el --- Rust Client settings -*- lexical-binding: t; -*-
;; Copyright (C) 2019 Ivan Yonchovski
;; Author: Ivan Yonchovski <yyoncho@gmail.com>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; lsp-rust client
;;; Code:
(require 'lsp-mode)
(require 'ht)
(require 'dash)
(require 'lsp-semantic-tokens)
(require 's)
(defgroup lsp-rust nil
"LSP support for Rust, using Rust Language Server or rust-analyzer."
:group 'lsp-mode
:link '(url-link "https://github.com/rust-lang/rls")
:package-version '(lsp-mode . "6.1"))
(defgroup lsp-rust-rls nil
"LSP support for Rust, using Rust Language Server."
:group 'lsp-mode
:link '(url-link "https://github.com/rust-lang/rls")
:package-version '(lsp-mode . "8.0.0"))
(defgroup lsp-rust-analyzer nil
"LSP support for Rust, using rust-analyzer."
:group 'lsp-mode
:link '(url-link "https://github.com/rust-lang/rust-analyzer")
:package-version '(lsp-mode . "8.0.0"))
(defgroup lsp-rust-analyzer-semantic-tokens nil
"LSP semantic tokens support for rust-analyzer."
:group 'lsp-rust-analyzer
:link '(url-link "https://github.com/rust-lang/rust-analyzer")
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-server 'rust-analyzer
"Choose LSP server."
:type '(choice (const :tag "rls" rls)
(const :tag "rust-analyzer" rust-analyzer))
:group 'lsp-rust
:package-version '(lsp-mode . "6.2"))
;; RLS
(defcustom lsp-rust-rls-server-command '("rls")
"Command to start RLS."
:type '(repeat string)
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-library-directories
'("~/.cargo/registry/src" "~/.rustup/toolchains")
"List of directories which will be considered to be libraries."
:risky t
:type '(repeat string)
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-sysroot nil
"If non-nil, use the given path as the sysroot for all rustc invocations
instead of trying to detect the sysroot automatically."
:type '(choice
(const :tag "None" nil)
(string :tag "Sysroot"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-target nil
"If non-nil, use the given target triple for all rustc invocations."
:type '(choice
(const :tag "None" nil)
(string :tag "Target"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-rustflags nil
"Flags added to RUSTFLAGS."
:type '(choice
(const :tag "None" nil)
(string :tag "Flags"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-clear-env-rust-log t
"Clear the RUST_LOG environment variable before running rustc or cargo."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-build-lib nil
"If non-nil, checks the project as if you passed the `--lib' argument to
cargo.
Mutually exclusive with, and preferred over, `lsp-rust-build-bin'. (Unstable)"
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-build-bin nil
"If non-nil, checks the project as if you passed `-- bin <build_bin>'
argument to cargo.
Mutually exclusive with `lsp-rust-build-lib'. (Unstable)"
:type '(choice
(const :tag "None" nil)
(string :tag "Binary"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-cfg-test nil
"If non-nil, checks the project as if you were running `cargo test' rather
than cargo build.
I.e., compiles (but does not run) test code."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-unstable-features nil
"Enable unstable features."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-wait-to-build nil
"Time in milliseconds between receiving a change notification
and starting build. If not specified, automatically inferred by
the latest build duration."
:type '(choice
(const :tag "Auto" nil)
(number :tag "Time"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-show-warnings t
"Show warnings."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-crate-blocklist [
"cocoa"
"gleam"
"glium"
"idna"
"libc"
"openssl"
"rustc_serialize"
"serde"
"serde_json"
"typenum"
"unicode_normalization"
"unicode_segmentation"
"winapi"
]
"A list of Cargo crates to blocklist."
:type 'lsp-string-vector
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-build-on-save nil
"Only index the project when a file is saved and not on change."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-features []
"List of features to activate.
Set this to `\"all\"` to pass `--all-features` to cargo."
:type 'lsp-string-vector
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-all-features nil
"Enable all Cargo features."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-no-default-features nil
"Do not enable default Cargo features."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-racer-completion t
"Enables code completion using racer."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-clippy-preference "opt-in"
"Controls eagerness of clippy diagnostics when available.
Valid values are (case-insensitive):
- \"off\": Disable clippy lints.
- \"opt-in\": Clippy lints are shown when crates specify `#![warn(clippy)]'.
- \"on\": Clippy lints enabled for all crates in workspace.
You need to install clippy via rustup if you haven't already."
:type '(choice
(const "on")
(const "opt-in")
(const "off"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-jobs nil
"Number of Cargo jobs to be run in parallel."
:type '(choice
(const :tag "Auto" nil)
(number :tag "Jobs"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-all-targets t
"Checks the project as if you were running cargo check --all-targets.
I.e., check all targets and integration tests too."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-target-dir nil
"When specified, it places the generated analysis files at the
specified target directory. By default it is placed target/rls
directory."
:type '(choice
(const :tag "Default" nil)
(string :tag "Directory"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-rustfmt-path nil
"When specified, RLS will use the Rustfmt pointed at the path
instead of the bundled one"
:type '(choice
(const :tag "Bundled" nil)
(string :tag "Path"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-build-command nil
"EXPERIMENTAL (requires `rust.unstable_features')
If set, executes a given program responsible for rebuilding save-analysis to be
loaded by the RLS. The program given should output a list of resulting .json
files on stdout.
Implies `rust.build_on_save': true."
:type '(choice
(const :tag "None" nil)
(string :tag "Command"))
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-full-docs nil
"Instructs cargo to enable full documentation extraction during
save-analysis while building the crate."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(defcustom lsp-rust-show-hover-context t
"Show additional context in hover tooltips when available. This
is often the type local variable declaration."
:type 'boolean
:group 'lsp-rust-rls
:package-version '(lsp-mode . "6.1"))
(lsp-register-custom-settings
'(("rust.show_hover_context" lsp-rust-show-hover-context t)
("rust.full_docs" lsp-rust-full-docs t)
("rust.build_command" lsp-rust-build-command)
("rust.rustfmt_path" lsp-rust-rustfmt-path)
("rust.target_dir" lsp-rust-target-dir)
("rust.all_targets" lsp-rust-all-targets t)
("rust.jobs" lsp-rust-jobs)
("rust.clippy_preference" lsp-rust-clippy-preference)
("rust.racer_completion" lsp-rust-racer-completion t)
("rust.no_default_features" lsp-rust-no-default-features t)
("rust.all_features" lsp-rust-all-features t)
("rust.features" lsp-rust-features)
("rust.build_on_save" lsp-rust-build-on-save t)
("rust.crate_blocklist" lsp-rust-crate-blocklist)
("rust.show_warnings" lsp-rust-show-warnings t)
("rust.wait_to_build" lsp-rust-wait-to-build)
("rust.unstable_features" lsp-rust-unstable-features t)
("rust.cfg_test" lsp-rust-cfg-test t)
("rust.build_bin" lsp-rust-build-bin)
("rust.build_lib" lsp-rust-build-lib t)
("rust.clear_env_rust_log" lsp-rust-clear-env-rust-log t)
("rust.rustflags" lsp-rust-rustflags)
("rust.target" lsp-rust-target)
("rust.sysroot" lsp-rust-sysroot)))
(defun lsp-clients--rust-window-progress (workspace params)
"Progress report handling.
PARAMS progress report notification data."
(-let [(&v1:ProgressParams :done? :message? :title) params]
(if (or done? (s-blank-str? message?))
(lsp-workspace-status nil workspace)
(lsp-workspace-status (format "%s - %s" title (or message? "")) workspace))))
(lsp-defun lsp-rust--rls-run ((&Command :arguments? params))
(-let* (((&rls:Cmd :env :binary :args :cwd) (lsp-seq-first params))
(default-directory (or cwd (lsp-workspace-root) default-directory) ))
(compile
(format "%s %s %s"
(s-join " " (ht-amap (format "%s=%s" key value) env))
binary
(s-join " " args)))))
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection (lambda () lsp-rust-rls-server-command))
:activation-fn (lsp-activate-on "rust")
:priority (if (eq lsp-rust-server 'rls) 1 -1)
:initialization-options '((omitInitBuild . t)
(cmdRun . t))
:notification-handlers (ht ("window/progress" 'lsp-clients--rust-window-progress))
:action-handlers (ht ("rls.run" 'lsp-rust--rls-run))
:library-folders-fn (lambda (_workspace) lsp-rust-library-directories)
:initialized-fn (lambda (workspace)
(with-lsp-workspace workspace
(lsp--set-configuration
(lsp-configuration-section "rust"))))
:server-id 'rls))
;; rust-analyzer
(defcustom lsp-rust-analyzer-server-command '("rust-analyzer")
"Command to start rust-analyzer."
:type '(repeat string)
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2"))
(defcustom lsp-rust-analyzer-library-directories
'("~/.cargo/registry/src" "~/.rustup/toolchains")
"List of directories which will be considered to be libraries."
:risky t
:type '(repeat string)
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-server-format-inlay-hints t
"Whether to ask rust-analyzer to format inlay hints itself. If
active, the various inlay format settings are not used."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-hide-closure-initialization nil
"Whether to hide inlay type hints for `let` statements that initialize
to a closure. Only applies to closures with blocks, same as
`#rust-analyzer.inlayHints.closureReturnTypeHints.enable#`."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-highlight-breakpoints t
"Enables highlighting of related references while the cursor is on
`break`, `loop`, `while`, or `for` keywords."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-highlight-closure-captures t
"Enables highlighting of all captures of a closure while the
cursor is on the `|` or move keyword of a closure."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-highlight-exit-points t
"Enables highlighting of all exit points while the cursor is on
any `return`, `?`, `fn`, or return type arrow (`->`)."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-highlight-references t
"Enables highlighting of related references while the cursor is on
any identifier."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-highlight-yield-points t
"Enables highlighting of all break points for a loop or block
context while the cursor is on any `async` or `await` keywords."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-closure-return-type-hints "never"
"Whether to show inlay type hints for return types of closures."
:type '(choice
(const "never")
(const "always")
(const "with_block"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-discriminants-hints "never"
"Whether to show enum variant discriminant hints."
:type '(choice
(const "never")
(const "always")
(const "fieldless"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-expression-adjustment-hints "never"
"Whether to show inlay hints for type adjustments.."
:type '(choice
(const "never")
(const "always")
(const "reborrow"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-expression-adjustment-hints-mode "prefix"
"Whether to show inlay hints as postfix ops (`.*` instead of `*`, etc)."
:type '(choice
(const "prefix")
(const "postfix")
(const "prefer_prefix")
(const "prefer_postfix"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-expression-adjustment-hide-unsafe nil
"Whether to hide inlay hints for type adjustments outside of
`unsafe` blocks."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-implicit-drops nil
"Whether to show implicit drop hints."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-closure-capture-hints nil
"Whether to show inlay hints for closure captures."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-closure-style "impl_fn"
"Closure notation in type and chaining inlay hints."
:type 'string
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-hide-named-constructor nil
"Whether to hide inlay type hints for constructors."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-max-inlay-hint-length nil
"Max inlay hint length."
:type 'integer
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-display-chaining-hints nil
"Whether to show inlay type hints for method chains. These
hints will be formatted with the type hint formatting options, if
the mode is not configured to ask the server to format them."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-display-lifetime-elision-hints-enable "never"
"Whether to show elided lifetime inlay hints."
:type '(choice
(const "never")
(const "always")
(const "skip_trivial"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-display-lifetime-elision-hints-use-parameter-names nil
"When showing elided lifetime inlay hints, whether to use
parameter names or numeric placeholder names for the lifetimes."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-display-closure-return-type-hints nil
"Whether to show closure return type inlay hints for closures
with block bodies."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-display-parameter-hints nil
"Whether to show function parameter name inlay hints at the call site."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-display-reborrow-hints "never"
"Whether to show inlay type hints for compiler inserted reborrows."
:type '(choice
(const "always")
(const "never")
(const "mutable"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-binding-mode-hints nil
"Whether to show inlay type hints for binding modes."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-closing-brace-hints t
"Whether to show inlay hints after a closing `}` to indicate what item it
belongs to."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-closing-brace-hints-min-lines 25
"Minimum number of lines required before the `}` until the hint is shown
\(set to 0 or 1 to always show them)."
:type 'integer
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-lru-capacity nil
"Number of syntax trees rust-analyzer keeps in memory."
:type 'integer
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-cargo-target nil
"Compilation target (target triple)."
:type '(choice
(string :tag "Target")
(const :tag "None" nil))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-cargo-watch-enable t
"Enable Cargo watch."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-cargo-watch-command "check"
"Cargo watch command."
:type 'string
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-cargo-watch-args []
"Extra arguments for `cargo check`."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-cargo-override-command []
"Advanced option, fully override the command rust-analyzer uses for checking.
The command should include `--message=format=json` or similar option."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-check-all-targets t
"Enables --all-targets for `cargo check`."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.2"))
(defcustom lsp-rust-analyzer-checkonsave-features []
"List of features to activate.
Set this to `\"all\"` to pass `--all-features` to cargo."
:type 'lsp-string-vector
:group 'lsp-rust-rust-analyzer
:package-version '(lsp-mode . "8.0.2"))
(defcustom lsp-rust-analyzer-cargo-unset-test []
"force rust-analyzer to unset `#[cfg(test)]` for the specified crates."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-use-client-watching t
"Use client watching"
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-exclude-globs []
"Exclude globs"
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-exclude-dirs []
"These directories will be ignored by rust-analyzer."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-macro-expansion-method 'lsp-rust-analyzer-macro-expansion-default
"Use a different function if you want formatted macro expansion results and
syntax highlighting."
:type 'function
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.2.2"))
(defcustom lsp-rust-analyzer-diagnostics-enable t
"Whether to show native rust-analyzer diagnostics."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-diagnostics-enable-experimental nil
"Whether to show native rust-analyzer diagnostics that are still experimental
\(might have more false positives than usual)."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-diagnostics-disabled []
"List of native rust-analyzer diagnostics to disable."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-diagnostics-warnings-as-hint []
"List of warnings that should be displayed with hint severity."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-diagnostics-warnings-as-info []
"List of warnings that should be displayed with info severity."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(define-obsolete-variable-alias
'lsp-rust-analyzer-cargo-load-out-dirs-from-check
'lsp-rust-analyzer-cargo-run-build-scripts
"8.0.0")
(defcustom lsp-rust-analyzer-cargo-run-build-scripts t
"Whether to run build scripts (`build.rs`) for more precise code analysis."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-rustfmt-extra-args []
"Additional arguments to rustfmt."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-rustfmt-override-command []
"Advanced option, fully override the command rust-analyzer uses
for formatting."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-rustfmt-rangeformatting-enable nil
"Enables the use of rustfmt's unstable range formatting command for the
`textDocument/rangeFormatting` request. The rustfmt option is unstable and only
available on a nightly build."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-completion-add-call-parenthesis t
"Whether to add parenthesis when completing functions."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-completion-add-call-argument-snippets t
"Whether to add argument snippets when completing functions."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-completion-postfix-enable t
"Whether to show postfix snippets like `dbg`, `if`, `not`, etc."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-call-info-full t
"Whether to show function name and docs in parameter hints."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-proc-macro-enable t
"Enable Proc macro support.
Implies `lsp-rust-analyzer-cargo-run-build-scripts'"
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "6.3.2"))
(defcustom lsp-rust-analyzer-import-prefix "plain"
"The path structure for newly inserted paths to use.
Valid values are:
- \"plain\": Insert import paths relative to the current module, using up to
one `super' prefix if the parent module contains the requested item.
- \"by_self\": Prefix all import paths with `self' if they don't begin with
`self', `super', `crate' or a crate name.
- \"by_crate\": Force import paths to be absolute by always starting
them with `crate' or the crate name they refer to."
:type '(choice
(const "plain")
(const "by_self")
(const "by_crate"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-import-granularity "crate"
"How imports should be grouped into use statements."
:type '(choice
(const "crate" :doc "Merge imports from the same crate into a single use statement. This kind of nesting is only supported in Rust versions later than 1.24.")
(const "module" :doc "Merge imports from the same module into a single use statement.")
(const "item" :doc "Don’t merge imports at all, creating one import per item.")
(const "preserve" :doc "Do not change the granularity of any imports. For auto-import this has the same effect as `\"item\"'"))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-cargo-auto-reload t
"Automatically refresh project info via `cargo metadata' on `Cargo.toml' changes."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-use-rustc-wrapper-for-build-scripts t
"Use `RUSTC_WRAPPER=rust-analyzer' when running build scripts to avoid
compiling unnecessary things."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-completion-auto-import-enable t
"Toggles the additional completions that automatically add imports when
completed. `lsp-completion-enable-additional-text-edit' must be non-nil
for this feature to be fully enabled."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-completion-auto-self-enable t
"Toggles the additional completions that automatically show method calls
and field accesses with self prefixed to them when inside a method."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-import-enforce-granularity nil
"Whether to enforce the import granularity setting for all files.
If set to nil rust-analyzer will try to keep import styles consistent per file."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-imports-merge-glob t
"Whether to allow import insertion to merge new imports into single path
glob imports like `use std::fmt::*;`."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-import-group t
"Group inserted imports by the following order:
https://rust-analyzer.github.io/manual.html#auto-import.
Groups are separated by newlines."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-highlighting-strings t
"Use semantic tokens for strings."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-rustc-source nil
"Path to the Cargo.toml of the rust compiler workspace."
:type '(choice
(file :tag "Path")
(const :tag "None" nil))
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-linked-projects []
"Disable project auto-discovery in favor of explicitly specified set of
projects. Elements must be paths pointing to `Cargo.toml`, `rust-project.json`,
or JSON objects in `rust-project.json` format."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-experimental-proc-attr-macros t
"Whether to enable experimental support for expanding proc macro attributes."
:type 'boolean
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-cargo-extra-args []
"Extra arguments that are passed to every cargo invocation."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defcustom lsp-rust-analyzer-cargo-extra-env []
"Extra environment variables that will be set when running cargo, rustc or
other commands within the workspace. Useful for setting RUSTFLAGS."
:type 'lsp-string-vector
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "9.0.0"))
(defconst lsp-rust-notification-handlers
'(("rust-analyzer/publishDecorations" . (lambda (_w _p)))))
(defconst lsp-rust-action-handlers
'())
(define-derived-mode lsp-rust-analyzer-syntax-tree-mode special-mode "Rust-Analyzer-Syntax-Tree"
"Mode for the rust-analyzer syntax tree buffer.")
(defun lsp-rust-analyzer-syntax-tree ()
"Display syntax tree for current buffer."
(interactive)
(-let* ((root (lsp-workspace-root default-directory))
(params (lsp-make-rust-analyzer-syntax-tree-params
:text-document (lsp--text-document-identifier)
:range? (if (use-region-p)
(lsp--region-to-range (region-beginning) (region-end))
(lsp--region-to-range (point-min) (point-max)))))
(results (lsp-send-request (lsp-make-request
"rust-analyzer/syntaxTree"
params))))
(let ((buf (get-buffer-create (format "*rust-analyzer syntax tree %s*" root)))
(inhibit-read-only t))
(with-current-buffer buf
(lsp-rust-analyzer-syntax-tree-mode)
(erase-buffer)
(insert results)
(goto-char (point-min)))
(pop-to-buffer buf))))
(define-derived-mode lsp-rust-analyzer-status-mode special-mode "Rust-Analyzer-Status"
"Mode for the rust-analyzer status buffer.")
(defun lsp-rust-analyzer-status ()
"Displays status information for rust-analyzer."
(interactive)
(-let* ((root (lsp-workspace-root default-directory))
(params (lsp-make-rust-analyzer-analyzer-status-params
:text-document (lsp--text-document-identifier)))
(results (lsp-send-request (lsp-make-request
"rust-analyzer/analyzerStatus"
params))))
(let ((buf (get-buffer-create (format "*rust-analyzer status %s*" root)))
(inhibit-read-only t))
(with-current-buffer buf
(lsp-rust-analyzer-status-mode)
(erase-buffer)
(insert results)
(pop-to-buffer buf)))))
(defun lsp-rust-analyzer-view-item-tree ()
"Show item tree of rust file."
(interactive)
(-let* ((params (lsp-make-rust-analyzer-view-item-tree
:text-document (lsp--text-document-identifier)))
(results (lsp-send-request (lsp-make-request
"rust-analyzer/viewItemTree"
params))))
(let ((buf (get-buffer-create "*rust-analyzer item tree*"))
(inhibit-read-only t))
(with-current-buffer buf
(special-mode)
(erase-buffer)
(insert (lsp--render-string results "rust"))
(pop-to-buffer buf)))))
(defun lsp-rust-analyzer-view-hir ()
"View Hir of function at point."
(interactive)
(-let* ((params (lsp-make-rust-analyzer-expand-macro-params
:text-document (lsp--text-document-identifier)
:position (lsp--cur-position)))
(results (lsp-send-request (lsp-make-request
"rust-analyzer/viewHir"
params))))
(let ((buf (get-buffer-create "*rust-analyzer hir*"))
(inhibit-read-only t))
(with-current-buffer buf
(special-mode)
(erase-buffer)
(insert results)
(pop-to-buffer buf)))))
(defun lsp-rust-analyzer-join-lines ()
"Join selected lines into one, smartly fixing up whitespace and trailing commas."
(interactive)
(let* ((params (lsp-make-rust-analyzer-join-lines-params
:text-document (lsp--text-document-identifier)
:ranges (vector (if (use-region-p)
(lsp--region-to-range (region-beginning) (region-end))
(lsp--region-to-range (point) (point))))))
(result (lsp-send-request (lsp-make-request "experimental/joinLines" params))))
(lsp--apply-text-edits result 'code-action)))
(defun lsp-rust-analyzer-reload-workspace ()
"Reload workspace, picking up changes from Cargo.toml"
(interactive)
(lsp--cur-workspace-check)
(lsp-send-request (lsp-make-request "rust-analyzer/reloadWorkspace")))
(defcustom lsp-rust-analyzer-download-url
(let* ((x86 (string-prefix-p "x86_64" system-configuration))
(arch (if x86 "x86_64" "aarch64")))
(format "https://github.com/rust-lang/rust-analyzer/releases/latest/download/%s"
(pcase system-type
('gnu/linux (format "rust-analyzer-%s-unknown-linux-gnu.gz" arch))
('darwin (format "rust-analyzer-%s-apple-darwin.gz" arch))
('windows-nt (format "rust-analyzer-%s-pc-windows-msvc.zip" arch)))))
"Automatic download url for Rust Analyzer"
:type 'string
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(defcustom lsp-rust-analyzer-store-path (f-join lsp-server-install-dir "rust"
(pcase system-type
('windows-nt "rust-analyzer.exe")
(_ "rust-analyzer")))
"The path to the file in which `rust-analyzer' will be stored."
:type 'file
:group 'lsp-rust-analyzer
:package-version '(lsp-mode . "8.0.0"))
(lsp-dependency
'rust-analyzer
`(:download :url lsp-rust-analyzer-download-url
:decompress ,(pcase system-type ('windows-nt :zip) (_ :gzip))
:store-path lsp-rust-analyzer-store-path
:set-executable? t)
`(:system ,(file-name-nondirectory lsp-rust-analyzer-store-path)))
(lsp-defun lsp-rust--analyzer-run-single ((&Command :arguments?))
(lsp-rust-analyzer-run (lsp-seq-first arguments?)))
(lsp-defun lsp-rust--analyzer-show-references
((&Command :title :arguments? [_uri _filepos references]))
(lsp-show-xrefs (lsp--locations-to-xref-items references) nil
(s-contains-p "reference" title)))
(declare-function dap-debug "ext:dap-mode" (template) t)
(lsp-defun lsp-rust--analyzer-debug-lens ((&Command :arguments? [args]))
(lsp-rust-analyzer-debug args))
;; Semantic tokens
;; Modifier faces
(defface lsp-rust-analyzer-documentation-modifier-face
'((t nil))
"The face modification to use for documentation items."
:group 'lsp-rust-analyzer-semantic-tokens)
(defface lsp-rust-analyzer-declaration-modifier-face
'((t nil))