Skip to content

Commit db9a614

Browse files
committed
Add clojure-ts-mode support
1 parent 9aea501 commit db9a614

File tree

3 files changed

+106
-52
lines changed

3 files changed

+106
-52
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Changelog
22

33
## master (unreleased)
4+
45
* Improve support for multiple forms in the same line by replacing beginning-of-defun fn.
56
* [#202](https://github.com/clojure-emacs/inf-clojure/issues/202): Add ClojureCLR support.
67
* [#204](https://github.com/clojure-emacs/inf-clojure/issues/204): Scroll repl buffer on insert commands
78
* [#208](https://github.com/clojure-emacs/inf-clojure/pull/208) Display message after setting repl.
8-
* [#210](https://github.com/clojure-emacs/inf-clojure/pull/210) Include `inf-clojure-socket-repl` to create a socket REPL and connect to it from inside Emacs.
9-
9+
* [#210](https://github.com/clojure-emacs/inf-clojure/pull/210) Include `inf-clojure-socket-repl` to create a socket REPL and connect to it from inside Emacs.
10+
- Add `clojure-ts-mode` support.
1011

1112
## 3.2.1 (2022-07-22)
1213

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ You can also add the following to your Emacs config to enable
103103

104104
```emacs-lisp
105105
(add-hook 'clojure-mode-hook #'inf-clojure-minor-mode)
106+
107+
;; or if you're a `clojure-ts-mode' user:
108+
109+
(add-hook 'clojure-ts-mode-hook #'inf-clojure-minor-mode)
106110
```
107111

108112
**Warning:** Don't enable `inf-clojure-minor-mode` and `cider-mode` at the same time. They
@@ -217,6 +221,22 @@ If you want to update a specific form there is a function
217221
(inf-clojure-update-feature 'clojure 'completion "(incomplete.core/completions \"%s\")")
218222
```
219223

224+
### `clojure-ts-mode` support
225+
226+
To use `inf-clojure` with `clojure-ts-mode`, you can adjust the
227+
following variable:
228+
229+
```emacs-lisp
230+
(setopt inf-clojure-source-modes '(clojure-ts-mode clojure-mode))
231+
```
232+
233+
If you want to use `inf-clojure` with `clojure-ts-mode` exclusively,
234+
you can set it to:
235+
236+
```emacs-lisp
237+
(setopt inf-clojure-source-modes '(clojure-ts-mode))
238+
```
239+
220240
#### Caveats
221241

222242
As `inf-clojure` is built on top of `comint` it has all the usual comint limitations -

inf-clojure.el

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
(require 'comint)
6767
(require 'clojure-mode)
68+
(require 'clojure-ts-mode nil :no-error)
6869
(require 'eldoc)
6970
(require 'thingatpt)
7071
(require 'ansi-color)
@@ -193,10 +194,10 @@ either `setq-local` or an entry in `.dir-locals.el`." )
193194
MULTIPLE PROCESS SUPPORT
194195
===========================================================================
195196
To run multiple Clojure processes, you start the first up
196-
with \\[inf-clojure]. It will be in a buffer named `*inf-clojure*'.
197+
with \\[inf-clojure]. It will be in a buffer named *inf-clojure*.
197198
Rename this buffer with \\[rename-buffer]. You may now start up a new
198199
process with another \\[inf-clojure]. It will be in a new buffer,
199-
named `*inf-clojure*'. You can switch between the different process
200+
named *inf-clojure*. You can switch between the different process
200201
buffers with \\[switch-to-buffer].
201202
202203
Commands that send text from source buffers to Clojure processes --
@@ -205,7 +206,7 @@ process to send to, when you have more than one Clojure process around. This
205206
is determined by the global variable `inf-clojure-buffer'. Suppose you
206207
have three inferior Clojures running:
207208
Buffer Process
208-
foo inf-clojure
209+
foo `inf-clojure'
209210
bar inf-clojure<2>
210211
*inf-clojure* inf-clojure<3>
211212
If you do a \\[inf-clojure-eval-defun] command on some Clojure source code,
@@ -269,7 +270,7 @@ has been found. See also variable `inf-clojure-buffer'."
269270
(error "No Clojure subprocess; see variable `inf-clojure-buffer'"))))
270271

271272
(defun inf-clojure-repl-p (&optional buf)
272-
"Indicates if BUF is an inf-clojure REPL.
273+
"Indicates if BUF is an `inf-clojure' REPL.
273274
If BUF is nil then defaults to the current buffer.
274275
Checks the mode and that there is a live process."
275276
(let ((buf (or buf (current-buffer))))
@@ -278,35 +279,35 @@ Checks the mode and that there is a live process."
278279
(process-live-p (get-buffer-process buf)))))
279280

280281
(defun inf-clojure-repls ()
281-
"Return a list of all inf-clojure REPL buffers."
282+
"Return a list of all `inf-clojure' REPL buffers."
282283
(let (repl-buffers)
283284
(dolist (b (buffer-list))
284285
(when (inf-clojure-repl-p b)
285286
(push (buffer-name b) repl-buffers)))
286287
repl-buffers))
287288

288289
(defun inf-clojure--prompt-repl-buffer (prompt)
289-
"Prompt the user to select an inf-clojure repl buffer.
290+
"Prompt the user to select an `inf-clojure' repl buffer.
290291
PROMPT is a string to prompt the user.
291292
Returns nil when no buffer is selected."
292293
(let ((repl-buffers (inf-clojure-repls)))
293294
(if (> (length repl-buffers) 0)
294-
(when-let ((repl-buffer (completing-read prompt repl-buffers nil t)))
295+
(when-let* ((repl-buffer (completing-read prompt repl-buffers nil t)))
295296
(get-buffer repl-buffer))
296297
(user-error "No buffers have an inf-clojure process"))))
297298

298299
(defun inf-clojure-set-repl (always-ask)
299-
"Set an inf-clojure buffer as the active (default) REPL.
300+
"Set an `inf-clojure' buffer as the active (default) REPL.
300301
If in a REPL buffer already, use that unless a prefix is used (or
301-
ALWAYS-ASK). Otherwise get a list of all active inf-clojure
302+
ALWAYS-ASK). Otherwise get a list of all active `inf-clojure'
302303
REPLS and offer a choice. It's recommended to rename REPL
303304
buffers after they are created with `rename-buffer'."
304305
(interactive "P")
305-
(when-let ((new-repl-buffer
306-
(if (or always-ask
307-
(not (inf-clojure-repl-p)))
308-
(inf-clojure--prompt-repl-buffer "Select default REPL: ")
309-
(current-buffer))))
306+
(when-let* ((new-repl-buffer
307+
(if (or always-ask
308+
(not (inf-clojure-repl-p)))
309+
(inf-clojure--prompt-repl-buffer "Select default REPL: ")
310+
(current-buffer))))
310311
(setq inf-clojure-buffer new-repl-buffer)
311312
(message "Current inf-clojure REPL set to %s" new-repl-buffer)))
312313

@@ -349,6 +350,14 @@ mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword
349350
\(as in :a, :c, etc.)"
350351
:type 'regexp)
351352

353+
(defcustom inf-clojure-source-modes '(clojure-mode)
354+
"Used to determine if a buffer contains Clojure source code.
355+
356+
Any buffer with one of these major modes, it's considered a Clojure
357+
source file by all `inf-clojure' commands."
358+
:type '(repeat symbol)
359+
:safe #'symbolp)
360+
352361
(defun inf-clojure--modeline-info ()
353362
"Return modeline info for `inf-clojure-minor-mode'.
354363
Either \"no process\" or \"buffer-name(repl-type)\""
@@ -453,7 +462,7 @@ The value of this variable is a mode line template as in
453462
`mode-line-format'. See Info Node `(elisp)Mode Line Format' for details
454463
about mode line templates.
455464
456-
Customize this variable to change how inf-clojure-minor-mode
465+
Customize this variable to change how `inf-clojure-minor-mode'
457466
displays its status in the mode line. The default value displays
458467
the current REPL. Set this variable to nil to disable the
459468
mode line entirely."
@@ -609,24 +618,35 @@ This should usually be a combination of `inf-clojure-prompt' and
609618
:package-version '(inf-clojure . "2.0.0"))
610619

611620
(defcustom inf-clojure-auto-mode t
612-
"Automatically enable inf-clojure-minor-mode.
621+
"Automatically enable `inf-clojure-minor-mode'.
613622
All buffers in `clojure-mode' will automatically be in
614623
`inf-clojure-minor-mode' unless set to nil."
615624
:type 'boolean
616625
:safe #'booleanp
617626
:package-version '(inf-clojure . "3.1.0"))
618627

628+
(defun inf-clojure--get-preferred-major-modes ()
629+
"Return list of preferred major modes that are actually available."
630+
(cl-remove-if-not (lambda (mode) (featurep mode))
631+
inf-clojure-source-modes))
632+
633+
(defun inf-clojure--clojure-buffer-p ()
634+
"Return TRUE if the current buffer is a Clojure buffer."
635+
(derived-mode-p (inf-clojure--get-preferred-major-modes)))
636+
619637
(defun inf-clojure--clojure-buffers ()
620638
"Return a list of all existing `clojure-mode' buffers."
621-
(cl-remove-if-not
622-
(lambda (buffer) (with-current-buffer buffer (derived-mode-p 'clojure-mode)))
623-
(buffer-list)))
639+
(cl-remove-if-not (lambda (buffer)
640+
(with-current-buffer buffer
641+
(inf-clojure--clojure-buffer-p)))
642+
(buffer-list)))
624643

625644
(defun inf-clojure-enable-on-existing-clojure-buffers ()
626645
"Enable inf-clojure's minor mode on existing Clojure buffers.
627646
See command `inf-clojure-minor-mode'."
628647
(interactive)
629-
(add-hook 'clojure-mode-hook #'inf-clojure-minor-mode)
648+
(dolist (mode (inf-clojure--get-preferred-major-modes))
649+
(add-hook (derived-mode-hook-name mode) #'inf-clojure-minor-mode))
630650
(dolist (buffer (inf-clojure--clojure-buffers))
631651
(with-current-buffer buffer
632652
(inf-clojure-minor-mode +1))))
@@ -688,8 +708,12 @@ If `comint-use-prompt-regexp' is nil (the default), \\[comint-insert-input] on
688708
(setq comint-input-sender 'inf-clojure--send-string)
689709
(setq comint-prompt-regexp inf-clojure-comint-prompt-regexp)
690710
(setq mode-line-process '(":%s"))
691-
(clojure-mode-variables)
692-
(clojure-font-lock-setup)
711+
;; We don't need to call it in Clojure buffers.
712+
(unless (inf-clojure--clojure-buffer-p)
713+
;; Using Tree-sitter based syntax highlighting in comint buffer is
714+
;; currently not possible.
715+
(clojure-mode-variables)
716+
(clojure-font-lock-setup))
693717
(when inf-clojure-enable-eldoc
694718
(inf-clojure-eldoc-setup))
695719
(setq comint-get-old-input #'inf-clojure-get-old-input)
@@ -801,8 +825,8 @@ The name is simply the final segment of the path."
801825

802826
;;;###autoload
803827
(defun inf-clojure (cmd &optional suppress-message)
804-
"Run an inferior Clojure process, input and output via buffer `*inf-clojure*'.
805-
If there is a process already running in `*inf-clojure*', just
828+
"Run an inferior Clojure process, input and output via buffer *inf-clojure*.
829+
If there is a process already running in *inf-clojure*, just
806830
switch to that buffer.
807831
808832
CMD is a string which serves as the startup command or a cons of
@@ -826,6 +850,8 @@ process buffer for a list of commands.)"
826850
(mapcar #'cdr inf-clojure-startup-forms)
827851
nil
828852
'confirm-after-completion))))
853+
;; TODO: Currently there is no `clojure-project-dir' analogue in
854+
;; `clojure-ts-mode'.
829855
(let* ((project-dir (clojure-project-dir))
830856
(process-buffer-name (or
831857
inf-clojure-custom-repl-name
@@ -850,7 +876,9 @@ process buffer for a list of commands.)"
850876
(with-current-buffer (apply #'make-comint
851877
process-buffer-name (car cmdlist) nil (cdr cmdlist))
852878
(inf-clojure-mode)
853-
(set-syntax-table clojure-mode-syntax-table)
879+
(set-syntax-table (pcase (car (inf-clojure--get-preferred-major-modes))
880+
('clojure-ts-mode clojure-ts-mode-syntax-table)
881+
(_ clojure-mode-syntax-table)))
854882
(setq-local inf-clojure-repl-type repl-type)
855883
(hack-dir-local-variables-non-file-buffer))))
856884
;; update the default comint buffer and switch to it
@@ -978,6 +1006,8 @@ of forms."
9781006
(condition-case nil
9791007
(with-temp-buffer
9801008
(progn
1009+
;; TODO: Should it be adjusted for `clojure-ts-mode'
1010+
;; somehow?
9811011
(clojurec-mode)
9821012
(insert str)
9831013
(whitespace-cleanup)
@@ -986,8 +1016,9 @@ of forms."
9861016
(while (looking-at "\n")
9871017
(delete-char 1))
9881018
(unless (eobp)
989-
(clojure-forward-logical-sexp))
990-
(unless (eobp)
1019+
;; There is no special API for that in
1020+
;; `clojure-ts-mode', so probably for now lets keep this
1021+
;; `clojure-mode' function.
9911022
(forward-char)))
9921023
(buffer-substring-no-properties (point-min) (point-max))))
9931024
(scan-error str)))
@@ -1105,13 +1136,6 @@ START and END are the beginning and end positions in the buffer to send."
11051136
This holds a cons cell of the form `(DIRECTORY . FILE)'
11061137
describing the last `inf-clojure-load-file' command.")
11071138

1108-
(defcustom inf-clojure-source-modes '(clojure-mode)
1109-
"Used to determine if a buffer contains Clojure source code.
1110-
If it's loaded into a buffer that is in one of these major modes, it's
1111-
considered a Clojure source file by `inf-clojure-load-file'.
1112-
Used by this command to determine defaults."
1113-
:type '(repeat symbol))
1114-
11151139
(defun inf-clojure-load-file (&optional switch-to-repl file-name)
11161140
"Load a Clojure file into the inferior Clojure process.
11171141
@@ -1123,7 +1147,7 @@ is present it will be used instead of the current file."
11231147
(file-name (or file-name
11241148
(car (comint-get-source "Load Clojure file: " inf-clojure-prev-l/c-dir/file
11251149
;; nil because doesn't need an exact name
1126-
inf-clojure-source-modes nil))))
1150+
(inf-clojure--get-preferred-major-modes) nil))))
11271151
(load-form (inf-clojure-get-feature proc 'load)))
11281152
(comint-check-source file-name) ; Check to see if buffer needs saved.
11291153
(setq inf-clojure-prev-l/c-dir/file (cons (file-name-directory file-name)
@@ -1132,23 +1156,32 @@ is present it will be used instead of the current file."
11321156
(when switch-to-repl
11331157
(inf-clojure-switch-to-repl t))))
11341158

1159+
(defun inf-clojure--find-ns ()
1160+
"Return the namespace of the current Clojure buffer.
1161+
1162+
This function delegates its job to an appropritate function, considering
1163+
`inf-clojure-source-modes'."
1164+
(pcase (car (inf-clojure--get-preferred-major-modes))
1165+
('clojure-ts-mode (clojure-ts-find-ns))
1166+
(_ (clojure-find-ns))))
1167+
11351168
(defun inf-clojure-reload (arg)
11361169
"Send a query to the inferior Clojure for reloading the namespace.
1137-
See variable `inf-clojure-reload-form' and
1170+
See variable `inf-clojure-reload-form' and variable
11381171
`inf-clojure-reload-all-form'.
11391172
11401173
The prefix argument ARG can change the behavior of the command:
11411174
1142-
- C-u M-x `inf-clojure-reload': prompts for a namespace name.
1143-
- M-- M-x `inf-clojure-reload': executes (require ... :reload-all).
1144-
- M-- C-u M-x `inf-clojure-reload': reloads all AND prompts."
1175+
- \\`C-u' \\[inf-clojure-reload]: prompts for a namespace name.
1176+
- \\`M--' \\[inf-clojure-reload]: executes (require ... :reload-all).
1177+
- \\`M--' \\`C-u' \\[inf-clojure-reload]: reloads all AND prompts."
11451178
(interactive "P")
11461179
(let* ((proc (inf-clojure-proc))
11471180
(reload-all-p (or (equal arg '-) (equal arg '(-4))))
11481181
(prompt-p (or (equal arg '(4)) (equal arg '(-4))))
11491182
(ns (if prompt-p
1150-
(car (inf-clojure-symprompt "Namespace" (clojure-find-ns)))
1151-
(clojure-find-ns)))
1183+
(car (inf-clojure-symprompt "Namespace" (inf-clojure--find-ns)))
1184+
(inf-clojure--find-ns)))
11521185
(form (if (not reload-all-p)
11531186
(inf-clojure-reload-form proc)
11541187
(inf-clojure-reload-all-form proc))))
@@ -1357,11 +1390,11 @@ for evaluation, therefore FORM should not include it."
13571390
(defun inf-clojure-arglists (fn)
13581391
"Send a query to the inferior Clojure for the arglists for function FN.
13591392
See variable `inf-clojure-arglists-form'."
1360-
(when-let ((proc (inf-clojure-proc 'no-error)))
1361-
(when-let ((arglists-form (inf-clojure-get-feature proc 'arglists)))
1362-
(thread-first (format arglists-form fn)
1363-
(inf-clojure--process-response proc "(" ")")
1364-
(inf-clojure--some)))))
1393+
(when-let* ((proc (inf-clojure-proc 'no-error))
1394+
(arglists-form (inf-clojure-get-feature proc 'arglists)))
1395+
(thread-first (format arglists-form fn)
1396+
(inf-clojure--process-response proc "(" ")")
1397+
(inf-clojure--some))))
13651398

13661399
(defun inf-clojure-show-arglists (prompt-for-symbol)
13671400
"Show the arglists for function FN in the mini-buffer.
@@ -1383,8 +1416,8 @@ prefix argument PROMPT-FOR-NS, it prompts for a namespace name."
13831416
(interactive "P")
13841417
(let* ((proc (inf-clojure-proc))
13851418
(ns (if prompt-for-ns
1386-
(car (inf-clojure-symprompt "Ns vars" (clojure-find-ns)))
1387-
(clojure-find-ns)))
1419+
(car (inf-clojure-symprompt "Ns vars" (inf-clojure--find-ns)))
1420+
(inf-clojure--find-ns)))
13881421
(ns-vars-form (inf-clojure-get-feature proc 'ns-vars)))
13891422
(inf-clojure--send-string proc (format ns-vars-form ns))))
13901423

@@ -1396,8 +1429,8 @@ PROMPT-FOR-NS, it prompts for a namespace name."
13961429
(interactive "P")
13971430
(let* ((proc (inf-clojure-proc))
13981431
(ns (if prompt-for-ns
1399-
(car (inf-clojure-symprompt "Set ns to" (clojure-find-ns)))
1400-
(clojure-find-ns)))
1432+
(car (inf-clojure-symprompt "Set ns to" (inf-clojure--find-ns)))
1433+
(inf-clojure--find-ns)))
14011434
(set-ns-form (inf-clojure-get-feature proc 'set-ns)))
14021435
(when (or (not ns) (equal ns ""))
14031436
(user-error "No namespace selected"))

0 commit comments

Comments
 (0)