-
-
Notifications
You must be signed in to change notification settings - Fork 645
/
cider.el
2228 lines (1968 loc) · 99.8 KB
/
cider.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
;;; cider.el --- Clojure Interactive Development Environment that Rocks -*- lexical-binding: t -*-
;; Copyright © 2012-2024 Tim King, Phil Hagelberg, Bozhidar Batsov
;; Copyright © 2013-2024 Bozhidar Batsov, Artur Malabarba and CIDER contributors
;;
;; Author: Tim King <kingtim@gmail.com>
;; Phil Hagelberg <technomancy@gmail.com>
;; Bozhidar Batsov <bozhidar@batsov.dev>
;; Artur Malabarba <bruce.connor.am@gmail.com>
;; Hugo Duncan <hugo@hugoduncan.org>
;; Steve Purcell <steve@sanityinc.com>
;; Maintainer: Bozhidar Batsov <bozhidar@batsov.dev>
;; URL: https://www.github.com/clojure-emacs/cider
;; Version: 1.16.1
;; Package-Requires: ((emacs "26") (clojure-mode "5.19") (parseedn "1.2.1") (queue "0.2") (spinner "1.7") (seq "2.22") (sesman "0.3.2") (transient "0.4.1"))
;; Keywords: languages, clojure, cider
;; 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 <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Provides a Clojure interactive development environment for Emacs, built on
;; top of nREPL. See https://docs.cider.mx for more details.
;;; Installation:
;; CIDER is available as a package in melpa.org and stable.melpa.org. First, make sure you've
;; enabled one of the repositories in your Emacs config:
;; (add-to-list 'package-archives
;; '("melpa" . "https://melpa.org/packages/"))
;;
;; or
;;
;; (add-to-list 'package-archives
;; '("melpa-stable" . "https://stable.melpa.org/packages/") t)
;; Afterwards, installing CIDER is as easy as:
;; M-x package-install cider
;;; Usage:
;; You can start CIDER with one of the following commands:
;; M-x cider-jack-in-clj
;; M-x cider-jack-in-cljs
;;
;; M-x cider-connect-sibling-clj
;; M-x cider-connect-sibling-cljs
;;
;; M-x cider-connect-clj
;; M-x cider-connect-cljs
;;; Code:
(defgroup cider nil
"Clojure Interactive Development Environment that Rocks."
:prefix "cider-"
:group 'applications
:link '(url-link :tag "GitHub" "https://github.com/clojure-emacs/cider")
:link '(url-link :tag "Homepage" "https://cider.mx")
:link '(url-link :tag "Documentation" "https://docs.cider.mx")
:link '(emacs-commentary-link :tag "Commentary" "cider"))
(require 'cider-client)
(require 'cider-eldoc)
(require 'cider-repl)
(require 'cider-repl-history)
(require 'cider-connection)
(require 'cider-mode)
(require 'cider-common)
(require 'cider-debug)
(require 'cider-util)
(require 'cl-lib)
(require 'tramp-sh)
(require 'subr-x)
(require 'seq)
(require 'sesman)
(require 'package)
(defconst cider-version "1.16.1"
"The current version of CIDER.")
(defconst cider-codename "Kherson"
"Codename used to denote stable releases.")
(defcustom cider-lein-command
"lein"
"The command used to execute Leiningen."
:type 'string)
(defcustom cider-lein-global-options
nil
"Command global options used to execute Leiningen (e.g.: -o for offline)."
:type 'string
:safe #'stringp)
(defcustom cider-lein-parameters
"repl :headless :host localhost"
"Params passed to Leiningen to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp)
(defcustom cider-boot-command
"boot"
"The command used to execute Boot."
:type 'string
:package-version '(cider . "0.9.0"))
(defcustom cider-boot-global-options
nil
"Command global options used to execute Boot (e.g.: -c for checkouts)."
:type 'string
:safe #'stringp
:package-version '(cider . "0.14.0"))
(defcustom cider-boot-parameters
"repl -s -b localhost wait"
"Params passed to boot to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp
:package-version '(cider . "0.9.0"))
(defcustom cider-clojure-cli-command
(if (and (eq system-type 'windows-nt)
(null (executable-find "clojure")))
"powershell"
"clojure")
"The command used to execute clojure with tools.deps.
Don't use clj here, as it doesn't work when spawned from Emacs due to it
using rlwrap. If on Windows and no \"clojure\" executable is found we
default to \"powershell\"."
:type 'string
:safe #'stringp
:package-version '(cider . "0.17.0"))
(defcustom cider-clojure-cli-global-options
nil
"Command line options used to execute clojure with tools.deps."
:type 'string
:safe #'stringp
:package-version '(cider . "0.17.0"))
(defcustom cider-clojure-cli-parameters
nil
"Params passed to clojure cli to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp
:package-version '(cider . "1.8.0"))
(defcustom cider-clojure-cli-aliases
nil
"A list of aliases to include when using the clojure cli.
Alias names should be of the form \":foo:bar\".
Leading \"-A\" \"-M\" \"-T\" or \"-X\" are stripped from aliases
then concatenated into the \"-M[your-aliases]:cider/nrepl\" form."
:type 'string
:safe #'stringp
:package-version '(cider . "1.1"))
(defcustom cider-clojure-cli-global-aliases
nil
"Global aliases to include when jacking in with the clojure CLI.
This value should be a string of the form \":foo:bar\", and
will be prepended to the value of `cider-clojure-cli-aliases'.
Alias names should be of the form \":foo:bar\".
Leading \"-A\" \"-M\" \"-T\" or \"-X\" are stripped from aliases
then concatenated into the \"-M[your-aliases]:cider/nrepl\" form."
:type 'string
:safe #'stringp
:package-version '(cider . "1.14"))
(defcustom cider-shadow-cljs-command
"npx shadow-cljs"
"The command used to execute shadow-cljs.
By default we favor the project-specific shadow-cljs over the system-wide."
:type 'string
:safe #'stringp
:package-version '(cider . "0.17.0"))
(defcustom cider-shadow-cljs-global-options
""
"Command line options used to execute shadow-cljs (e.g.: -v for verbose mode)."
:type 'string
:safe #'stringp
:package-version '(cider . "0.17.0"))
(defcustom cider-shadow-cljs-parameters
"server"
"Params passed to shadow-cljs to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp
:package-version '(cider . "0.17.0"))
(defcustom cider-gradle-command
"./gradlew"
"The command used to execute Gradle."
:type 'string
:safe #'stringp
:package-version '(cider . "0.10.0"))
(defcustom cider-gradle-global-options
""
"Command line options used to execute Gradle (e.g.: -m for dry run)."
:type 'string
:safe #'stringp
:package-version '(cider . "0.14.0"))
(defcustom cider-gradle-parameters
"clojureRepl"
"Params passed to gradle to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp
:package-version '(cider . "0.10.0"))
(defcustom cider-babashka-command
"bb"
"The command used to execute Babashka."
:type 'string
:safe #'stringp
:package-version '(cider . "1.2.0"))
(defcustom cider-babashka-global-options
nil
"Command line options used to execute Babashka."
:type 'string
:safe #'stringp
:package-version '(cider . "1.2.0"))
(defcustom cider-babashka-parameters
"nrepl-server localhost:0"
"Params passed to babashka to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp
:package-version '(cider . "1.2.0"))
(defcustom cider-nbb-command
"nbb"
"The command used to execute nbb."
:type 'string
:safe #'stringp
:package-version '(cider . "1.6.0"))
(defcustom cider-nbb-global-options
nil
"Command line options used to execute nbb."
:type 'string
:safe #'stringp
:package-version '(cider . "1.6.0"))
(defcustom cider-nbb-parameters
"nrepl-server"
"Params passed to nbb to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp
:package-version '(cider . "1.6.0"))
(defcustom cider-basilisp-command
"basilisp"
"The command used to execute Basilisp.
If Basilisp is installed in a virtual environment, update this to the
full path of the Basilisp executable within that virtual environment."
:type 'string
:safe #'stringp
:package-version '(cider . "1.14.0"))
(defcustom cider-basilisp-parameters
"nrepl-server"
"Params passed to Basilisp to start an nREPL server via `cider-jack-in'."
:type 'string
:safe #'stringp
:package-version '(cider . "1.14.0"))
(make-obsolete-variable 'cider-lein-global-options 'cider-lein-parameters "1.8.0")
(make-obsolete-variable 'cider-boot-global-options 'cider-boot-parameters "1.8.0")
(make-obsolete-variable 'cider-clojure-cli-global-options 'cider-clojure-cli-parameters "1.8.0")
(make-obsolete-variable 'cider-shadow-cljs-global-options 'cider-shadow-cljs-parameters "1.8.0")
(make-obsolete-variable 'cider-gradle-global-options 'cider-gradle-parameters "1.8.0")
(make-obsolete-variable 'cider-babashka-global-options 'cider-babashka-parameters "1.8.0")
(make-obsolete-variable 'cider-nbb-global-options 'cider-nbb-parameters "1.8.0")
(defcustom cider-jack-in-default
(if (executable-find "clojure") 'clojure-cli 'lein)
"The default tool to use when doing `cider-jack-in' outside a project.
This value will only be consulted when no identifying file types, i.e.
project.clj for leiningen or build.boot for boot, could be found.
As the Clojure CLI is bundled with Clojure itself, it's the default.
In the absence of the Clojure CLI (e.g. on Windows), we fallback
to Leiningen."
:type '(choice (const lein)
(const boot)
(const clojure-cli)
(const shadow-cljs)
(const gradle)
(const babashka)
(const nbb)
(const basilisp))
:safe #'symbolp
:package-version '(cider . "0.9.0"))
(defcustom cider-preferred-build-tool
nil
"Allow choosing a build system when there are many.
When there are project markers from multiple build systems (e.g. lein and
boot) the user is prompted to select one of them. When non-nil, this
variable will suppress this behavior and will select whatever build system
is indicated by the variable if present. Note, this is only when CIDER
cannot decide which of many build systems to use and will never override a
command when there is no ambiguity."
:type '(choice (const lein)
(const boot)
(const clojure-cli)
(const shadow-cljs)
(const gradle)
(const babashka)
(const nbb)
(const basilisp)
(const :tag "Always ask" nil))
:safe #'symbolp
:package-version '(cider . "0.13.0"))
(defcustom cider-allow-jack-in-without-project 'warn
"Controls what happens when doing `cider-jack-in' outside a project.
When set to 'warn you'd prompted to confirm the command.
When set to t `cider-jack-in' will quietly continue.
When set to nil `cider-jack-in' will fail."
:type '(choice (const :tag "always" t)
(const warn)
(const :tag "never" nil))
:safe #'symbolp
:package-version '(cider . "0.15.0"))
(defcustom cider-known-endpoints nil
"A list of connection endpoints where each endpoint is a list.
For example: \\='((\"label\" \"host\" \"port\")).
The label is optional so that \\='(\"host\" \"port\") will suffice.
This variable is used by `cider-connect'."
:type '(repeat (list (string :tag "label")
(string :tag "host")
(string :tag "port"))))
(defcustom cider-connected-hook nil
"List of functions to call when connected to Clojure nREPL server."
:type 'hook
:package-version '(cider . "0.9.0"))
(defcustom cider-disconnected-hook nil
"List of functions to call when disconnected from the Clojure nREPL server."
:type 'hook
:package-version '(cider . "0.9.0"))
(defcustom cider-inject-dependencies-at-jack-in t
"When nil, do not inject repl dependencies at `cider-jack-in' time.
The repl dependendcies are most likely to be nREPL middlewares."
:type 'boolean
:safe #'booleanp
:version '(cider . "0.11.0"))
(defcustom cider-enable-nrepl-jvmti-agent nil
"When t, add `-Djdk.attach.allowAttachSelf' to the command line arguments.
This allows nREPL JVMTI agent to be loaded. It is needed for evaluation
interruption to properly work on Java 21 and above."
:type 'boolean
:safe #'booleanp
:version '(cider . "1.15.0"))
(defcustom cider-offer-to-open-cljs-app-in-browser t
"When nil, do not offer to open ClojureScript apps in a browser on connect."
:type 'boolean
:safe #'booleanp
:version '(cider . "0.15.0"))
(defvar cider-ps-running-lein-nrepls-command "ps u | grep leiningen"
"Process snapshot command used in `cider-locate-running-nrepl-ports'.")
(defvar cider-ps-running-lein-nrepl-path-regexp-list
'("\\(?:leiningen.original.pwd=\\)\\(.+?\\) -D"
"\\(?:-classpath +:?\\(.+?\\)/self-installs\\)")
"Regexp list to get project paths.
Extract project paths from output of `cider-ps-running-lein-nrepls-command'.
Sub-match 1 must be the project path.")
(defvar cider-host-history nil
"Completion history for connection hosts.")
(defvar cider-jack-in-universal-options
'((clojure-cli (:prefix-arg 1 :cmd (:jack-in-type clj :project-type clojure-cli :edit-project-dir t)))
(lein (:prefix-arg 2 :cmd (:jack-in-type clj :project-type lein :edit-project-dir t)))
(babashka (:prefix-arg 3 :cmd (:jack-in-type clj :project-type babashka :edit-project-dir t)))
(nbb (:prefix-arg 4 :cmd (:jack-in-type cljs :project-type nbb :cljs-repl-type nbb :edit-project-dir t)))
(basilisp (:prefix-arg 5 :cmd (:jack-in-type clj :project-type basilisp :edit-project-dir t))))
"The list of project tools that are supported by the universal jack in command.
Each item in the list consists of the tool name and its plist options.
The plist supports the following keys
- :prefix-arg the numerical prefix arg to use to jack in to the tool.
- :cmd a plist of instructions how to invoke the jack in command, with keys
- :jack-in-type 'clj to start a clj repl and 'cljs for a cljs repl.
- &rest the same set of params supported by the `cider-jack-in-clj' and
`cider-jack-in-cljs' commands.")
;;;###autoload
(defun cider-version ()
"Display CIDER's version."
(interactive)
(message "CIDER %s" (cider--version)))
(defun cider-jack-in-command (project-type)
"Determine the command `cider-jack-in' needs to invoke for the PROJECT-TYPE."
(pcase project-type
('lein cider-lein-command)
('boot cider-boot-command)
('clojure-cli cider-clojure-cli-command)
('babashka cider-babashka-command)
('shadow-cljs cider-shadow-cljs-command)
('gradle cider-gradle-command)
('nbb cider-nbb-command)
('basilisp cider-basilisp-command)
(_ (user-error "Unsupported project type `%S'" project-type))))
(defcustom cider-enrich-classpath nil
"If t, use enrich-classpath for adding sources/javadocs to the classpath.
enrich-classpath is a Clojure CLI shim, and Leiningen plugin.
This classpath expansion is done in a clean manner,
without interfering with classloaders."
:type 'boolean
:package-version '(cider . "1.2.0")
:safe #'booleanp)
(defun cider--get-enrich-classpath-lein-script ()
"Returns the location of enrich-classpath's lein.sh wrapper script."
(when-let ((cider-location (locate-library "cider.el" t)))
(concat (file-name-directory cider-location)
"lein.sh")))
(defun cider--get-enrich-classpath-clojure-cli-script ()
"Returns the location of enrich-classpath's clojure.sh wrapper script."
(when-let ((cider-location (locate-library "cider.el" t)))
(concat (file-name-directory cider-location)
"clojure.sh")))
(defun cider-jack-in-resolve-command (project-type)
"Determine the resolved file path to `cider-jack-in-command'.
Throws an error if PROJECT-TYPE is unknown."
(pcase project-type
('lein (let ((r (cider--resolve-command cider-lein-command)))
(if (and cider-enrich-classpath
(not (eq system-type 'windows-nt))
(executable-find (cider--get-enrich-classpath-lein-script)))
(concat "bash " ;; don't assume lein.sh is executable - MELPA might change that
(cider--get-enrich-classpath-lein-script)
" "
r)
r)))
('boot (cider--resolve-command cider-boot-command))
('clojure-cli (if (and cider-enrich-classpath
(not (eq system-type 'windows-nt))
(executable-find (cider--get-enrich-classpath-clojure-cli-script)))
(concat "bash " ;; don't assume clojure.sh is executable - MELPA might change that
(cider--get-enrich-classpath-clojure-cli-script)
" "
(cider--resolve-command cider-clojure-cli-command))
(cider--resolve-command cider-clojure-cli-command)))
('babashka (cider--resolve-command cider-babashka-command))
;; here we have to account for the possibility that the command is either
;; "npx shadow-cljs" or just "shadow-cljs"
('shadow-cljs (let ((parts (split-string cider-shadow-cljs-command)))
(when-let* ((command (cider--resolve-command (car parts))))
(mapconcat #'identity (cons command (cdr parts)) " "))))
;; TODO: Address the duplicated code below.
;; here we have to account for the possibility that the command is either
;; "nbb" (default) or "npx nbb".
('nbb (let ((parts (split-string cider-nbb-command)))
(when-let* ((command (cider--resolve-command (car parts))))
(mapconcat #'identity (cons command (cdr parts)) " "))))
;; here we have to account for use of the Gradle wrapper which is
;; a shell script within their project, so if they have a clearly
;; relative path like "./gradlew" use locate file instead of checking
;; the exec-path
('gradle (cider--resolve-project-command cider-gradle-command))
('basilisp (cider--resolve-command cider-basilisp-command))
(_ (user-error "Unsupported project type `%S'" project-type))))
(defun cider-jack-in-global-options (project-type)
"Determine the command line options for `cider-jack-in' for the PROJECT-TYPE."
(pcase project-type
('lein cider-lein-global-options)
('boot cider-boot-global-options)
('clojure-cli cider-clojure-cli-global-options)
('babashka cider-babashka-global-options)
('shadow-cljs cider-shadow-cljs-global-options)
('gradle cider-gradle-global-options)
('nbb cider-nbb-global-options)
('basilisp nil)
(_ (user-error "Unsupported project type `%S'" project-type))))
(defun cider-jack-in-params (project-type)
"Determine the commands params for `cider-jack-in' for the PROJECT-TYPE."
;; The format of these command-line strings must consider different shells,
;; different values of IFS, and the possibility that they'll be run remotely
;; (e.g. with TRAMP). Using `", "` causes problems with TRAMP, for example.
;; Please be careful when changing them.
(pcase project-type
('lein cider-lein-parameters)
('boot cider-boot-parameters)
('clojure-cli cider-clojure-cli-parameters)
('babashka cider-babashka-parameters)
('shadow-cljs cider-shadow-cljs-parameters)
('gradle cider-gradle-parameters)
('nbb cider-nbb-parameters)
('basilisp cider-basilisp-parameters)
(_ (user-error "Unsupported project type `%S'" project-type))))
;;; Jack-in dependencies injection
(defvar cider-jack-in-dependencies nil
"List of dependencies where elements are lists of artifact name and version.")
(put 'cider-jack-in-dependencies 'risky-local-variable t)
(defcustom cider-injected-nrepl-version "1.3.0"
"The version of nREPL injected on jack-in.
We inject the newest known version of nREPL just in case
your version of Boot or Leiningen is bundling an older one."
:type 'string
:package-version '(cider . "1.2.0")
:safe #'stringp)
(defvar cider-jack-in-cljs-dependencies nil
"List of dependencies where elements are lists of artifact name and version.
Added to `cider-jack-in-dependencies' when doing `cider-jack-in-cljs'.")
(put 'cider-jack-in-cljs-dependencies 'risky-local-variable t)
(cider-add-to-alist 'cider-jack-in-cljs-dependencies "cider/piggieback" "0.5.3")
(defvar cider-jack-in-dependencies-exclusions nil
"List of exclusions for jack in dependencies.
Elements of the list are artifact name and list of exclusions to apply for
the artifact.")
(put 'cider-jack-in-dependencies-exclusions 'risky-local-variable t)
(defconst cider-clojure-artifact-id "org.clojure/clojure"
"Artifact identifier for Clojure.")
(defconst cider-minimum-clojure-version "1.10.0"
"Minimum supported version of Clojure.")
(defconst cider-latest-clojure-version "1.11.3"
"Latest (newest) version of Clojure.
Used when `cider-jack-in-auto-inject-clojure' is set to `latest'.")
(defconst cider-required-middleware-version "0.50.3"
"The CIDER nREPL version that's known to work properly with CIDER.")
(defcustom cider-injected-middleware-version cider-required-middleware-version
"The version of cider-nrepl injected on jack-in.
Should be newer than the required version for optimal results."
:type 'string
:package-version '(cider . "1.2.0")
:safe #'stringp)
(defcustom cider-jack-in-auto-inject-clojure nil
"Version of clojure to auto-inject into REPL.
If nil, do not inject Clojure into the REPL. If `latest', inject
`cider-latest-clojure-version', which should approximate to the most recent
version of Clojure. If `minimal', inject `cider-minimum-clojure-version',
which will be the lowest version CIDER supports. If a string, use this as
the version number. If it is a list, the first element should be a string,
specifying the artifact ID, and the second element the version number."
:type '(choice (const :tag "None" nil)
(const :tag "Latest" latest)
(const :tag "Minimal" minimal)
(string :tag "Specific Version")
(list :tag "Artifact ID and Version"
(string :tag "Artifact ID")
(string :tag "Version"))))
(defvar-local cider-jack-in-cmd nil
"The custom command used to start a nrepl server.
This is used by `cider-jack-in`.
If this variable is set, its value will be
used as the command to start the nrepl server
instead of the default command inferred from
the project type.
This allows for fine-grained control over the jack-in process.
The value should be a string representing the command to start
the nrepl server, such as \"nbb nrepl-server\".")
(defvar cider-jack-in-lein-plugins nil
"List of Leiningen plugins to be injected at jack-in.
Each element is a list of artifact name and version, followed optionally by
keyword arguments. The only keyword argument currently accepted is
`:predicate', which should be given a function that takes the list (name,
version, and keyword arguments) and returns non-nil to indicate that the
plugin should actually be injected. (This is useful primarily for packages
that extend CIDER, not for users. For example, a refactoring package might
want to inject some middleware only when within a project context.)")
(put 'cider-jack-in-lein-plugins 'risky-local-variable t)
(defvar cider-jack-in-lein-middlewares nil
"List of Leiningen :middleware values to be injected at jack-in.
Necessary for plugins which require an explicit middleware name to be specified.
Can also facilitate using middleware in a specific order.")
(put 'cider-jack-in-lein-middlewares 'risky-local-variable t)
(defvar cider-jack-in-cljs-lein-plugins nil
"List of Leiningen plugins to be injected at jack-in.
Added to `cider-jack-in-lein-plugins' (which see) when doing
`cider-jack-in-cljs'.")
(put 'cider-jack-in-cljs-lein-plugins 'risky-local-variable t)
(defun cider-jack-in-normalized-lein-plugins ()
"Return a normalized list of Leiningen plugins to be injected.
See `cider-jack-in-lein-plugins' for the format, except that the list
returned by this function does not include keyword arguments."
(let ((plugins (if cider-enrich-classpath
(append cider-jack-in-lein-plugins
`(("cider/cider-nrepl" ,cider-injected-middleware-version)
("mx.cider/lein-enrich-classpath" "1.19.3")))
(append cider-jack-in-lein-plugins
`(("cider/cider-nrepl" ,cider-injected-middleware-version))))))
(thread-last plugins
(seq-filter
(lambda (spec)
(if-let* ((pred (plist-get (seq-drop spec 2) :predicate)))
(funcall pred spec)
t)))
(mapcar
(lambda (spec)
(seq-take spec 2))))))
(defvar cider-jack-in-nrepl-middlewares nil
"List of Clojure variable names.
Each of these Clojure variables should hold a vector of nREPL middlewares.
Instead of a string, an element can be a list containing a string followed
by optional keyword arguments. The only keyword argument currently
accepted is `:predicate', which should be given a function that takes the
list (string and keyword arguments) and returns non-nil to indicate that
the middlewares should actually be injected.")
(put 'cider-jack-in-nrepl-middlewares 'risky-local-variable t)
(add-to-list 'cider-jack-in-nrepl-middlewares "cider.nrepl/cider-middleware")
(defvar cider-jack-in-cljs-nrepl-middlewares nil
"List of Clojure variable names.
Added to `cider-jack-in-nrepl-middlewares' (which see) when doing
`cider-jack-in-cljs'.")
(put 'cider-jack-in-cljs-nrepl-middlewares 'risky-local-variable t)
(add-to-list 'cider-jack-in-cljs-nrepl-middlewares "cider.piggieback/wrap-cljs-repl")
(defun cider-jack-in-normalized-nrepl-middlewares ()
"Return a normalized list of middleware variable names.
See `cider-jack-in-nrepl-middlewares' for the format, except that the list
returned by this function only contains strings."
(thread-last cider-jack-in-nrepl-middlewares
(seq-filter
(lambda (spec)
(or (not (listp spec))
(if-let* ((pred (plist-get (cdr spec) :predicate)))
(funcall pred spec)
t))))
(mapcar
(lambda (spec)
(if (listp spec)
(car spec)
spec)))))
(defun cider--list-as-boot-artifact (list)
"Return a boot artifact string described by the elements of LIST.
LIST should have the form (ARTIFACT-NAME ARTIFACT-VERSION). The returned
string is quoted for passing as argument to an inferior shell."
(concat "-d " (shell-quote-argument (format "%s:%s" (car list) (cadr list)))))
(defun cider--jack-in-required-dependencies ()
"Returns the required CIDER deps.
They are normally added to `cider-jack-in-dependencies',
unless it's a Lein project."
`(("nrepl/nrepl" ,cider-injected-nrepl-version)
("cider/cider-nrepl" ,cider-injected-middleware-version)))
(defun cider-boot-dependencies (dependencies)
"Return a list of boot artifact strings created from DEPENDENCIES."
(concat (mapconcat #'cider--list-as-boot-artifact dependencies " ")
(unless (seq-empty-p dependencies) " ")))
(defun cider-boot-middleware-task (params middlewares)
"Create a command to add MIDDLEWARES with corresponding PARAMS."
(concat "cider.tasks/add-middleware "
(mapconcat (lambda (middleware)
(format "-m %s" (shell-quote-argument middleware)))
middlewares
" ")
" " params))
(defun cider-boot-jack-in-dependencies (global-opts params dependencies middlewares)
"Create boot jack-in dependencies.
Does so by concatenating GLOBAL-OPTS, DEPENDENCIES,
and MIDDLEWARES. PARAMS and MIDDLEWARES are passed on to
`cider-boot-middleware-task` before concatenating and DEPENDENCIES
are passed on to `cider-boot-dependencies`."
(concat global-opts
(unless (seq-empty-p global-opts) " ")
"-i \"(require 'cider.tasks)\" " ;; Note the space at the end here
(cider-boot-dependencies (append (cider--jack-in-required-dependencies) dependencies))
(cider-boot-middleware-task params middlewares)))
(defun cider--gradle-dependency-notation (dependency)
"Returns Gradle's GAV dependency syntax.
For a \"group/artifact\" \"version\") DEPENDENCY list
return as group:artifact:version notation."
(let ((group-artifact (replace-regexp-in-string "/" ":" (car dependency)))
(version (cadr dependency)))
(format "%s:%s" group-artifact version)))
(defun cider--gradle-jack-in-property (dependencies)
"Returns Clojurephant's dependency jack-in property.
For DEPENDENCIES, translates to Gradle's dependency notation
using `cider--gradle-dependency-notation`.''"
(if (seq-empty-p dependencies)
""
(shell-quote-argument
(concat "-Pdev.clojurephant.jack-in.nrepl="
(mapconcat #'cider--gradle-dependency-notation dependencies ",")))))
(defun cider--gradle-middleware-params (middlewares)
"Returns Gradle-formatted middleware params.
Given a list of MIDDLEWARES symbols, this returns
the Gradle parameters expected by Clojurephant's
ClojureNRepl task."
(mapconcat (lambda (middleware)
(shell-quote-argument (concat "--middleware=" middleware)))
middlewares
" "))
(defun cider-gradle-jack-in-dependencies (global-opts params dependencies middlewares)
"Create gradle jack in dependencies.
Does so by concatenating GLOBAL-OPTS, DEPENDENCIES,
and MIDDLEWARES. GLOBAL-OPTS and PARAMS are taken as-is.
DEPENDENCIES are translated into Gradle's typical
group:artifact:version notation and MIDDLEWARES are
prepared as arguments to Clojurephant's ClojureNRepl task."
(concat global-opts
(unless (seq-empty-p global-opts) " ")
(when cider-enable-nrepl-jvmti-agent
"-Pjdk.attach.allowAttachSelf ")
(cider--gradle-jack-in-property (append (cider--jack-in-required-dependencies) dependencies))
" "
params
(unless (seq-empty-p params) " ")
(cider--gradle-middleware-params middlewares)))
(defun cider--lein-artifact-exclusions (exclusions)
"Return an exclusions vector described by the elements of EXCLUSIONS."
(if exclusions
(format " :exclusions [%s]" (mapconcat #'identity exclusions " "))
""))
(defun cider--list-as-lein-artifact (list &optional exclusions)
"Return an artifact string described by the elements of LIST.
LIST should have the form (ARTIFACT-NAME ARTIFACT-VERSION). Optionally a list
of EXCLUSIONS can be provided as well. The returned
string is quoted for passing as argument to an inferior shell."
(shell-quote-argument (format "[%s %S%s]" (car list) (cadr list) (cider--lein-artifact-exclusions exclusions))))
(defun cider--extract-lein-profiles (lein-params)
"Extracts a list of ('with-profile ...' and a repl command from LEIN-PARAMS).
If no `with-profile' call was found,
returns an empty string as the first member."
(or (when-let* ((pattern "\\(with-profiles?\\s-+\\S-+\\)")
(match-start (string-match pattern lein-params))
(match-end (match-end 0)))
(list (concat (substring lein-params match-start match-end) " ")
(string-trim (substring lein-params match-end))))
(list "" lein-params)))
(defun cider-lein-jack-in-dependencies (global-opts params dependencies dependencies-exclusions lein-plugins &optional lein-middlewares)
"Create lein jack-in dependencies.
Does so by concatenating GLOBAL-OPTS, DEPENDENCIES, with DEPENDENCIES-EXCLUSIONS
removed, LEIN-PLUGINS, LEIN-MIDDLEWARES and finally PARAMS."
(concat
global-opts
(unless (seq-empty-p global-opts) " ")
(mapconcat #'identity
(append (seq-map (lambda (dep)
(let ((exclusions (cadr (assoc (car dep) dependencies-exclusions))))
(concat "update-in :dependencies conj "
(cider--list-as-lein-artifact dep exclusions))))
dependencies)
(seq-map (lambda (plugin)
(concat "update-in :plugins conj "
(cider--list-as-lein-artifact plugin)))
lein-plugins)
(seq-map (lambda (middleware)
(concat "update-in :middleware conj "
middleware))
lein-middlewares)
(when cider-enable-nrepl-jvmti-agent
`(,(concat "update-in :jvm-opts conj '\"-Djdk.attach.allowAttachSelf\"'"))))
" -- ")
" -- "
(if (not cider-enrich-classpath)
params
;; enrich-classpath must be applied after the `with-profile` call, if present,
;; so that it can also process the classpath that is typically expanded by the presence of a set of profiles:
(let* ((profiles-and-repl-call (cider--extract-lein-profiles params))
(profiles (car profiles-and-repl-call))
(repl-call (nth 1 profiles-and-repl-call)))
(concat profiles
"update-in :middleware conj cider.enrich-classpath.plugin-v2/middleware -- "
repl-call)))))
(defun cider--dedupe-deps (deps)
"Removes the duplicates in DEPS."
(cl-delete-duplicates deps :test 'equal))
(defun cider--jack-in-cmd-powershell-p (command)
"Returns whether COMMAND is PowerShell."
(or (string-equal command "powershell")
(string-equal command "pwsh")))
(defun cider--shell-quote-argument (argument &optional command)
"Quotes ARGUMENT like `shell-quote-argument', suitable for use with COMMAND.
Uses `shell-quote-argument' to quote the ARGUMENT, unless COMMAND is given
and refers to PowerShell, in which case it uses (some limited) PowerShell
rules to quote it."
(if (cider--jack-in-cmd-powershell-p command)
;; please add more PowerShell quoting rules as necessary.
(format "'%s'" (replace-regexp-in-string "\"" "\"\"" argument))
(shell-quote-argument argument)))
(defun cider--powershell-encode-command (cmd-params)
"Base64 encode the powershell command and jack-in CMD-PARAMS for clojure-cli."
(let* ((quoted-params cmd-params)
;; Also ensure compatibility with pwsh 7.3 quoting rules
;;
;; https://stackoverflow.com/a/59036879
(command (format "$PSNativeCommandArgumentPassing = 'Legacy'; clojure %s" quoted-params))
(utf-16le-command (encode-coding-string command 'utf-16le)))
(format "-encodedCommand %s" (base64-encode-string utf-16le-command t))))
(defun cider--combined-aliases ()
"Creates the combined ailases as stringe separated by ':'."
(let ((final-cider-clojure-cli-aliases
(cond ((and cider-clojure-cli-global-aliases cider-clojure-cli-aliases)
(concat cider-clojure-cli-global-aliases ":" cider-clojure-cli-aliases))
(cider-clojure-cli-global-aliases cider-clojure-cli-global-aliases)
(t cider-clojure-cli-aliases))))
(if final-cider-clojure-cli-aliases
;; remove exec-opts flags -A -M -T or -X from cider-clojure-cli-aliases
;; concatenated with :cider/nrepl to ensure :cider/nrepl comes last
(let ((aliases (format "%s" (replace-regexp-in-string "^-\\(A\\|M\\|T\\|X\\)" "" final-cider-clojure-cli-aliases))))
(if (string-prefix-p ":" aliases)
aliases
(concat ":" aliases)))
"")))
(defun cider-clojure-cli-jack-in-dependencies (global-options params dependencies &optional command)
"Create Clojure tools.deps jack-in dependencies.
Does so by concatenating DEPENDENCIES, PARAMS and GLOBAL-OPTIONS into a
suitable `clojure` invocation and quoting, also accounting for COMMAND if
provided. The main is placed in an inline alias :cider/nrepl so that if
your aliases contain any mains, the cider/nrepl one will be the one used."
(let* ((all-deps (thread-last dependencies
(append (cider--jack-in-required-dependencies))
;; Duplicates are never OK since they would result in
;; `java.lang.IllegalArgumentException: Duplicate key [...]`:
(cider--dedupe-deps)
(seq-map (lambda (dep)
(if (listp (cadr dep))
(format "%s {%s}"
(car dep)
(seq-reduce
(lambda (acc v)
(concat acc (format " :%s \"%s\" " (car v) (cdr v))))
(cadr dep)
""))
(format "%s {:mvn/version \"%s\"}" (car dep) (cadr dep)))))))
(middleware (mapconcat
(apply-partially #'format "%s")
(cider-jack-in-normalized-nrepl-middlewares)
","))
(main-opts (format "\"-m\" \"nrepl.cmdline\" \"--middleware\" \"[%s]\"" middleware))
(deps (format "{:deps {%s} :aliases {:cider/nrepl {%s:main-opts [%s]}}}"
(string-join all-deps " ")
(if cider-enable-nrepl-jvmti-agent
":jvm-opts [\"-Djdk.attach.allowAttachSelf\"], "
"")
main-opts))
(deps-quoted (cider--shell-quote-argument deps command)))
(format "%s-Sdeps %s -M%s:cider/nrepl%s"
;; TODO: global-options are deprecated and should be removed in CIDER 2.0
(if global-options (format "%s " global-options) "")
deps-quoted
(cider--combined-aliases)
(if params (format " %s" params) ""))))
(defun cider-shadow-cljs-jack-in-dependencies (global-opts params dependencies)
"Create shadow-cljs jack-in deps.
Does so by concatenating GLOBAL-OPTS, DEPENDENCIES finally PARAMS."
(let ((dependencies (append (cider--jack-in-required-dependencies) dependencies)))
(concat
global-opts
(unless (seq-empty-p global-opts) " ")
(mapconcat #'identity
(seq-map (lambda (dep) (format "-d %s:%s" (car dep) (cadr dep))) dependencies)
" ")
" "
params)))
(defun cider-add-clojure-dependencies-maybe (dependencies)
"Return DEPENDENCIES with an added Clojure dependency if requested.
See also `cider-jack-in-auto-inject-clojure'."
(if cider-jack-in-auto-inject-clojure
(if (consp cider-jack-in-auto-inject-clojure)
(cons cider-jack-in-auto-inject-clojure dependencies)
(cons (list cider-clojure-artifact-id
(cond
((stringp cider-jack-in-auto-inject-clojure)
cider-jack-in-auto-inject-clojure)
((eq cider-jack-in-auto-inject-clojure 'minimal)
cider-minimum-clojure-version)
((eq cider-jack-in-auto-inject-clojure 'latest)
cider-latest-clojure-version)))
dependencies))
dependencies))
(defun cider-inject-jack-in-dependencies (global-opts params project-type &optional command)
"Return GLOBAL-OPTS and PARAMS with injected REPL dependencies.
These are set in `cider-jack-in-dependencies', `cider-jack-in-lein-plugins'
and `cider-jack-in-nrepl-middlewares' are injected from the CLI according
to the used PROJECT-TYPE, and COMMAND if provided. Eliminates the need for
hacking profiles.clj or the boot script for supporting CIDER with its nREPL
middleware and dependencies."
(pcase project-type
('lein (cider-lein-jack-in-dependencies
global-opts
params
(cider-add-clojure-dependencies-maybe
(append `(("nrepl/nrepl" ,cider-injected-nrepl-version)) cider-jack-in-dependencies))
cider-jack-in-dependencies-exclusions
(cider-jack-in-normalized-lein-plugins)
cider-jack-in-lein-middlewares))
('boot (cider-boot-jack-in-dependencies
global-opts
params
(cider-add-clojure-dependencies-maybe
cider-jack-in-dependencies)
(cider-jack-in-normalized-nrepl-middlewares)))
('clojure-cli (cider-clojure-cli-jack-in-dependencies
global-opts
params
(cider-add-clojure-dependencies-maybe
cider-jack-in-dependencies)
command))
('babashka (concat
global-opts
(unless (seq-empty-p global-opts) " ")
params))
('shadow-cljs (cider-shadow-cljs-jack-in-dependencies
global-opts
params
(cider-add-clojure-dependencies-maybe
cider-jack-in-dependencies)))
('gradle (cider-gradle-jack-in-dependencies
global-opts
params
(cider-add-clojure-dependencies-maybe
cider-jack-in-dependencies)
(cider-jack-in-normalized-nrepl-middlewares)))
('nbb (concat