Skip to content

Commit 7b6891b

Browse files
committed
lsp-ui-doc: adapt lsp-mode #487 (Refactor lsp-hover)
1 parent 504d5f6 commit 7b6891b

File tree

1 file changed

+26
-48
lines changed

1 file changed

+26
-48
lines changed

lsp-ui-doc.el

+26-48
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ They are added to `markdown-code-lang-modes'")
147147
"Hooks run on child-frame creation.
148148
The functions receive 2 parameters: the frame and its window.")
149149

150-
(defvar-local lsp-ui-doc--bounds nil)
151-
152150
;; Avoid warning with emacs < 26
153151
(declare-function display-buffer-in-child-frame "window.el")
154152

@@ -265,59 +263,38 @@ MODE is the mode used in the parent frame."
265263
CONTENTS can be differents type of values:
266264
MarkedString | MarkedString[] | MarkupContent (as defined in the LSP).
267265
We don't extract the string that `lps-line' is already displaying."
268-
(when contents
269-
(cond
270-
((stringp contents) contents)
271-
((sequencep contents) ;; MarkedString[]
272-
(mapconcat 'lsp-ui-doc--extract-marked-string
273-
(lsp-ui-doc--filter-marked-string contents)
274-
"\n\n"
275-
;; (propertize "\n\n" 'face '(:height 0.4))
276-
))
277-
((gethash "kind" contents) (gethash "value" contents)) ;; MarkupContent
278-
((gethash "language" contents) ;; MarkedString
279-
(lsp-ui-doc--extract-marked-string contents)))))
280-
281-
(defun lsp-ui-doc--hover (orig-fn)
282-
"Request the documentation to the LS."
266+
(cond
267+
((stringp contents) contents)
268+
((sequencep contents) ;; MarkedString[]
269+
(mapconcat 'lsp-ui-doc--extract-marked-string
270+
(lsp-ui-doc--filter-marked-string contents)
271+
"\n\n"
272+
;; (propertize "\n\n" 'face '(:height 0.4))
273+
))
274+
((gethash "kind" contents) (gethash "value" contents)) ;; MarkupContent
275+
((gethash "language" contents) ;; MarkedString
276+
(lsp-ui-doc--extract-marked-string contents))))
277+
278+
(defun lsp-ui-doc--render-on-hover-content (orig-fn contents &rest args)
283279
(if lsp-ui-doc-mode
284-
(when (and (not (bound-and-true-p lsp-ui-peek-mode))
285-
(lsp--capability "hoverProvider"))
286-
(cond
287-
((symbol-at-point)
288-
(let ((bounds (bounds-of-thing-at-point 'symbol)))
289-
(unless (equal lsp-ui-doc--bounds bounds)
290-
(lsp--send-request-async (lsp--make-request "textDocument/hover"
291-
(lsp--text-document-position-params))
292-
(lambda (hover)
293-
(lsp-ui-doc--callback hover bounds))))))
294-
((looking-at "[[:graph:]]")
295-
(lsp--send-request-async (lsp--make-request "textDocument/hover"
296-
(lsp--text-document-position-params))
297-
(lambda (hover)
298-
(lsp-ui-doc--callback hover (cons (point) (1+ (point)))))))
299-
(t
300-
(eldoc-message nil)
301-
(lsp-ui-doc--hide-frame))))
302-
(funcall orig-fn))
303-
nil)
304-
305-
(defun lsp-ui-doc--callback (hover bounds)
280+
contents
281+
(apply orig-fn contents args)))
282+
283+
(defun lsp-ui-doc--hover-callback (orig-fn from-cache)
306284
"Process the received documentation.
307285
HOVER is the doc returned by the LS.
308286
BOUNDS are points of the symbol that have been requested.
309287
BUFFER is the buffer where the request has been made."
310-
(if (and hover
311-
(lsp--point-is-within-bounds-p (car bounds) (cdr bounds)))
312-
(let ((doc (lsp-ui-doc--extract (gethash "contents" hover))))
313-
(setq lsp-ui-doc--bounds bounds)
314-
(lsp-ui-doc--display (thing-at-point 'symbol t) doc))
315-
(eldoc-message nil)
316-
(lsp-ui-doc--hide-frame)))
288+
(if lsp-ui-doc-mode
289+
(if lsp--hover-saved-contents
290+
(lsp-ui-doc--display (thing-at-point 'symbol t)
291+
(lsp-ui-doc--extract lsp--hover-saved-contents))
292+
(eldoc-message nil)
293+
(lsp-ui-doc--hide-frame))
294+
(funcall orig-fn)))
317295

318296
(defun lsp-ui-doc--hide-frame ()
319297
"Hide the frame."
320-
(setq lsp-ui-doc--bounds nil)
321298
(when (overlayp lsp-ui-doc--inline-ov)
322299
(delete-overlay lsp-ui-doc--inline-ov))
323300
(when (lsp-ui-doc--get-frame)
@@ -633,7 +610,8 @@ HEIGHT is the documentation number of lines."
633610
(cl-callf copy-tree frameset-filter-alist)
634611
(push '(lsp-ui-doc-frame . :never) frameset-filter-alist)))
635612

636-
(advice-add 'lsp-hover :around #'lsp-ui-doc--hover)
613+
(advice-add 'lsp--render-on-hover-content :around #'lsp-ui-doc--render-on-hover-content)
614+
(advice-add 'lsp--hover-callback :around #'lsp-ui-doc--hover-callback)
637615
(add-hook 'delete-frame-functions 'lsp-ui-doc--on-delete nil t)))
638616
(t
639617
(remove-hook 'delete-frame-functions 'lsp-ui-doc--on-delete t))))

0 commit comments

Comments
 (0)