Skip to content

Commit

Permalink
[completion] Fix custom completion style and make it default for fuzz…
Browse files Browse the repository at this point in the history
…y matching
  • Loading branch information
alexander-yakushev authored and bbatsov committed Oct 20, 2024
1 parent e51ebbc commit ced9f25
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Changes

- [#3746](https://github.com/clojure-emacs/cider/issues/3746): Bring back `cider` completion style for activating backend-driven completion.

### Bugs fixed

- [#3742](https://github.com/clojure-emacs/cider/issues/3742): Restore syntax highlighting in result minibuffer.
Expand Down
41 changes: 36 additions & 5 deletions cider-completion.el
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ in the buffer."

;; Fuzzy completion for company-mode

(defun cider-completion-try-completion (string collection pred _)
"Return longest common substring of all completions of STRING in COLLECTION,
also pass PRED to `try-completion'.
This function is only needed to be a correct citizen in
`completion-styles-alist'."
(try-completion string collection pred))

(defun cider-company-unfiltered-candidates (string &rest _)
"Return CIDER completion candidates for STRING as is, unfiltered."
(cider-complete string))
Expand All @@ -268,17 +276,40 @@ in the buffer."
;; which introduced `cider-company-enable-fuzzy-completion')
(add-to-list 'completion-styles-alist
'(cider
cider-company-unfiltered-candidates
cider-completion-try-completion
cider-company-unfiltered-candidates
"CIDER backend-driven completion style."))

(defun cider-company-enable-fuzzy-completion ()
"Enable backend-driven fuzzy completion in the current buffer.
"Enables `cider' completion style for CIDER in all buffers.
DEPRECATED: please use `cider-enable-flex-completion' instead."
(setq-local completion-styles '(cider)))
DEPRECATED: please use `cider-enable-cider-completion-style' instead."
(interactive)
(cider-enable-cider-completion-style))

(defun cider-enable-cider-completion-style ()
"Enables `cider' completion style for CIDER in all buffers.
(make-obsolete 'cider-company-enable-fuzzy-completion 'cider-enable-flex-completion "1.8.0")
This style supports non-prefix completion candidates returned by the
completion backend. Only affects the `cider' completion category."
(interactive)
(let* ((cider (assq 'cider completion-category-overrides))
(found-styles (assq 'styles cider))
(new-styles (if found-styles
(cons 'styles (cons 'cider (cdr found-styles)))
'(styles cider basic)))
(new-cider (if cider
(cons 'cider
(cons new-styles
(seq-remove (lambda (x) (equal 'styles (car x)))
(cdr cider))))
(list 'cider new-styles)))
(new-overrides (cons new-cider
(seq-remove (lambda (x) (equal 'cider (car x)))
completion-category-overrides))))
(setq completion-category-overrides new-overrides)))

(make-obsolete 'cider-company-enable-fuzzy-completion 'cider-enable-cider-completion-style "1.17.0")

(defun cider-enable-flex-completion ()
"Enables `flex' (fuzzy) completion for CIDER in all buffers.
Expand Down
19 changes: 9 additions & 10 deletions doc/modules/ROOT/pages/usage/code_completion.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ CIDER defines a specialized completion category through the `cider-complete-at-p
added to `completion-at-point-functions`, establishing a dedicated completion category named
`cider`.

The CIDER completion at point function supports most completion styles, including
`partial-completion`, `orderless` and `flex` (read more below).

The CIDER completion at point function supports most completion styles,
including `partial-completion`, `orderless` and `flex`. It also supports a
custom completion style that is confusingly named `cider` too. Activating it
provides a richer set of completion candidates (see
xref:usage/code_completion.adoc#fuzzy-candidate-matching[fuzzy candidate
matching]).

Sometimes the user may want to use a different completion style just for the CIDER
complete at point function. That can be achieved by setting
Expand All @@ -58,8 +61,6 @@ complete at point function. The following snippet accomplishes that:
This specifies that the `cider` completion category should employ the basic completion style by
default.

You can also enable the `flex` completion style by activating xref:usage/code_completion.adoc#fuzzy-candidate-matching[fuzzy candidate matching].

== Auto-completion

While the standard Emacs tooling works just fine, we suggest that
Expand Down Expand Up @@ -146,15 +147,15 @@ emacs22)` since Emacs 23. For a better description of how those
completion styles operates, refer to the official Emacs manual on
https://www.gnu.org/software/emacs/manual/html_node/emacs/Completion-Styles.html[how completion alternatives are chosen].

CIDER provides a function to enable the `flex` completion style for CIDER-specific
CIDER provides a function to enable the `cider` completion style for CIDER-specific
completions. If you wish to enable that, you can add this to your config:

[source,lisp]
----
(cider-enable-flex-completion)
(cider-enable-cider-completion-style)
----

This adds the `flex` completion style, as introduced in Emacs 27.
This adds the `cider` completion style for CIDER buffers.

Now, `company-mode` (and other completion packages like `corfu`) will
accept certain fuzziness when matching candidates against the
Expand All @@ -163,8 +164,6 @@ the possible completion candidates and `cji` will complete to
`clojure.java.io`. Different completion examples are shown
https://github.com/alexander-yakushev/compliment/wiki/Examples[here].

NOTE: `cider-company-enable-fuzzy-completion` (now deprecated) should be used for Emacs < 27.

=== Completion annotations

Completion candidates will be annotated by default with an abbreviation
Expand Down

0 comments on commit ced9f25

Please sign in to comment.