Skip to content

Commit 468d6cd

Browse files
committed
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 247ca70 commit 468d6cd

File tree

2 files changed

+74
-67
lines changed

2 files changed

+74
-67
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [#114](https://github.com/clojure-emacs/inf-clojure/pull/114): Introduce `inf-clojure-project-type` defcustom.
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.
12+
* [#125](https://github.com/clojure-emacs/inf-clojure/pull/125): Avoid throwing an error for frequent operations like completion.
1213

1314
### Bugs Fixed
1415

inf-clojure.el

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

364-
(defun inf-clojure-load-form ()
365-
"Return the form to query inferior Clojure for a var's documentation.
364+
(defun inf-clojure-load-form (proc)
365+
"Return the form to query the Inf-Clojure PROC for var's documentation.
366366
If you are using REPL types, it will pickup the most appropriate
367367
`inf-clojure-var-doc-form` variant."
368-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
368+
(pcase (inf-clojure--set-repl-type proc)
369369
(`lumo inf-clojure-load-form-lumo)
370370
(`planck inf-clojure-load-form-planck)
371371
(_ inf-clojure-load-form)))
@@ -684,15 +684,15 @@ Used by this command to determine defaults."
684684
685685
The prefix argument SWITCH-TO-REPL controls whether to switch to REPL after the file is loaded or not."
686686
(interactive "P")
687-
(let ((file-name (or file-name
687+
(let ((proc (inf-clojure-proc))
688+
(file-name (or file-name
688689
(car (comint-get-source "Load Clojure file: " inf-clojure-prev-l/c-dir/file
689690
;; nil because doesn't need an exact name
690691
inf-clojure-source-modes nil)))))
691692
(comint-check-source file-name) ; Check to see if buffer needs saved.
692693
(setq inf-clojure-prev-l/c-dir/file (cons (file-name-directory file-name)
693694
(file-name-nondirectory file-name)))
694-
(inf-clojure--send-string (inf-clojure-proc)
695-
(format (inf-clojure-load-form) file-name))
695+
(inf-clojure--send-string proc (format (inf-clojure-load-form proc) file-name))
696696
(when switch-to-repl
697697
(inf-clojure-switch-to-repl t))))
698698

@@ -731,12 +731,12 @@ The prefix argument SWITCH-TO-REPL controls whether to switch to REPL after the
731731
:safe #'stringp
732732
:package-version '(inf-clojure . "2.0.0"))
733733

734-
(defun inf-clojure-var-doc-form ()
735-
"Return the form to query inferior Clojure for a var's documentation.
734+
(defun inf-clojure-var-doc-form (proc)
735+
"Return the form to query the Inf-Clojure PROC for a var's documentation.
736736
If you are using REPL types, it will pickup the most approapriate
737737
`inf-clojure-var-doc-form` variant."
738738
(inf-clojure--sanitize-command
739-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
739+
(pcase (inf-clojure--set-repl-type proc)
740740
(`lumo inf-clojure-var-doc-form-lumo)
741741
(`planck inf-clojure-var-doc-form-planck)
742742
(_ inf-clojure-var-doc-form))))
@@ -762,12 +762,12 @@ If you are using REPL types, it will pickup the most approapriate
762762
:safe #'stringp
763763
:package-version '(inf-clojure . "2.0.0"))
764764

765-
(defun inf-clojure-var-source-form ()
766-
"Return the form to query inferior Clojure for a var's source.
765+
(defun inf-clojure-var-source-form (proc)
766+
"Return the form to query the Inf-Clojure PROC for a var's source.
767767
If you are using REPL types, it will pickup the most approapriate
768768
`inf-clojure-var-source-form` variant."
769769
(inf-clojure--sanitize-command
770-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
770+
(pcase (inf-clojure--set-repl-type proc)
771771
(`lumo inf-clojure-var-source-form-lumo)
772772
(`planck inf-clojure-var-source-form-planck)
773773
(_ inf-clojure-var-source-form))))
@@ -805,12 +805,12 @@ If you are using REPL types, it will pickup the most approapriate
805805
:safe #'stringp
806806
:package-version '(inf-clojure . "2.1.0"))
807807

808-
(defun inf-clojure-arglists-form ()
809-
"Return the form to query inferior Clojure for arglists of a var.
808+
(defun inf-clojure-arglists-form (proc)
809+
"Return the form to query the Inf-Clojure PROC for arglists of a var.
810810
If you are using REPL types, it will pickup the most approapriate
811811
`inf-clojure-arglists-form` variant."
812812
(inf-clojure--sanitize-command
813-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
813+
(pcase (inf-clojure--set-repl-type proc)
814814
(`lumo inf-clojure-arglists-form-lumo)
815815
(`planck inf-clojure-arglists-form-planck)
816816
(_ inf-clojure-arglists-form))))
@@ -841,12 +841,12 @@ If you are using REPL types, it will pickup the most approapriate
841841
:safe #'stringp
842842
:package-version '(inf-clojure . "2.0.0"))
843843

844-
(defun inf-clojure-completion-form ()
845-
"Return the form to query inferior Clojure for a var's documentation.
844+
(defun inf-clojure-completion-form (proc)
845+
"Return the form to query the Inf-Clojure PROC for completions.
846846
If you are using REPL types, it will pickup the most approapriate
847847
`inf-clojure-completion-form` variant."
848848
(inf-clojure--sanitize-command
849-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
849+
(pcase (inf-clojure--set-repl-type proc)
850850
(`lumo inf-clojure-completion-form-lumo)
851851
(`planck inf-clojure-completion-form-planck)
852852
(_ inf-clojure-completion-form))))
@@ -872,12 +872,12 @@ If you are using REPL types, it will pickup the most approapriate
872872
:safe #'stringp
873873
:package-version '(inf-clojure . "2.0.0"))
874874

875-
(defun inf-clojure-ns-vars-form ()
876-
"Return the form to query inferior Clojure for public vars in a namespace.
875+
(defun inf-clojure-ns-vars-form (proc)
876+
"Return the form to query the Inf-Clojure PROC for public vars in a namespace.
877877
If you are using REPL types, it will pickup the most approapriate
878878
`inf-clojure-ns-vars-form` variant."
879879
(inf-clojure--sanitize-command
880-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
880+
(pcase (inf-clojure--set-repl-type proc)
881881
(`lumo inf-clojure-ns-vars-form-lumo)
882882
(`planck inf-clojure-ns-vars-form-planck)
883883
(_ inf-clojure-ns-vars-form))))
@@ -905,11 +905,11 @@ If you are using REPL types, it will pickup the most approapriate
905905
:safe #'stringp
906906
:package-version '(inf-clojure . "2.0.0"))
907907

908-
(defun inf-clojure-set-ns-form ()
909-
"Return the form to set the ns of the inferior Clojure process.
908+
(defun inf-clojure-set-ns-form (proc)
909+
"Return the form to set the namespace of the Inf-Clojure PROC.
910910
If you are using REPL types, it will pickup the most approapriate
911911
`inf-clojure-set-ns-form` variant."
912-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
912+
(pcase (inf-clojure--set-repl-type proc)
913913
(`planck inf-clojure-set-ns-form-planck)
914914
(`lumo inf-clojure-set-ns-form-lumo)
915915
(_ inf-clojure-set-ns-form)))
@@ -939,12 +939,12 @@ If you are using REPL types, it will pickup the most approapriate
939939
:safe #'stringp
940940
:package-version '(inf-clojure . "2.0.0"))
941941

942-
(defun inf-clojure-apropos-form ()
943-
"Return the form to query inferior Clojure for public vars in a namespace.
942+
(defun inf-clojure-apropos-form (proc)
943+
"Return the form to query the Inf-Clojure PROC for a var's apropos.
944944
If you are using REPL types, it will pickup the most approapriate
945-
`inf-clojure-ns-vars-form` variant."
945+
`inf-clojure-apropos-form` variant."
946946
(inf-clojure--sanitize-command
947-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
947+
(pcase (inf-clojure--set-repl-type proc)
948948
(`lumo inf-clojure-apropos-form-lumo)
949949
(`planck inf-clojure-apropos-form-planck)
950950
(_ inf-clojure-apropos-form))))
@@ -965,12 +965,12 @@ If you are using REPL types, it will pickup the most approapriate
965965
:safe #'stringp
966966
:package-version '(inf-clojure . "2.0.0"))
967967

968-
(defun inf-clojure-macroexpand-form ()
969-
"Return the form for macroexpansion in the inferior Clojure process.
968+
(defun inf-clojure-macroexpand-form (proc)
969+
"Return the form for macroexpansion in the Inf-Clojure PROC.
970970
If you are using REPL types, it will pickup the most approapriate
971971
`inf-clojure-macroexpand-form` variant."
972972
(inf-clojure--sanitize-command
973-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
973+
(pcase (inf-clojure--set-repl-type proc)
974974
(`planck inf-clojure-macroexpand-form-planck)
975975
(_ inf-clojure-macroexpand-form))))
976976

@@ -990,12 +990,12 @@ If you are using REPL types, it will pickup the most approapriate
990990
:safe #'stringp
991991
:package-version '(inf-clojure . "2.0.0"))
992992

993-
(defun inf-clojure-macroexpand-1-form ()
994-
"Return the form for macroexpand-1 in the inferior Clojure process.
993+
(defun inf-clojure-macroexpand-1-form (proc)
994+
"Return the form for macroexpand-1 in the Inf-Clojure PROC.
995995
If you are using REPL types, it will pickup the most approapriate
996996
`inf-clojure-macroexpand-1-form` variant."
997997
(inf-clojure--sanitize-command
998-
(pcase (inf-clojure--set-repl-type (inf-clojure-proc))
998+
(pcase (inf-clojure--set-repl-type proc)
999999
(`planck inf-clojure-macroexpand-1-form-planck)
10001000
(_ inf-clojure-macroexpand-1-form))))
10011001

@@ -1039,20 +1039,22 @@ The value is nil if it can't find one."
10391039
See function `inf-clojure-var-doc-form'. When invoked with a
10401040
prefix argument PROMPT-FOR-SYMBOL, it prompts for a symbol name."
10411041
(interactive "P")
1042-
(let ((var (if prompt-for-symbol
1043-
(car (inf-clojure-symprompt "Var doc" (inf-clojure-symbol-at-point)))
1044-
(inf-clojure-symbol-at-point))))
1045-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-var-doc-form) var))))
1042+
(let ((proc (inf-clojure-proc))
1043+
(var (if prompt-for-symbol
1044+
(car (inf-clojure-symprompt "Var doc" (inf-clojure-symbol-at-point)))
1045+
(inf-clojure-symbol-at-point))))
1046+
(inf-clojure--send-string proc (format (inf-clojure-var-doc-form proc) var))))
10461047

10471048
(defun inf-clojure-show-var-source (prompt-for-symbol)
10481049
"Send a command to the inferior Clojure to give source for VAR.
10491050
See variable `inf-clojure-var-source-form'. When invoked with a
10501051
prefix argument PROMPT-FOR-SYMBOL, it prompts for a symbol name."
10511052
(interactive "P")
1052-
(let ((var (if prompt-for-symbol
1053+
(let ((proc (inf-clojure-proc))
1054+
(var (if prompt-for-symbol
10531055
(car (inf-clojure-symprompt "Var source" (inf-clojure-symbol-at-point)))
10541056
(inf-clojure-symbol-at-point))))
1055-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-var-source-form) var))))
1057+
(inf-clojure--send-string proc (format (inf-clojure-var-source-form proc) var))))
10561058

10571059
;;;; Response parsing
10581060
;;;; ================
@@ -1182,10 +1184,11 @@ for evaluation, therefore FORM should not include it."
11821184
(defun inf-clojure-arglists (fn)
11831185
"Send a query to the inferior Clojure for the arglists for function FN.
11841186
See variable `inf-clojure-arglists-form'."
1185-
(thread-first
1186-
(format (inf-clojure-arglists-form) fn)
1187-
(inf-clojure--process-response (inf-clojure-proc) "(" ")")
1188-
(inf-clojure--some)))
1187+
(when-let ((proc (inf-clojure-proc 'no-error)))
1188+
(thread-first
1189+
(format (inf-clojure-arglists-form proc) fn)
1190+
(inf-clojure--process-response proc "(" ")")
1191+
(inf-clojure--some))))
11891192

11901193
(defun inf-clojure-show-arglists (prompt-for-symbol)
11911194
"Show the arglists for function FN in the mini-buffer.
@@ -1204,51 +1207,54 @@ prefix argument PROMPT-FOR-SYMBOL, it prompts for a symbol name."
12041207
See variable `inf-clojure-ns-vars-form'. When invoked with a
12051208
prefix argument PROMPT-FOR-NS, it prompts for a namespace name."
12061209
(interactive "P")
1207-
(let ((ns (if prompt-for-ns
1208-
(car (inf-clojure-symprompt "Ns vars" (clojure-find-ns)))
1209-
(clojure-find-ns))))
1210-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-ns-vars-form) ns))))
1210+
(let ((proc (inf-clojure-proc))
1211+
(ns (if prompt-for-ns
1212+
(car (inf-clojure-symprompt "Ns vars" (clojure-find-ns)))
1213+
(clojure-find-ns))))
1214+
(inf-clojure--send-string proc (format (inf-clojure-ns-vars-form proc) ns))))
12111215

12121216
(defun inf-clojure-set-ns (prompt-for-ns)
12131217
"Set the ns of the inferior Clojure process to NS.
12141218
See variable `inf-clojure-set-ns-form'. It defaults to the ns of
12151219
the current buffer. When invoked with a prefix argument
12161220
PROMPT-FOR-NS, it prompts for a namespace name."
12171221
(interactive "P")
1218-
(let ((ns (if prompt-for-ns
1222+
(let ((proc (inf-clojure-proc))
1223+
(ns (if prompt-for-ns
12191224
(car (inf-clojure-symprompt "Set ns to" (clojure-find-ns)))
12201225
(clojure-find-ns))))
12211226
(when (or (not ns) (equal ns ""))
12221227
(user-error "No namespace selected"))
1223-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-set-ns-form) ns))))
1228+
(inf-clojure--send-string proc (format (inf-clojure-set-ns-form proc) ns))))
12241229

12251230
(defun inf-clojure-apropos (var)
12261231
"Send a form to the inferior Clojure to give apropos for VAR.
12271232
See variable `inf-clojure-apropos-form'."
12281233
(interactive (inf-clojure-symprompt "Var apropos" (inf-clojure-symbol-at-point)))
1229-
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-apropos-form) var)))
1234+
(inf-clojure--send-string (inf-clojure-proc) (format (inf-clojure-apropos-form proc) var)))
12301235

12311236
(defun inf-clojure-macroexpand (&optional macro-1)
12321237
"Send a form to the inferior Clojure to give apropos for VAR.
12331238
See variable `inf-clojure-macroexpand-form'.
12341239
With a prefix arg MACRO-1 uses `inf-clojure-macroexpand-1-form'."
12351240
(interactive "P")
1236-
(let ((last-sexp (buffer-substring-no-properties (save-excursion (backward-sexp) (point)) (point))))
1241+
(let ((proc (inf-clojure-proc))
1242+
(last-sexp (buffer-substring-no-properties (save-excursion (backward-sexp) (point)) (point))))
12371243
(inf-clojure--send-string
1238-
(inf-clojure-proc)
1244+
proc
12391245
(format (if macro-1
1240-
(inf-clojure-macroexpand-1-form)
1241-
(inf-clojure-macroexpand-form))
1246+
(inf-clojure-macroexpand-1-form proc)
1247+
(inf-clojure-macroexpand-form proc))
12421248
last-sexp))))
12431249

1244-
1245-
(defun inf-clojure-proc ()
1250+
(defun inf-clojure-proc (&optional no-error)
12461251
"Return the current inferior Clojure process.
1247-
See variable `inf-clojure-buffer'."
1248-
(let ((proc (get-buffer-process (if (derived-mode-p 'inf-clojure-mode)
1249-
(current-buffer)
1250-
inf-clojure-buffer))))
1251-
(or proc
1252+
When NO-ERROR is non-nil, don't throw an error when no connection
1253+
has been found. See also variable `inf-clojure-buffer'."
1254+
(or (get-buffer-process (if (derived-mode-p 'inf-clojure-mode)
1255+
(current-buffer)
1256+
inf-clojure-buffer))
1257+
(unless no-error
12521258
(error "No Clojure subprocess; see variable `inf-clojure-buffer'"))))
12531259

12541260
(defun inf-clojure--list-or-nil (data)
@@ -1271,11 +1277,11 @@ every other EXPR will be discarded and nil will be returned."
12711277
Under the hood it calls the function
12721278
\\[inf-clojure-completions-fn] passing in the result of
12731279
evaluating \\[inf-clojure-completion-form] at the REPL."
1274-
(when (not (string-blank-p expr))
1275-
(let ((proc (inf-clojure-proc))
1276-
(completion-form (format (inf-clojure-completion-form) (substring-no-properties expr))))
1277-
(funcall inf-clojure-completions-fn
1278-
(inf-clojure--process-response completion-form proc "(" ")")))))
1280+
(let ((proc (inf-clojure-proc 'no-error)))
1281+
(when (and proc (not (string-blank-p expr)))
1282+
(let ((completion-form (format (inf-clojure-completion-form proc) (substring-no-properties expr))))
1283+
(funcall inf-clojure-completions-fn
1284+
(inf-clojure--process-response completion-form proc "(" ")"))))))
12791285

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

0 commit comments

Comments
 (0)