Skip to content

Introduce inf-clojure-completions-fn defcustom #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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

## 2.0.1 (2017-05-18)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ following to you Emacs config:
ElDoc currently doesn't work with ClojureScript buffers and REPL's.
You can leave it enabled, it just won't show anything in the echo area.

#### Code Completion

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

#### Lumo Setup

For an optimal Lumo experience the `-d` needs to be passed to Lumo
Expand Down
52 changes: 45 additions & 7 deletions inf-clojure.el
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ prefix argument PROMPT-FOR-NS, it prompts for a namespace name."

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

(defun inf-clojure-list-completions (response-str)
"Parse completions from RESPONSE-STR.

Its only ability is to parse a Lisp list of candidate strings,
every other EXPR will be discarded and nil will be returned."
(thread-first
response-str
(inf-clojure--read-or-nil)
(inf-clojure--list-or-nil)))

(defun inf-clojure-completions (expr)
"Return a list of completions for the Clojure expression starting with EXPR."
"Return completions for the Clojure expression starting with EXPR.

Under the hood it calls the function
\\[inf-clojure-completions-fn] passing in the result of
evaluating \\[inf-clojure-completion-form] at the REPL."
(when (not (string-blank-p expr))
(thread-first
(format (inf-clojure-completion-form) (substring-no-properties expr))
(inf-clojure--process-response (inf-clojure-proc) "(" ")")
(inf-clojure--read-or-nil)
(inf-clojure--list-or-nil))))
(let ((proc (inf-clojure-proc))
(completion-form (format (inf-clojure-completion-form) (substring-no-properties expr))))
(funcall inf-clojure-completions-fn
(inf-clojure--process-response completion-form proc "(" ")")))))

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

It is a single-arity function that will receive the REPL
evaluation result of \\[inf-clojure-completion-form] as string and
should return elisp data compatible with your completion mode.

The easiest possible data passed in input is a list of
candidates (e.g.: (\"def\" \"defn\")) but more complex libraries
like `alexander-yakushev/compliment' can return other things like
edn.

The expected return depends on the mode that you use for
completion: usually it is something compatible with
\\[completion-at-point-functions] but other modes like
`company-mode' allow an even higher level of sophistication.

The default value is the `inf-clojure-list-completions' function,
which is able to parse results in list form only. You can peek
at its implementation for getting to know some utility functions
you might want to use in your customization."
:type 'function
:safe #'functionp
:package-version '(inf-clojure . "2.1.0"))

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

Expand Down