Skip to content

Commit c4adc89

Browse files
arichiardibbatsov
authored andcommitted
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 ca96cfc commit c4adc89

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

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

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

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@ following to you Emacs config:
214214
ElDoc currently doesn't work with ClojureScript buffers and REPL's.
215215
You can leave it enabled, it just won't show anything in the echo area.
216216

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

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

inf-clojure.el

+45-7
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ prefix argument PROMPT-FOR-NS, it prompts for a namespace name."
12111211

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

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

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

0 commit comments

Comments
 (0)