Subject: bug#67514: 30.0.50; completion preview symbol length calculation should use point

From: Eshel Yaron <[email-masked]>
Date: Wed, 29 Nov 2023 09:55:48 +0100

Géza Herman <[email-masked]> writes:

> Eshel Yaron <[email-masked]> writes:
>
>> ...`completion-at-point-functions` take into account text that
>> follows point as well as the text that precedes point, and Completion
>> Preview mode works also when you're typing in the middle of a symbol.
>> For example, consider the following text in an Elisp buffer...
>
> I didn't know about this behavior, it makes sense how it works in
> emacs-lisp-mode.  I tried this feature with lsp-mode (using the
> clangd language server), and it doesn't play this nicely.
>
> Suppose that you have this C code:
>
> int main() {
>     int longVariableName = 0;
>
>     VariableName = 1;
> }
>
> And the point is at the first character at VariableName.  Now,
> pressing "l" will preview longVariableName, but it doesn't do
> anything with VariableName, so the buffer looks like
> l(ongVariableName)VariableName (parentheses are not part of the
> text, I used them to mark the greyed out part).

I see that.  I think this is a bug in `lsp-mode`, FWIW.  You get the
same erroneous completion with `completion-at-point` in that case.
Eglot seems to do the right thing though.

> My suggestion doesn't fix this, it just postpones this problem
> until I write "lon", and then the same thing will happen.

Indeed.  What follows is a tangent, I'm happy to continue the discussion
but we can already close this as "notabug", unless you think otherwise.

> The reason that I suggested this is that I use evil-mode, and I put
> evil-insert to completion-preview-commands.

Note that `completion-preview-commands` is a "list of commands that
should trigger completion preview", as the docstring says.  You seem to
indicate below that you often want `evil-insert` not to trigger
completion preview, so why add it to this list in the first place?

I'm curious, because perhaps your use case can be solved more directly.

> So whenever I enter insert mode, preview could happen.  And a lot of
> cases, I enter insert mode while the point is at the beginning of some
> word.  So with my suggestion, preview won't be happening, if the point
> is at the beginning of the word.  But currently when I enter insert
> mode, a random preview will be presented, because completion uses an
> empty string.

See above.

> Perhaps this is what makes the difference? While emacs-lisp-mode uses
> the whole word for completion (so my suggestion doesn't make sense in
> this case), but lsp-mode/clangd uses just part of the word until the
> point (my suggestion makes some sense in this case)?

AFAICT `lsp-mode` is giving you inappropriate completion suggestions,
and I don't think that it's up to Completion Preview mode to fix that.
Is this problem common among other completion backends?  If so we may
consider adding some measure to circumvent it.  But it'd be better to
improve these backends instead, IMO.

Eshel