Skip to content

Commit f3962e9

Browse files
committed
Introduce inf-clojure-completions-fn defcustom
Now the user can provide a custom parsing function for completion and therefore has complete (pun intended) freedom in what to use for it. Some could use compliment completion for instance or even use directly what cider provides. The defcustom defaults to inf-clojure-list-completions, which can only parse candidates coming as a Lisp list of strings.
1 parent ae43e2e commit f3962e9

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

inf-clojure.el

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ prefix argument PROMPT-FOR-NS, it prompts for a namespace name."
12101210

12111211
(defun inf-clojure-set-ns (prompt-for-ns)
12121212
"Set the ns of the inferior Clojure process to NS.
1213-
See variable `inf-clojure-set-ns-form`. It defaults to the ns of
1213+
See variable `inf-clojure-set-ns-form'. It defaults to the ns of
12141214
the current buffer. When invoked with a prefix argument
12151215
PROMPT-FOR-NS, it prompts for a namespace name."
12161216
(interactive "P")
@@ -1254,14 +1254,52 @@ See variable `inf-clojure-buffer'."
12541254
"Return DATA if and only if it is a list."
12551255
(when (listp data) data))
12561256

1257+
(defun inf-clojure-list-completions (response-str)
1258+
"Parse completions from RESPONSE-STR.
1259+
1260+
Its only ability is to parse a Lisp list of candidate strings,
1261+
every other EXPR will be discarded and nil will be returned."
1262+
(thread-first
1263+
response-str
1264+
(inf-clojure--read-or-nil)
1265+
(inf-clojure--list-or-nil)))
1266+
12571267
(defun inf-clojure-completions (expr)
1258-
"Return a list of completions for the Clojure expression starting with EXPR."
1268+
"Return completions for the Clojure expression starting with EXPR.
1269+
1270+
Under the hood it calls the function
1271+
\\[inf-clojure-completions-fn] passing in the result of
1272+
evaluating \\[inf-clojure-completion-form] at the REPL."
12591273
(when (not (string-blank-p expr))
1260-
(thread-first
1261-
(format (inf-clojure-completion-form) (substring-no-properties expr))
1262-
(inf-clojure--process-response (inf-clojure-proc) "(" ")")
1263-
(inf-clojure--read-or-nil)
1264-
(inf-clojure--list-or-nil))))
1274+
(let ((proc (inf-clojure-proc))
1275+
(completion-form (format (inf-clojure-completion-form) (substring-no-properties expr))))
1276+
(funcall inf-clojure-completions-fn
1277+
(inf-clojure--process-response completion-form proc "(" ")")))))
1278+
1279+
(defcustom inf-clojure-completions-fn 'inf-clojure-list-completions
1280+
"The function that parses completion results.
1281+
1282+
It is a single-arity function that will receive the REPL
1283+
evaluation result of \\[inf-clojure-completion-form] as string and
1284+
should return elisp data compatible with your completion mode.
1285+
1286+
The easiest possible data passed in input is a list of
1287+
candidates (e.g.: (\"def\" \"defn\")) but more complex libraries
1288+
like `alexander-yakushev/compliment' can return other things like
1289+
edn.
1290+
1291+
The expected return depends on the mode that you use for
1292+
completion: `company-mode' for instance allows metadata along
1293+
with the candidates completion. See \\[completion-table-dynamic]
1294+
for more details.
1295+
1296+
The default value is the `inf-clojure-list-completions' function,
1297+
which is able to parse results in list form only. You can peek
1298+
at its implementation for getting to know some utility functions
1299+
you might want to use in your customization."
1300+
:type 'function
1301+
:safe #'functionp
1302+
:package-version '(inf-clojure . "2.1.0"))
12651303

12661304
(defconst inf-clojure-clojure-expr-break-chars " \t\n\"\'`><,;|&{()[]")
12671305

0 commit comments

Comments
 (0)