Skip to content

Commit

Permalink
Add comment for inlay hint module.
Browse files Browse the repository at this point in the history
  • Loading branch information
manateelazycat committed Oct 6, 2023
1 parent 9a1bc67 commit 7ec6650
Showing 1 changed file with 60 additions and 43 deletions.
103 changes: 60 additions & 43 deletions lsp-bridge-inlay-hint.el
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@

;;; Code:

(defface lsp-bridge-inlay-hint-face
`((t :foreground "#aaaaaa"))
"Face for inlay hint."
:group 'lsp-bridge-inlay-hint)

(defun lsp-bridge-inlay-hint ()
(lsp-bridge-call-file-api "inlay_hint"
(lsp-bridge--point-position (window-start))
Expand All @@ -99,50 +104,62 @@

(setq-local lsp-bridge-inlay-hint-overlays nil))

(defun lsp-bridge-inlay-hint-padding-text (hint-padding is-left)
(and hint-padding
(not (eq hint-padding :json-false))
(not (memq (if is-left
(char-before)
(char-after))
'(32 9)))
" "))

(defun lsp-bridge-inlay-hint-label-text (label-info)
(defun lsp-bridge-inlay-hint-label-text (label-info)
(format "%s"
(if (listp label-info)
;; We concat value of list if label is list.
(mapconcat (lambda (label)
(plist-get label :value))
label-info)
;; Otherwise label is string, just return itself.
label-info))))

(defun lsp-bridge-inlay-hint--render (filepath filehost inlay-hints)
(lsp-bridge--with-file-buffer filepath filehost
(lsp-bridge-inlay-hint-hide-overlays)

(save-excursion
(save-restriction
(let ((hint-index 0))
(dolist (hint inlay-hints)
(goto-char (acm-backend-lsp-position-to-point (plist-get hint :position)))
(let* ((hint-kind (plist-get hint :kind))
(hint-peg-after-p (eql hint-kind 1))
(hint-text (format "%s"
(cond ((listp (plist-get hint :label))
(apply 'concat
(mapcar (lambda (label)
(concat (plist-get label :value) " "))
(plist-get hint :label)
)))
(t (plist-get hint :label)))))
(hint-padding-left (plist-get hint :paddingLeft))
(hint-padding-right (plist-get hint :paddingRight))
(hint-padding-left-text (and hint-padding-left
(not (eq hint-padding-left :json-false))
(not (memq (char-before) '(32 9)))
" "))
(hint-padding-right-text (and hint-padding-right
(not (eq hint-padding-right :json-false))
(not (memq (char-after) '(32 9)))
" "))
(hint-insert-text (concat hint-padding-left-text hint-text hint-padding-right-text))
(overlay (if hint-peg-after-p
(make-overlay (point) (1+ (point)) nil t)
(make-overlay (1- (point)) (point) nil nil nil))))
(when (and (equal hint-index 0)
hint-peg-after-p)
(put-text-property 0 1 'cursor 1 hint-insert-text))
(overlay-put overlay
(if hint-peg-after-p 'before-string 'after-string)
(propertize hint-insert-text 'face `(:foreground "#aaaaaa")))
(overlay-put overlay 'evaporate t)
(push overlay lsp-bridge-inlay-hint-overlays)

(setq hint-index (1+ hint-index))
)))))))
(lsp-bridge--with-file-buffer
filepath filehost
;; Hide previous overlays first.
(lsp-bridge-inlay-hint-hide-overlays)

;; Render new overlays.
(save-excursion
(save-restriction
(let ((hint-index 0))
(dolist (hint inlay-hints)
(goto-char (acm-backend-lsp-position-to-point (plist-get hint :position)))
(let* ((hint-kind (plist-get hint :kind))
;; InlayHintKind is 1 mean is an inlay hint that for a type annotation.
;; 2 mean is an inlay hint that is for a parameter.
;; type annotation is hint need render at end of line, we use `after-string' overlay to implement it.
(hint-render-use-after-string-p (eql hint-kind 1))
;; Hint text need concat padding-left, label and padding-right.
(hint-text (concat
(lsp-bridge-inlay-hint-padding-text (plist-get hint :paddingLeft) t)
(lsp-bridge-inlay-hint-label-text (plist-get hint :label))
(lsp-bridge-inlay-hint-padding-text (plist-get hint :paddingRight) nil)))
(overlay (if hint-render-use-after-string-p
(make-overlay (point) (1+ (point)) nil t)
(make-overlay (1- (point)) (point) nil nil nil))))
(when (and (equal hint-index 0)
hint-render-use-after-string-p)
(put-text-property 0 1 'cursor 1 hint-text))
(overlay-put overlay
(if hint-render-use-after-string-p 'before-string 'after-string)
(propertize hint-text 'face 'lsp-bridge-inlay-hint-face))
(overlay-put overlay 'evaporate t) ; NOTE, `evaporate' is import
(push overlay lsp-bridge-inlay-hint-overlays)

(setq hint-index (1+ hint-index))
)))))))

(provide 'lsp-bridge-inlay-hint)

Expand Down

0 comments on commit 7ec6650

Please sign in to comment.