Subject: bug#72765: Eglot + Clangd + Company + non-empty suffix = duplicate text

From: Dmitry Gutov <[email-masked]>
Date: Sun, 1 Sep 2024 04:43:06 +0300

On 31/08/2024 15:03, João Távora wrote:

> Eglot aims primarily at that, since it's what's in Emacs proper. But
> Eglot also aims at supporting Company in particular as fully
> as possible.
> 
> Anyway, I don't have time to investigate this. The :exit-function in
> eglot.el should be stepped through to understand exactly who's at
> fault. And I don't think differences between servers matter:
> clangd is likely following the spec correctly here.

It seems the difference comes from bug#70968 as well (which came up 
recently).

When the completion style emacs22 is used, Company doesn't delete the 
suffix text before inserting completion. Which is an improvement for 
some other completion sources, but not Eglot, so far.

To to fix this here, we can avoid a fail-over to emacs22 by only 
matching the prefix in eglot--dumb-allc like this:

diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el
index 59d9c346424..20c15584d2d 100644
--- a/lisp/progmodes/eglot.el
+++ b/lisp/progmodes/eglot.el
@@ -3138,7 +3138,9 @@ eglot--dumb-flex
                                        nil comp)
             finally (cl-return comp)))

-(defun eglot--dumb-allc (pat table pred _point) (funcall table pat pred t))
+(defun eglot--dumb-allc (pat table pred point)
+  (funcall table (substring pat 0 point) pred t))
+
  (defun eglot--dumb-tryc (pat table pred point)
    (let ((probe (funcall table pat pred nil)))
      (cond ((eq probe t) t)

That fixes the scenario in Company, with seemingly no change with 
completion-at-point. Or if we want 100% compatibility, we can use 'or':

   (or
    (funcall table pat pred t)
    (funcall table (substring pat 0 point) pred t))

But in any case this doesn't help with the completion-at-point behavior 
described at the end of the report (where foo_|bar_2 turns into 
foo_bar_2bar_2|). If we consider it okay - then the above patch fixes 
the discrepancy with Company completion, and done. But if we think it a 
problem, then the fix might be required somewhere in the area of

                  (cond (textEdit
                         ;; Revert buffer back to state when the edit
                         ;; was obtained from server. If a `proxy'

After (and if) that is done, we might not need to change the completion 
style in the end.