emacs-lsp / lsp-mode

Emacs client/library for the Language Server Protocol
https://emacs-lsp.github.io/lsp-mode
GNU General Public License v3.0
4.75k stars 869 forks source link

Possibility to send range in a hover request #4014

Open kurnevsky opened 1 year ago

kurnevsky commented 1 year ago

There is an open issue in lsp protocol to allow to send ranges in hover requests. It's already implemented in some lsp servers, e.g rust-analyzer and metals. However because it's not standardized the implementation is different - metals adds an additional optional field while rust-analyzer accepts range in the position field. For metals it can be implemented in a hacky way like:

(defun lsp--text-document-position-params (&optional identifier position range)
  "Make TextDocumentPositionParams for the current point in the current document.
If IDENTIFIER, POSITION and RANGE are non-nil, they will be used as the document
identifier and the position respectively."
  (list :textDocument (or identifier (lsp--text-document-identifier))
    :position (or position (lsp--cur-position))
    :range (or range (when (use-region-p) (lsp--region-to-range (region-beginning) (region-end))))))

This is useful to infer type of the selection.

kurnevsky commented 1 year ago

Metals also sets capabilities.experimental.rangeHoverProvider: true in the initialize response which can be used (while rust one is capabilities.experimental.hoverRange).

matklad commented 10 months ago

I'd be open to changing rust-analyzer implementation to match that of metals!