Skip to content

Commit 33afad3

Browse files
authored
Move inlay hints implementation in core (#3970)
1 parent 5c3ce8b commit 33afad3

File tree

5 files changed

+133
-246
lines changed

5 files changed

+133
-246
lines changed

CHANGELOG.org

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
* Add [[https://github.com/charliermarsh/ruff-lsp][ruff-lsp]] support (additional server for Python).
7373
* Update documentation of Vue3 install server
7474
* Add GLSL support.
75+
* Move inlay hint support in core from ~lsp-rust~ and ~lsp-javascript~. Use
76+
~lsp-inlay-hint-enable~ instead of ~lsp-rust-analyzer-server-display-inlay-hints~ and
77+
~lsp-javascript-server-display-inlay-hints~
7578
** Release 8.0.0
7679
* Add ~lsp-clients-angular-node-get-prefix-command~ to get the Angular server from another location which is still has ~/lib/node_modules~ in it.
7780
* Set ~lsp-clients-angular-language-server-command~ after the first connection to speed up subsequent connections.

clients/lsp-javascript.el

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -599,50 +599,12 @@ TypeScript 3.0 or newer in the workspace."
599599
:type 'boolean
600600
:package-version '(lsp-mode . "6.1"))
601601

602-
;; inlay hints
603-
604-
(defface lsp-javascript-inlay-face
605-
'((t :inherit font-lock-comment-face))
606-
"The face to use for the JavaScript inlays."
607-
:group 'lsp-javascript
608-
:package-version '(lsp-mode . "8.0.1"))
609-
610-
(defface lsp-javascript-inlay-type-face
611-
'((t :inherit lsp-javascript-inlay-face))
612-
"Face for inlay type hints (e.g. inferred variable types)."
613-
:group 'lsp-javascript
614-
:package-version '(lsp-mode . "8.0.1"))
615-
616-
(defcustom lsp-javascript-inlay-type-format ": %s"
617-
"Format string for variable inlays (part of the inlay face)."
618-
:type '(string :tag "String")
619-
:group 'lsp-javascript
620-
:package-version '(lsp-mode . "8.0.1"))
621-
622-
(defface lsp-javascript-inlay-parameter-face
623-
'((t :inherit lsp-javascript-inlay-face))
624-
"Face for inlay parameter hints (e.g. function parameter names at
625-
call-site)."
626-
:group 'lsp-javascript
627-
:package-version '(lsp-mode . "8.0.1"))
628-
629-
(defcustom lsp-javascript-inlay-param-format "%s:"
630-
"Format string for parameter inlays (part of the inlay face)."
631-
:type '(string :tag "String")
632-
:group 'lsp-javascript
633-
:package-version '(lsp-mode . "8.0.1"))
634-
635602
(defcustom lsp-typescript-surveys-enabled t
636603
"Enabled/disable occasional surveys that help us improve VS
637604
Code's JavaScript and TypeScript support."
638605
:type 'boolean
639606
:package-version '(lsp-mode . "6.1"))
640607

641-
(defcustom lsp-javascript-display-inlay-hints nil
642-
"Whether to display inlay hints."
643-
:type 'boolean
644-
:package-version '(lsp-mode . "8.0.1"))
645-
646608
(defcustom lsp-javascript-display-enum-member-value-hints nil
647609
"Show inlay hints for enum member values."
648610
:type 'boolean
@@ -681,11 +643,6 @@ name (e.g. `data' variable passed as `data' parameter)."
681643
:type 'boolean
682644
:package-version '(lsp-mode . "8.0.1"))
683645

684-
(defcustom lsp-javascript-update-inlay-hints-on-scroll t
685-
"Update inlay hints immediately when scrolling or modifying window sizes."
686-
:type 'boolean
687-
:package-version '(lsp-mode . "8.0.1"))
688-
689646
(defcustom lsp-javascript-completions-complete-function-calls t
690647
"Complete function calls."
691648
:type 'boolean
@@ -829,75 +786,10 @@ name (e.g. `data' variable passed as `data' parameter)."
829786
(lsp)
830787
(lsp--info "Renamed '%s' to '%s'." name (file-name-nondirectory new)))))
831788

832-
(defun lsp-javascript-update-inlay-hints-scroll-function (window start)
833-
(lsp-javascript--update-inlay-hints start (window-end window t)))
834-
835-
(defun lsp-javascript-update-inlay-hints ()
836-
(lsp-javascript--update-inlay-hints (window-start) (window-end nil t)))
837-
838-
(defun lsp-javascript--update-inlay-hints (start end)
839-
(if (lsp-javascript-initialized?)
840-
(lsp-request-async
841-
"textDocument/inlayHint"
842-
(lsp-make-javascript-inlay-hints-params
843-
:text-document (lsp--text-document-identifier)
844-
:range (lsp-make-range :start
845-
(lsp-point-to-position start)
846-
:end
847-
(lsp-point-to-position end)))
848-
(lambda (res)
849-
(lsp--remove-overlays 'lsp-javascript-inlay-hint)
850-
(dolist (hint res)
851-
(-let* (((&javascript:InlayHint :label :position :kind :padding-left? :padding-right?) hint)
852-
(pos (lsp--position-to-point position))
853-
(overlay (make-overlay pos pos nil 'front-advance 'end-advance)))
854-
(when (stringp label)
855-
(overlay-put overlay 'lsp-javascript-inlay-hint t)
856-
(overlay-put overlay 'before-string
857-
(format "%s%s%s"
858-
(if padding-left? " " "")
859-
(propertize (lsp-javascript-format-inlay label kind)
860-
'font-lock-face (lsp-javascript-face-for-inlay kind))
861-
(if padding-right? " " "")))))))
862-
:mode 'tick)))
863-
864-
(defun lsp-javascript-column-at-pos (pos)
865-
(save-excursion
866-
(goto-char pos)
867-
(current-column)))
868-
869-
(defun lsp-javascript-format-inlay (text kind)
870-
(cond
871-
((eql kind lsp/javascript-inlay-hint-kind-type-hint) (format lsp-javascript-inlay-type-format text))
872-
((eql kind lsp/javascript-inlay-hint-kind-parameter-hint) (format lsp-javascript-inlay-param-format text))
873-
;; ((eql kind lsp/javascript-inlay-hint-kind-enum-hint) (format lsp-javascript-inlay-enum-format text))
874-
(t text)))
875-
876-
(defun lsp-javascript-face-for-inlay (kind)
877-
(cond
878-
((eql kind lsp/javascript-inlay-hint-kind-type-hint) 'lsp-javascript-inlay-type-face)
879-
((eql kind lsp/javascript-inlay-hint-kind-parameter-hint) 'lsp-javascript-inlay-parameter-face)
880-
(t 'lsp-javascript-inlay-face)))
881-
882789
(defun lsp-javascript-initialized? ()
883790
(when-let ((workspace (lsp-find-workspace 'ts-ls (buffer-file-name))))
884791
(eq 'initialized (lsp--workspace-status workspace))))
885792

886-
(define-minor-mode lsp-javascript-inlay-hints-mode
887-
"Mode for displaying inlay hints."
888-
:lighter nil
889-
(cond
890-
(lsp-javascript-inlay-hints-mode
891-
(lsp-javascript-update-inlay-hints)
892-
(add-hook 'lsp-on-idle-hook #'lsp-javascript-update-inlay-hints nil t)
893-
(when lsp-javascript-update-inlay-hints-on-scroll
894-
(add-to-list (make-local-variable 'window-scroll-functions) #'lsp-javascript-update-inlay-hints-scroll-function)))
895-
(t
896-
(lsp--remove-overlays 'lsp-javascript-inlay-hint)
897-
(remove-hook 'lsp-on-idle-hook #'lsp-javascript-update-inlay-hints t)
898-
(when lsp-javascript-update-inlay-hints-on-scroll
899-
(setf window-scroll-functions (delete #'lsp-javascript-update-inlay-hints-scroll-function window-scroll-functions))))))
900-
901793
(defun lsp-clients-typescript-project-ts-server-path ()
902794
(f-join (lsp-workspace-root) "node_modules" "typescript" "lib" "tsserver.js"))
903795

@@ -946,9 +838,6 @@ name (e.g. `data' variable passed as `data' parameter)."
946838
(format-enable (or lsp-javascript-format-enable lsp-typescript-format-enable)))
947839
(lsp:set-server-capabilities-document-formatting-provider? caps format-enable)
948840
(lsp:set-server-capabilities-document-range-formatting-provider? caps format-enable)))
949-
:after-open-fn (lambda ()
950-
(when lsp-javascript-display-inlay-hints
951-
(lsp-javascript-inlay-hints-mode)))
952841
:ignore-messages '("readFile .*? requested by TypeScript but content not available")
953842
:server-id 'ts-ls
954843
:request-handlers (ht ("_typescript.rename" #'lsp-javascript--rename))

clients/lsp-rust.el

Lines changed: 4 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,6 @@ to a closure.ppOnly applies to closures with blocks, same as
378378
:group 'lsp-rust-analyzer
379379
:package-version '(lsp-mode . "8.0.1"))
380380

381-
(defcustom lsp-rust-analyzer-server-display-inlay-hints nil
382-
"Show inlay hints."
383-
:type 'boolean
384-
:group 'lsp-rust-analyzer
385-
:package-version '(lsp-mode . "6.2"))
386-
387381
(defcustom lsp-rust-analyzer-max-inlay-hint-length nil
388382
"Max inlay hint length."
389383
:type 'integer
@@ -783,7 +777,7 @@ or JSON objects in `rust-project.json` format."
783777
:parameterHints ,(lsp-json-bool lsp-rust-analyzer-display-parameter-hints)
784778
:reborrowHints ,lsp-rust-analyzer-display-reborrow-hints
785779
:renderColons ,(lsp-json-bool lsp-rust-analyzer-server-format-inlay-hints)
786-
:typeHints (:enable ,(lsp-json-bool lsp-rust-analyzer-server-display-inlay-hints)
780+
:typeHints (:enable ,(lsp-json-bool lsp-inlay-hint-enable)
787781
:hideClosureInitialization ,(lsp-json-bool lsp-rust-analyzer-hide-closure-initialization)
788782
:hideNamedConstructor ,(lsp-json-bool lsp-rust-analyzer-hide-named-constructor)))
789783
:completion (:addCallParenthesis ,(lsp-json-bool lsp-rust-analyzer-completion-add-call-parenthesis)
@@ -1233,9 +1227,7 @@ tokens legend."
12331227
("public" . ,lsp-rust-analyzer-public-modifier)
12341228
("reference" . ,lsp-rust-analyzer-reference-modifier)
12351229
("trait" . ,lsp-rust-analyzer-trait-modifier)
1236-
("unsafe" . ,lsp-rust-analyzer-unsafe-modifier)
1237-
))
1238-
1230+
("unsafe" . ,lsp-rust-analyzer-unsafe-modifier)))
12391231

12401232
(lsp-register-client
12411233
(make-lsp-client
@@ -1255,14 +1247,9 @@ tokens legend."
12551247
("rust-analyzer.showReferences" #'lsp-rust--analyzer-show-references)
12561248
("rust-analyzer.triggerParameterHints" #'lsp--action-trigger-parameter-hints))
12571249
:library-folders-fn (lambda (_workspace) lsp-rust-analyzer-library-directories)
1258-
:after-open-fn (lambda ()
1259-
(when lsp-rust-analyzer-server-display-inlay-hints
1260-
(lsp-rust-analyzer-inlay-hints-mode)))
1261-
:ignore-messages nil
1262-
12631250
:semantic-tokens-faces-overrides `(:discard-default-modifiers t
1264-
:modifiers
1265-
,(lsp-rust-analyzer--semantic-modifiers))
1251+
:modifiers
1252+
,(lsp-rust-analyzer--semantic-modifiers))
12661253
:server-id 'rust-analyzer
12671254
:custom-capabilities `((experimental . ((snippetTextEdit . ,(and lsp-enable-snippet (featurep 'yasnippet))))))
12681255
:download-server-fn (lambda (_client callback error-callback _update?)
@@ -1282,42 +1269,6 @@ tokens legend."
12821269

12831270
;; inlay hints
12841271

1285-
(defvar-local lsp-rust-analyzer-inlay-hints-timer nil)
1286-
1287-
(defface lsp-rust-analyzer-inlay-face
1288-
'((t :inherit font-lock-comment-face))
1289-
"The face to use for the Rust Analyzer inlays."
1290-
:group 'lsp-rust-analyzer
1291-
:package-version '(lsp-mode . "7.0"))
1292-
1293-
(defface lsp-rust-analyzer-inlay-type-face
1294-
'((t :inherit lsp-rust-analyzer-inlay-face))
1295-
"Face for inlay type hints (e.g. inferred variable types)."
1296-
:group 'lsp-rust-analyzer
1297-
:package-version '(lsp-mode . "8.0.0"))
1298-
1299-
(defcustom lsp-rust-analyzer-inlay-type-format ": %s"
1300-
"Format string for variable inlays (part of the inlay face,
1301-
used only if lsp-rust-analyzer-server-format-inlay-hints is
1302-
non-nil)."
1303-
:type '(string :tag "String")
1304-
:group 'lsp-rust-analyzer
1305-
:package-version '(lsp-mode . "8.0.0"))
1306-
1307-
(defface lsp-rust-analyzer-inlay-param-face
1308-
'((t :inherit lsp-rust-analyzer-inlay-face))
1309-
"Face for inlay parameter hints (e.g. function parameter names at call-site)."
1310-
:group 'lsp-rust-analyzer
1311-
:package-version '(lsp-mode . "8.0.0"))
1312-
1313-
(defcustom lsp-rust-analyzer-inlay-param-format "%s:"
1314-
"Format string for parameter inlays (part of the inlay face,
1315-
used only if lsp-rust-analyzer-server-format-inlay-hints is
1316-
non-nil)."
1317-
:type '(string :tag "String")
1318-
:group 'lsp-rust-analyzer
1319-
:package-version '(lsp-mode . "8.0.0"))
1320-
13211272
(defcustom lsp-rust-analyzer-debug-lens-extra-dap-args
13221273
'(:MIMode "gdb" :miDebuggerPath "gdb" :stopAtEntry t :externalConsole :json-false)
13231274
"Extra arguments to pass to DAP template when debugging a test from code lens.
@@ -1331,85 +1282,10 @@ meaning."
13311282
:group 'lsp-rust-analyzer
13321283
:package-version '(lsp-mode . "8.0.0"))
13331284

1334-
(defvar lsp-rust-analyzer-already-warned-about-inlay-hint-type nil)
1335-
1336-
(defun lsp-rust-analyzer-update-inlay-hints (buffer)
1337-
(if (and (lsp-rust-analyzer-initialized?)
1338-
(eq buffer (current-buffer)))
1339-
(lsp-request-async
1340-
"textDocument/inlayHint"
1341-
(lsp-make-rust-analyzer-inlay-hints-params
1342-
:text-document (lsp--text-document-identifier)
1343-
:range (if (use-region-p)
1344-
(lsp--region-to-range (region-beginning) (region-end))
1345-
(lsp--region-to-range (point-min) (point-max))))
1346-
(lambda (res)
1347-
(remove-overlays (point-min) (point-max) 'lsp-rust-analyzer-inlay-hint t)
1348-
(dolist (hint res)
1349-
(-let* (((&rust-analyzer:InlayHint :position :label :kind :padding-left :padding-right) hint)
1350-
(pos (lsp--position-to-point position))
1351-
(overlay (make-overlay pos pos nil 'front-advance 'end-advance)))
1352-
(let ((concat-label
1353-
(cl-typecase label
1354-
(vector
1355-
(string-join
1356-
(mapcar
1357-
(lambda (label)
1358-
(when (lsp-structure-p label)
1359-
(lsp-get label :value)))
1360-
label)))
1361-
(string
1362-
label)
1363-
(t
1364-
(unless lsp-rust-analyzer-already-warned-about-inlay-hint-type
1365-
(message "Unexpected type for inlay hint: %s" (type-of label))
1366-
(setq lsp-rust-analyzer-already-warned-about-inlay-hint-type t))
1367-
""))))
1368-
(overlay-put overlay 'lsp-rust-analyzer-inlay-hint t)
1369-
(overlay-put overlay 'before-string
1370-
(format "%s%s%s"
1371-
(if padding-left " " "")
1372-
(propertize (lsp-rust-analyzer-format-inlay concat-label kind)
1373-
'font-lock-face (lsp-rust-analyzer-face-for-inlay kind))
1374-
(if padding-right " " "")))))))
1375-
:mode 'tick))
1376-
nil)
1377-
1378-
(defun lsp-rust-analyzer-format-inlay (label kind)
1379-
(if lsp-rust-analyzer-server-format-inlay-hints
1380-
label
1381-
(cond
1382-
((eql kind lsp/rust-analyzer-inlay-hint-kind-type-hint) (format lsp-rust-analyzer-inlay-type-format label))
1383-
((eql kind lsp/rust-analyzer-inlay-hint-kind-param-hint) (format lsp-rust-analyzer-inlay-param-format label))
1384-
(t label))))
1385-
1386-
(defun lsp-rust-analyzer-face-for-inlay (kind)
1387-
(cond
1388-
((eql kind lsp/rust-analyzer-inlay-hint-kind-type-hint) 'lsp-rust-analyzer-inlay-type-face)
1389-
((eql kind lsp/rust-analyzer-inlay-hint-kind-param-hint) 'lsp-rust-analyzer-inlay-param-face)
1390-
(t 'lsp-rust-analyzer-inlay-face)))
1391-
13921285
(defun lsp-rust-analyzer-initialized? ()
13931286
(when-let ((workspace (lsp-find-workspace 'rust-analyzer (buffer-file-name))))
13941287
(eq 'initialized (lsp--workspace-status workspace))))
13951288

1396-
(defun lsp-rust-analyzer-inlay-hints-change-handler (&rest _rest)
1397-
(when lsp-rust-analyzer-inlay-hints-timer
1398-
(cancel-timer lsp-rust-analyzer-inlay-hints-timer))
1399-
(setq lsp-rust-analyzer-inlay-hints-timer
1400-
(run-with-idle-timer 0.1 nil #'lsp-rust-analyzer-update-inlay-hints (current-buffer))))
1401-
1402-
(define-minor-mode lsp-rust-analyzer-inlay-hints-mode
1403-
"Mode for displaying inlay hints."
1404-
:lighter nil
1405-
(cond
1406-
(lsp-rust-analyzer-inlay-hints-mode
1407-
(lsp-rust-analyzer-update-inlay-hints (current-buffer))
1408-
(add-hook 'lsp-on-change-hook #'lsp-rust-analyzer-inlay-hints-change-handler nil t))
1409-
(t
1410-
(remove-overlays (point-min) (point-max) 'lsp-rust-analyzer-inlay-hint t)
1411-
(remove-hook 'lsp-on-change-hook #'lsp-rust-analyzer-inlay-hints-change-handler t))))
1412-
14131289
(defun lsp-rust-analyzer-expand-macro ()
14141290
"Expands the macro call at point recursively."
14151291
(interactive)

0 commit comments

Comments
 (0)