Subject: bug#65418: 29.1; Eglot: support clangd inactiveRegions extension

From: João Távora <[email-masked]>
Date: Fri, 25 Aug 2023 13:18:47 +0100

Filippo Argiolas <[email-masked]> writes:

> On Tue, Aug 22, 2023 at 10:56 AM João Távora <[email-masked]> wrote:
>> I'm more worried that this isn't even out yet. Afaik Filippo you
>> compiled a Clangd 17 with a patch, right? I have done that in the
>> past, but it's not very practical every time, so either we wait for
>> this to stabilize or you have to tell me where to grab the patched
>> Clangd and llvm toolchain somewhere.
>
> It's much easier than that! They release unstable git snapshots in
> github. You can find them at https://github.com/clangd/clangd/releases
> Clangd is released as a static binary you can just copy in your path
> (e.g. in ~/.local/bin). From what I can tell you don't need to upgrade
> the whole toolchain, the binary runs fine on its own. Latest one for
> linux is at
> https://github.com/clangd/clangd/releases/download/snapshot_20230820/clangd-linux-snapshot_20230820.zip
>
> It would be great if you could test it and see if you can get server
> notifications for inactiveRegions.

OK, after fetching that git snapshot today, I've done this:

   ;;; eglot-clangd-inactive-region.el -*- lexical-binding: t; -*-
    
   (require 'eglot)
   (require 'cl-lib)
    
   (cl-defmethod eglot-client-capabilities :around (server)
     (let ((base (cl-call-next-method)))
       (when (cl-find "clangd" (process-command (jsonrpc--process server))
                      :test #'string-match)
         (setf (cl-getf (cl-getf base :textDocument)
                        :inactiveRegionsCapabilities)
               '(:inactiveRegions t)))
       base))
    
   (defvar-local eglot-clangd-inactive-region-overlays '())
    
   (cl-defmethod eglot-handle-notification
     (_server (_method (eql textDocument/inactiveRegions))
              &key regions textDocument &allow-other-keys)
     (if-let* ((path (expand-file-name (eglot--uri-to-path
                                        (cl-getf textDocument :uri))))
               (buffer (find-buffer-visiting path)))
         (with-current-buffer buffer
           (mapc #'delete-overlay eglot-clangd-inactive-region-overlays)
       (cl-loop
        for r across regions
        for (beg . end) = (eglot--range-region r)
        for ov = (make-overlay beg end)
        do
        (overlay-put ov 'face 'shadow)
        (push ov eglot-clangd-inactive-region-overlays)))))

Be sure to evaluate with lexical-binding (easiest if you save it to a
file first).

It's bare-bone but it works, because the method for communicating
"inactive regions" is very basic (and similar to unsolicited
diagnostics).

Only minimally tested, so YMMV.

This serves as a good example of how to support unofficial LSP
extensions using Eglot as an API.  Could well be in the manual.

The method for providing a client-side capability based on a server is
crude.  Servers do identify themselves properly via LSP, but only after
being initialized, so it's too late and I had to use an heuritic based
on the command.  We could also use a proper subclass for clangd servers,
but that's too verbose and overkill IMHO.

The usage of some '--' symbols like 'eglot--uri-to-path' and
'eglot--range-region' points out that these symbols should probably be
promoted to be part of the Eglot API.  Patches welcome for that.

João