Subject: bug#65578: Eglot with mouse

From: João Távora <[email-masked]>
Date: Mon, 28 Aug 2023 10:20:12 +0100

Juri Linkov <[email-masked]> writes:

> X-Debbugs-CC: João Távora <[email-masked]>
>
> 0. emacs -Q
> 1. enable context-menu-mode
> 2. enable eglot in a suitable buffer
> 3. move point to one identifier
> 4. click the right mouse button on another identifier
> 5. in the context menu click "Find Definition"
>
> The problem is that a wrong identifier is visited.
>
> Whereas 'xref-find-definitions-at-mouse' takes care
> about getting the right identifier at the clicked position,
> Eglot returns a fictitious identifier "LSP identifier at point"
> that can't be used to find the identifier at the mouse click.

I don't think the name Eglot gives to the identifier matters here.  What
matters is that the LSP request is made with the correct buffer position
(where the user clicked).

So if 'xref-find-definitions-at-mouse' already has logic to protect
that, it should work.

> xref-find-definitions-at-mouse uses save-excursion with
> mouse-set-point before calling xref-backend-identifier-at-point.
> But the same save-excursion with mouse-set-point can't be added
> around xref-find-definitions because save-excursion will restore
> the original position after visiting the found identifier.

Alas, somehow, the position obtained when mouse is in the correct spot
in the first step must be recorded somehow and survive until the second
step.  Else, with LSP, it just won't work, not in the near future at
least.  I'd say this is something to be changed in Xref.

> Also tried to add xref-find-definitions-at-mouse to the list
> xref-prompt-for-identifier, no effect.
>
> Then tried to remove '(xref--prompt-p this-command)'
> from 'xref-backend-identifier-at-point ((_backend (eql eglot)))'.
> This fixed xref-find-definitions-at-mouse, but still
> doesn't work for xref-find-references-at-mouse.
> Also removing '(xref--prompt-p this-command)' has such a strange effect
> that it started to find approximate fuzzy matches that sound like
> the original identifier.

I don't think any of this is the solution.

This is a more-or-less typical case where the workhorse function
non-mouse function called by the mouse function does too much.  An
elegant (perhaps verbose) solution would separate the
definition-requesting from the definition-visiting parts of the former,
and then the mouse function would use the two parts separately.

But in practice I think something like a variable shared set in the
interactive spec and read in the function body.  Or maybe a text
property in the dummy "LSP Identifier at point" string.  Let's see what
Dmitry has to say.

João