1 Star 0 Fork 0

皮豪 / lsp-bridge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
lsp-bridge-inlay-hint.el 5.59 KB
一键复制 编辑 原始数据 按行查看 历史
manateelazycat 提交于 2024-01-10 09:48 . Fix issue #823
;;; lsp-bridge-inlay-hint.el --- Inlay hint protocol -*- lexical-binding: t; no-byte-compile: t; -*-*-
;; Filename: lsp-bridge-inlay-hint.el
;; Description: Inlay hint protocol
;; Author: Andy Stewart <lazycat.manatee@gmail.com>
;; Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
;; Copyright (C) 2023, Andy Stewart, all rights reserved.
;; Created: 2023-10-03 21:35:08
;; Version: 0.1
;; Last-Updated: 2023-10-03 21:35:08
;; By: Andy Stewart
;; URL: https://www.github.org/manateelazycat/lsp-bridge-inlay-hint
;; Keywords:
;; Compatibility: GNU Emacs 30.0.50
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Inlay hint protocol
;;
;;; Installation:
;;
;; Put lsp-bridge-inlay-hint.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'lsp-bridge-inlay-hint)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET lsp-bridge-inlay-hint RET
;;
;;; Change log:
;;
;; 2023/10/03
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
;;; 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))
;; We need pass UPDATE argument to `window-end', to make sure it's value is update, not cache.
(lsp-bridge--point-position (window-end nil t))))
(defvar-local lsp-bridge-inlay-hint-overlays '())
(defun lsp-bridge-inlay-hint-hide-overlays ()
(when lsp-bridge-inlay-hint-overlays
(dolist (inlay-hint-overlay lsp-bridge-inlay-hint-overlays)
(delete-overlay inlay-hint-overlay)))
(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)
(format "%s"
(if (listp label-info)
;; We concat value of list if label is list.
(mapconcat (lambda (label)
(plist-get label :value))
label-info
"") ; Separator for mapconcat
;; 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
;; 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)
;;; lsp-bridge-inlay-hint.el ends here
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pphboy/lsp-bridge.git
git@gitee.com:pphboy/lsp-bridge.git
pphboy
lsp-bridge
lsp-bridge
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891