Skip to content

Commit 8d22393

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 8d22393

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
* [#114](https://github.com/clojure-emacs/inf-clojure/pull/114): Introduce `inf-clojure-project-type` defcustom.
1616
* [#117](https://github.com/clojure-emacs/inf-clojure/pull/117): Introduce `tools.deps` project type and `inf-clojure-tools-deps-cmd`.
17+
* [#122](https://github.com/clojure-emacs/inf-clojure/pull/122): Introduce `inf-clojure-completions-fn` defcustom.
1718

1819
## 2.0.1 (2017-05-18)
1920

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ following to you Emacs config:
211211
ElDoc currently doesn't work with ClojureScript buffers and REPL's.
212212
You can leave it enabled, it just won't show anything in the echo area.
213213

214+
#### Code Completion
215+
216+
Code completion is particularly open to customization. Not only you can `setq`
217+
the customary `inf-clojure-completion-form`, `inf-clojure-completion-form-lumo`
218+
and `inf-clojure-completion-form-planck` - the form to send to the REPL - but
219+
you can also use `inf-clojure-completions-fn` for specifying a function that
220+
given the REPL response should return elisp data compatible with
221+
[`completion-at-point-functions`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion-in-Buffers.html).
222+
For more info run `M-x describe-variable RET inf-clojure-completions-fn`.
223+
Another option is to have a look at
224+
[how cider does it](https://github.com/clojure-emacs/cider/blob/3e9ed12e8cfbad04d7618e649322765dc9bff5d6/cider-interaction.el#L595).
225+
214226
#### Lumo Setup
215227

216228
For an optimal Lumo experience the `-d` needs to be passed to Lumo

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: usually it is something compatible with
1293+
\\[completion-at-point-functions] but other modes like
1294+
`company-mode' allow an even higher level of sophistication.
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)