Skip to content

Support clangd's editsNearCursor #5015

@rohhamh

Description

@rohhamh

Normally one would put -> in front of a pointer variable to get the completion candidates.
However it's also possible to get them by putting a ., by setting editsNearCursor client capability as it seems.

An example of such edit is the following screenshot from neovim, the listed toJsonArray needs a -> but I'm getting it with a . instead.

Image

To get the same behaviour, I set the editsNearCursor cap with the following code (this is provided by llm, is there a better way to do this? I couldn't find any documentation)

(after! lsp-mode
  (defun my/lsp-modify-clangd (caps)
    (setf (alist-get 'editsNearCursor
                     (alist-get 'completion
                                (alist-get 'textDocument caps)))
          t)
    caps)

  (advice-add 'lsp--client-capabilities
              :filter-return #'my/lsp-modify-clangd))

and also made this change (also provided by llm)

(after! lsp-clangd
  (advice-add 'lsp-completion--guess-prefix :filter-return
              (lambda (start)
                (when (and start
                           (char-after start)
                           (= (char-after start) ?.))
                  (setq start (1+ start)))
                start))
  )

The conclusion the llm made when it finally worked is as follows:

clangd's editsNearCursor returns completion items with a textEdit whose range starts before the . so it can rewrite it to ->. lsp-completion--guess-prefix faithfully returns that pre-. position, making the prefix ".", which doesn't match any member name, so company shows nothing.
The one-liner advice skips past the . so the prefix becomes "", and since :company-prefix-length t was already set, company doesn't filter on it and shows all the candidates.

The way I understand it is that some prefix (. here) is expected among all the candidates of clangd's response for completions to work currently.

I would be very happy to know if

  • There's a better way to set capabilities, and if you'd include editsNearCursor as default for clangd (or make it more easily configurable)
  • There's' a more straightforward way that the same result as (advice-add 'lsp-completion--guess-prefix :filter-return ...) can be achieved which I couldn't find (seems nobody cares about editsNearCursor on the web).
  • You think this would make sense for a feature

Thank you for what you're doing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions