Skip to content

Commit d3c0d4f

Browse files
arichiardibbatsov
authored andcommitted
Avoid throwing an error for frequent operations like completion
This patch introduces an argument to inf-clojure-proc so that client code can decide when to error out return the processs. This is necessary because while it is good to communicate an error on "eval" operations, it is not good to have it on "under the hood" and very frequent ones like completion and get arglists.
1 parent 62bb0fc commit d3c0d4f

File tree

2 files changed

+75
-67
lines changed

2 files changed

+75
-67
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [#117](https://github.com/clojure-emacs/inf-clojure/pull/117): Introduce `tools.deps` project type and `inf-clojure-tools-deps-cmd`.
1111
* [#122](https://github.com/clojure-emacs/inf-clojure/pull/122): Introduce `inf-clojure-completions-fn` defcustom.
1212
* [#128](https://github.com/clojure-emacs/inf-clojure/pull/128): Expose `inf-clojure-apropos` as `C-c C-S-a` in `inf-clojure-mode` (the REPL).
13+
* [#125](https://github.com/clojure-emacs/inf-clojure/pull/125): Avoid throwing an error for frequent operations like completion.
1314

1415
### Bugs Fixed
1516

inf-clojure.el

Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@ Clojure to load that file."
363363
:safe #'stringp
364364
:package-version '(inf-clojure . "2.0.0"))
365365

366-
(defun inf-clojure-load-form ()
367-
"Return the form to query inferior Clojure for a var's documentation.
366+
(defun inf-clojure-load-form (proc)
367+
"Return the form to query the Inf-Clojure PROC for var's documentation.
368368
If you are using REPL types, it will pickup the most appropriate
369369
`inf-clojure-var-doc-form` variant."
370-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
370+
(pcase (inf-clojure--set-repl-type proc)
371371
(`lumo inf-clojure-load-form-lumo)
372372
(`planck inf-clojure-load-form-planck)
373373
(_ inf-clojure-load-form)))
@@ -689,15 +689,15 @@ The prefix argument SWITCH-TO-REPL controls whether to switch to
689689
REPL after the file is loaded or not. If the argument FILE-NAME
690690
is present it will be used instead of the current file."
691691
(interactive "P")
692-
(let ((file-name (or file-name
692+
(let ((proc (inf-clojure-proc))
693+
(file-name (or file-name
693694
(car (comint-get-source "Load Clojure file: " inf-clojure-prev-l/c-dir/file
694695
;; nil because doesn't need an exact name
695696
inf-clojure-source-modes nil)))))
696697
(comint-check-source file-name) ; Check to see if buffer needs saved.
697698
(setq inf-clojure-prev-l/c-dir/file (cons (file-name-directory file-name)
698699
(file-name-nondirectory file-name)))
699-
(inf-clojure--send-string (inf-clojure-proc)
700-
(format (inf-clojure-load-form) file-name))
700+
(inf-clojure--send-string proc (format (inf-clojure-load-form proc) file-name))
701701
(when switch-to-repl
702702
(inf-clojure-switch-to-repl t))))
703703

@@ -736,12 +736,12 @@ is present it will be used instead of the current file."
736736
:safe #'stringp
737737
:package-version '(inf-clojure . "2.0.0"))
738738

739-
(defun inf-clojure-var-doc-form ()
740-
"Return the form to query inferior Clojure for a var's documentation.
739+
(defun inf-clojure-var-doc-form (proc)
740+
"Return the form to query the Inf-Clojure PROC for a var's documentation.
741741
If you are using REPL types, it will pickup the most approapriate
742742
`inf-clojure-var-doc-form` variant."
743743
(inf-clojure--sanitize-command
744-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
744+
(pcase (inf-clojure--set-repl-type proc)
745745
(`lumo inf-clojure-var-doc-form-lumo)
746746
(`planck inf-clojure-var-doc-form-planck)
747747
(_ inf-clojure-var-doc-form))))
@@ -767,12 +767,12 @@ If you are using REPL types, it will pickup the most approapriate
767767
:safe #'stringp
768768
:package-version '(inf-clojure . "2.0.0"))
769769

770-
(defun inf-clojure-var-source-form ()
771-
"Return the form to query inferior Clojure for a var's source.
770+
(defun inf-clojure-var-source-form (proc)
771+
"Return the form to query the Inf-Clojure PROC for a var's source.
772772
If you are using REPL types, it will pickup the most approapriate
773773
`inf-clojure-var-source-form` variant."
774774
(inf-clojure--sanitize-command
775-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
775+
(pcase (inf-clojure--set-repl-type proc)
776776
(`lumo inf-clojure-var-source-form-lumo)
777777
(`planck inf-clojure-var-source-form-planck)
778778
(_ inf-clojure-var-source-form))))
@@ -810,12 +810,12 @@ If you are using REPL types, it will pickup the most approapriate
810810
:safe #'stringp
811811
:package-version '(inf-clojure . "2.1.0"))
812812

813-
(defun inf-clojure-arglists-form ()
814-
"Return the form to query inferior Clojure for arglists of a var.
813+
(defun inf-clojure-arglists-form (proc)
814+
"Return the form to query the Inf-Clojure PROC for arglists of a var.
815815
If you are using REPL types, it will pickup the most approapriate
816816
`inf-clojure-arglists-form` variant."
817817
(inf-clojure--sanitize-command
818-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
818+
(pcase (inf-clojure--set-repl-type proc)
819819
(`lumo inf-clojure-arglists-form-lumo)
820820
(`planck inf-clojure-arglists-form-planck)
821821
(_ inf-clojure-arglists-form))))
@@ -846,12 +846,12 @@ If you are using REPL types, it will pickup the most approapriate
846846
:safe #'stringp
847847
:package-version '(inf-clojure . "2.0.0"))
848848

849-
(defun inf-clojure-completion-form ()
850-
"Return the form to query inferior Clojure for a var's documentation.
849+
(defun inf-clojure-completion-form (proc)
850+
"Return the form to query the Inf-Clojure PROC for completions.
851851
If you are using REPL types, it will pickup the most approapriate
852852
`inf-clojure-completion-form` variant."
853853
(inf-clojure--sanitize-command
854-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
854+
(pcase (inf-clojure--set-repl-type proc)
855855
(`lumo inf-clojure-completion-form-lumo)
856856
(`planck inf-clojure-completion-form-planck)
857857
(_ inf-clojure-completion-form))))
@@ -877,12 +877,12 @@ If you are using REPL types, it will pickup the most approapriate
877877
:safe #'stringp
878878
:package-version '(inf-clojure . "2.0.0"))
879879

880-
(defun inf-clojure-ns-vars-form ()
881-
"Return the form to query inferior Clojure for public vars in a namespace.
880+
(defun inf-clojure-ns-vars-form (proc)
881+
"Return the form to query the Inf-Clojure PROC for public vars in a namespace.
882882
If you are using REPL types, it will pickup the most approapriate
883883
`inf-clojure-ns-vars-form` variant."
884884
(inf-clojure--sanitize-command
885-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
885+
(pcase (inf-clojure--set-repl-type proc)
886886
(`lumo inf-clojure-ns-vars-form-lumo)
887887
(`planck inf-clojure-ns-vars-form-planck)
888888
(_ inf-clojure-ns-vars-form))))
@@ -910,11 +910,11 @@ If you are using REPL types, it will pickup the most approapriate
910910
:safe #'stringp
911911
:package-version '(inf-clojure . "2.0.0"))
912912

913-
(defun inf-clojure-set-ns-form ()
914-
"Return the form to set the ns of the inferior Clojure process.
913+
(defun inf-clojure-set-ns-form (proc)
914+
"Return the form to set the namespace of the Inf-Clojure PROC.
915915
If you are using REPL types, it will pickup the most approapriate
916916
`inf-clojure-set-ns-form` variant."
917-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
917+
(pcase (inf-clojure--set-repl-type proc)
918918
(`planck inf-clojure-set-ns-form-planck)
919919
(`lumo inf-clojure-set-ns-form-lumo)
920920
(_ inf-clojure-set-ns-form)))
@@ -944,12 +944,12 @@ If you are using REPL types, it will pickup the most approapriate
944944
:safe #'stringp
945945
:package-version '(inf-clojure . "2.0.0"))
946946

947-
(defun inf-clojure-apropos-form ()
948-
"Return the form to query inferior Clojure for public vars in a namespace.
947+
(defun inf-clojure-apropos-form (proc)
948+
"Return the form to query the Inf-Clojure PROC for a var's apropos.
949949
If you are using REPL types, it will pickup the most approapriate
950-
`inf-clojure-ns-vars-form` variant."
950+
`inf-clojure-apropos-form` variant."
951951
(inf-clojure--sanitize-command
952-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
952+
(pcase (inf-clojure--set-repl-type proc)
953953
(`lumo inf-clojure-apropos-form-lumo)
954954
(`planck inf-clojure-apropos-form-planck)
955955
(_ inf-clojure-apropos-form))))
@@ -970,12 +970,12 @@ If you are using REPL types, it will pickup the most approapriate
970970
:safe #'stringp
971971
:package-version '(inf-clojure . "2.0.0"))
972972

973-
(defun inf-clojure-macroexpand-form ()
974-
"Return the form for macroexpansion in the inferior Clojure process.
973+
(defun inf-clojure-macroexpand-form (proc)
974+
"Return the form for macroexpansion in the Inf-Clojure PROC.
975975
If you are using REPL types, it will pickup the most approapriate
976976
`inf-clojure-macroexpand-form` variant."
977977
(inf-clojure--sanitize-command
978-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
978+
(pcase (inf-clojure--set-repl-type proc)
979979
(`planck inf-clojure-macroexpand-form-planck)
980980
(_ inf-clojure-macroexpand-form))))
981981

@@ -995,12 +995,12 @@ If you are using REPL types, it will pickup the most approapriate
995995
:safe #'stringp
996996
:package-version '(inf-clojure . "2.0.0"))
997997

998-
(defun inf-clojure-macroexpand-1-form ()
999-
"Return the form for macroexpand-1 in the inferior Clojure process.
998+
(defun inf-clojure-macroexpand-1-form (proc)
999+
"Return the form for macroexpand-1 in the Inf-Clojure PROC.
10001000
If you are using REPL types, it will pickup the most approapriate
10011001
`inf-clojure-macroexpand-1-form` variant."
10021002
(inf-clojure--sanitize-command
1003-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
1003+
(pcase (inf-clojure--set-repl-type proc)
10041004
(`planck inf-clojure-macroexpand-1-form-planck)
10051005
(_ inf-clojure-macroexpand-1-form))))
10061006

@@ -1047,20 +1047,22 @@ The value is nil if it can't find one."
10471047
See function `inf-clojure-var-doc-form'. When invoked with a
10481048
prefix argument PROMPT-FOR-SYMBOL, it prompts for a symbol name."
10491049
(interactive "P")
1050-
(let ((var (if prompt-for-symbol
1051-
(car (inf-clojure-symprompt "Var doc" (inf-clojure-symbol-at-point)))
1052-
(inf-clojure-symbol-at-point))))
1053-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-var-doc-form) var))))
1050+
(let ((proc (inf-clojure-proc))
1051+
(var (if prompt-for-symbol
1052+
(car (inf-clojure-symprompt "Var doc" (inf-clojure-symbol-at-point)))
1053+
(inf-clojure-symbol-at-point))))
1054+
(inf-clojure--send-string proc (format (inf-clojure-var-doc-form proc) var))))
10541055

10551056
(defun inf-clojure-show-var-source (prompt-for-symbol)
10561057
"Send a command to the inferior Clojure to give source for VAR.
10571058
See variable `inf-clojure-var-source-form'. When invoked with a
10581059
prefix argument PROMPT-FOR-SYMBOL, it prompts for a symbol name."
10591060
(interactive "P")
1060-
(let ((var (if prompt-for-symbol
1061+
(let ((proc (inf-clojure-proc))
1062+
(var (if prompt-for-symbol
10611063
(car (inf-clojure-symprompt "Var source" (inf-clojure-symbol-at-point)))
10621064
(inf-clojure-symbol-at-point))))
1063-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-var-source-form) var))))
1065+
(inf-clojure--send-string proc (format (inf-clojure-var-source-form proc) var))))
10641066

10651067
;;;; Response parsing
10661068
;;;; ================
@@ -1190,10 +1192,11 @@ for evaluation, therefore FORM should not include it."
11901192
(defun inf-clojure-arglists (fn)
11911193
"Send a query to the inferior Clojure for the arglists for function FN.
11921194
See variable `inf-clojure-arglists-form'."
1193-
(thread-first
1194-
(format (inf-clojure-arglists-form) fn)
1195-
(inf-clojure--process-response (inf-clojure-proc) "(" ")")
1196-
(inf-clojure--some)))
1195+
(when-let ((proc (inf-clojure-proc 'no-error)))
1196+
(thread-first
1197+
(format (inf-clojure-arglists-form proc) fn)
1198+
(inf-clojure--process-response proc "(" ")")
1199+
(inf-clojure--some))))
11971200

11981201
(defun inf-clojure-show-arglists (prompt-for-symbol)
11991202
"Show the arglists for function FN in the mini-buffer.
@@ -1212,52 +1215,56 @@ prefix argument PROMPT-FOR-SYMBOL, it prompts for a symbol name."
12121215
See variable `inf-clojure-ns-vars-form'. When invoked with a
12131216
prefix argument PROMPT-FOR-NS, it prompts for a namespace name."
12141217
(interactive "P")
1215-
(let ((ns (if prompt-for-ns
1216-
(car (inf-clojure-symprompt "Ns vars" (clojure-find-ns)))
1217-
(clojure-find-ns))))
1218-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-ns-vars-form) ns))))
1218+
(let ((proc (inf-clojure-proc))
1219+
(ns (if prompt-for-ns
1220+
(car (inf-clojure-symprompt "Ns vars" (clojure-find-ns)))
1221+
(clojure-find-ns))))
1222+
(inf-clojure--send-string proc (format (inf-clojure-ns-vars-form proc) ns))))
12191223

12201224
(defun inf-clojure-set-ns (prompt-for-ns)
12211225
"Set the ns of the inferior Clojure process to NS.
12221226
See variable `inf-clojure-set-ns-form'. It defaults to the ns of
12231227
the current buffer. When invoked with a prefix argument
12241228
PROMPT-FOR-NS, it prompts for a namespace name."
12251229
(interactive "P")
1226-
(let ((ns (if prompt-for-ns
1230+
(let ((proc (inf-clojure-proc))
1231+
(ns (if prompt-for-ns
12271232
(car (inf-clojure-symprompt "Set ns to" (clojure-find-ns)))
12281233
(clojure-find-ns))))
12291234
(when (or (not ns) (equal ns ""))
12301235
(user-error "No namespace selected"))
1231-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-set-ns-form) ns))))
1236+
(inf-clojure--send-string proc (format (inf-clojure-set-ns-form proc) ns))))
12321237

12331238
(defun inf-clojure-apropos (expr)
12341239
"Send an expression to the inferior Clojure for apropos.
12351240
EXPR can be either a regular expression or a stringable
12361241
thing. See variable `inf-clojure-apropos-form'."
12371242
(interactive (inf-clojure-symprompt "Var apropos" (inf-clojure-symbol-at-point)))
1238-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-apropos-form) expr)))
1243+
(let ((proc (inf-clojure-proc)))
1244+
(inf-clojure--send-string proc (format (inf-clojure-apropos-form proc) expr))))
12391245

12401246
(defun inf-clojure-macroexpand (&optional macro-1)
12411247
"Send a form to the inferior Clojure for macro expansion.
12421248
See variable `inf-clojure-macroexpand-form'.
12431249
With a prefix arg MACRO-1 uses function `inf-clojure-macroexpand-1-form'."
12441250
(interactive "P")
1245-
(let ((last-sexp (buffer-substring-no-properties (save-excursion (backward-sexp) (point)) (point))))
1251+
(let ((proc (inf-clojure-proc))
1252+
(last-sexp (buffer-substring-no-properties (save-excursion (backward-sexp) (point)) (point))))
12461253
(inf-clojure--send-string
1247-
(inf-clojure-proc)
1254+
proc
12481255
(format (if macro-1
1249-
(inf-clojure-macroexpand-1-form)
1250-
(inf-clojure-macroexpand-form))
1256+
(inf-clojure-macroexpand-1-form proc)
1257+
(inf-clojure-macroexpand-form proc))
12511258
last-sexp))))
12521259

1253-
1254-
(defun inf-clojure-proc ()
1260+
(defun inf-clojure-proc (&optional no-error)
12551261
"Return the current inferior Clojure process.
1256-
See variable `inf-clojure-buffer'."
1257-
(let ((proc (get-buffer-process (if (derived-mode-p 'inf-clojure-mode)
1258-
(current-buffer)
1259-
inf-clojure-buffer))))
1260-
(or proc
1262+
When NO-ERROR is non-nil, don't throw an error when no connection
1263+
has been found. See also variable `inf-clojure-buffer'."
1264+
(or (get-buffer-process (if (derived-mode-p 'inf-clojure-mode)
1265+
(current-buffer)
1266+
inf-clojure-buffer))
1267+
(unless no-error
12611268
(error "No Clojure subprocess; see variable `inf-clojure-buffer'"))))
12621269

12631270
(defun inf-clojure--list-or-nil (data)
@@ -1280,11 +1287,11 @@ every other EXPR will be discarded and nil will be returned."
12801287
Under the hood it calls the function
12811288
\\[inf-clojure-completions-fn] passing in the result of
12821289
evaluating \\[inf-clojure-completion-form] at the REPL."
1283-
(when (not (string-blank-p expr))
1284-
(let ((proc (inf-clojure-proc))
1285-
(completion-form (format (inf-clojure-completion-form) (substring-no-properties expr))))
1286-
(funcall inf-clojure-completions-fn
1287-
(inf-clojure--process-response completion-form proc "(" ")")))))
1290+
(let ((proc (inf-clojure-proc 'no-error)))
1291+
(when (and proc (not (string-blank-p expr)))
1292+
(let ((completion-form (format (inf-clojure-completion-form proc) (substring-no-properties expr))))
1293+
(funcall inf-clojure-completions-fn
1294+
(inf-clojure--process-response completion-form proc "(" ")"))))))
12881295

12891296
(defcustom inf-clojure-completions-fn 'inf-clojure-list-completions
12901297
"The function that parses completion results.

0 commit comments

Comments
 (0)