bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: João Távora
Subject: bug#65418: 29.1; Eglot: support clangd inactiveRegions extension
Date: Fri, 25 Aug 2023 13:18:47 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Filippo Argiolas <filippo.argiolas@gmail.com> writes:

> On Tue, Aug 22, 2023 at 10:56 AM João Távora <joaotavora@gmail.com> 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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]