Subject: bug#68699: Eglot completion has stopped working in some cases

From: João Távora <[email-masked]>
Date: Thu, 25 Jan 2024 01:35:08 +0000

On Thu, Jan 25, 2024 at 12:43 AM João Távora <[email-masked]> wrote:
>
> On Wed, Jan 24, 2024 at 8:37 PM Brennan Vincent <[email-masked]> wrote:
>
> > For example, in a Rust crate with the following main file:
> >
> > fn foo_len() {
> > }
> >
> > fn main() {
> >     len
> > }
>
> I've reproduced this with latest master:
>
> ~/Source/Emacs/emacs/src/emacs -Q ~/MoreSource/Rust/hello/src/main.rs
> -f rust-ts-mode -f eglot
>
> where main.rs is the provided snippet.
>
> Pressing C-M-i with point after 'len' yields the error.
>
> The error only happens in partial-completion is in the completion-styles
> variable, so a workaround is to remove partial-completion from
> completion-styles, at least in Eglot enabled buffers.
>
> I will look now for the underlying cause.

Stefan, this is down to partial-completion somehow thinking
that "\\'len" should match at least some LSP-provided
completion candidate, otherwise it's an "internal error"

I wish I could turn off all the styles from the table but I can
only add to them.  I simply cannot write a reasonable
try-completion thing for LSP completions.  LSP completions
are really designed to be navigated using a company-like
popup.  Without it they should almost always use the *Completions*
buffer, and rarely ever Emacs partial completion shenanigans.

Nevertheless, I need to plug this bug somehow in Eglot.

Brennan, can you try this patch I've just pushed?

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 511000927cf..c5fbf5eb9d5 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -3060,9 +3060,13 @@ eglot--dumb-flex
            finally (cl-return comp)))

 (defun eglot--dumb-allc (pat table pred _point) (funcall table pat pred t))
+(defun eglot--dumb-tryc (pat table pred point)
+  (if-let ((probe (funcall table pat pred nil)))
+      (cons probe (length probe))
+    (cons pat point)))

 (add-to-list 'completion-category-defaults '(eglot-capf (styles
eglot--dumb-flex)))
-(add-to-list 'completion-styles-alist '(eglot--dumb-flex ignore
eglot--dumb-allc))
+(add-to-list 'completion-styles-alist '(eglot--dumb-flex
eglot--dumb-tryc eglot--dumb-allc))

 (defun eglot-completion-at-point ()
   "Eglot's `completion-at-point' function."

João