Skip to content

Commit bd2c372

Browse files
committed
Implement type hierarchy
- as implemented by clangd
1 parent 9d1a42b commit bd2c372

File tree

2 files changed

+131
-52
lines changed

2 files changed

+131
-52
lines changed

README.org

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [[#screenshot-2][Screenshot]]
1212
- [[#lsp-treemacs-call-hierarchy][lsp-treemacs-call-hierarchy]]
1313
- [[#screenshot-3][Screenshot]]
14+
- [[#lsp-treemacs-type-hierarchy][lsp-treemacs-type-hierarchy]]
1415
- [[#lsp-treemacs-deps-list][lsp-treemacs-deps-list]]
1516
- [[#commands-1][Commands]]
1617
- [[#screenshot-4][Screenshot]]
@@ -45,6 +46,8 @@
4546
Display call hierarchy. Use =C-u M-x lsp-treemacs-call-hierarchy= to display outgoing call hierarchy.
4647
*** Screenshot
4748
[[file:screenshots/call-hierarchy.gif]]
49+
** lsp-treemacs-type-hierarchy
50+
Display type hierarchy.
4851
** lsp-treemacs-deps-list
4952
Browse java dependencies, namespaces, files, etc.
5053
*** Commands

lsp-treemacs.el

Lines changed: 128 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,62 +1241,138 @@ With a prefix argument, select the new window expand the tree of implementations
12411241
;; Call hierarchy.
12421242

12431243
(defun lsp-treemacs--call-hierarchy-children (buffer method key node callback)
1244-
(-let [item (plist-get node :item)]
1245-
(with-current-buffer buffer
1246-
(lsp-request-async
1247-
method
1248-
(list :item item)
1249-
(lambda (result)
1250-
(funcall
1251-
callback
1252-
(seq-map
1253-
(-lambda ((node &as &hash key (child-item &as &hash "name"
1254-
"kind" "detail" "selectionRange" (&hash "start") "uri")))
1255-
(let ((label (concat name (when detail
1256-
(propertize (concat " - " detail) 'face 'lsp-lens-face)))))
1257-
(list :label label
1258-
:key label
1259-
:icon (lsp-treemacs-symbol-kind->icon kind)
1260-
:children-async (-partial #'lsp-treemacs--call-hierarchy-children buffer method key)
1261-
:ret-action (lambda (&rest _)
1262-
(interactive)
1263-
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
1264-
(goto-char (lsp--position-to-point start))
1265-
(run-hooks 'xref-after-jump-hook))
1266-
:item child-item)))
1267-
result)))
1268-
:mode 'detached))))
1244+
(-let [item (plist-get node :item)]
1245+
(with-current-buffer buffer
1246+
(lsp-request-async
1247+
method
1248+
(list :item item)
1249+
(lambda (result)
1250+
(funcall
1251+
callback
1252+
(seq-map
1253+
(-lambda ((node &as &hash key (child-item &as &hash "name"
1254+
"kind" "detail" "selectionRange" (&hash "start") "uri")))
1255+
(let ((label (concat name (when detail
1256+
(propertize (concat " - " detail) 'face 'lsp-lens-face)))))
1257+
(list :label label
1258+
:key label
1259+
:icon (lsp-treemacs-symbol-kind->icon kind)
1260+
:children-async (-partial #'lsp-treemacs--call-hierarchy-children buffer method key)
1261+
:ret-action (lambda (&rest _)
1262+
(interactive)
1263+
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
1264+
(goto-char (lsp--position-to-point start))
1265+
(run-hooks 'xref-after-jump-hook))
1266+
:item child-item)))
1267+
result)))
1268+
:mode 'detached))))
12691269

12701270
;;;###autoload
12711271
(defun lsp-treemacs-call-hierarchy (outgoing)
1272-
"Show the incoming call hierarchy for the symbol at point.
1272+
"Show the incoming call hierarchy for the symbol at point.
12731273
With a prefix argument, show the outgoing call hierarchy."
1274-
(interactive "P")
1275-
(unless (lsp--find-workspaces-for "textDocument/prepareCallHierarchy")
1276-
(user-error "Call hierarchy not supported by the current servers: %s"
1277-
(-map #'lsp--workspace-print (lsp-workspaces))))
1278-
(let ((buffer (current-buffer)))
1279-
(select-window
1280-
(display-buffer-in-side-window
1281-
(lsp-treemacs-render
1282-
(seq-map
1283-
(-lambda ((item &as &hash "name" "kind" "detail"))
1284-
(list :label (concat name (when detail
1285-
(propertize (concat " - " detail) 'face 'lsp-lens-face)))
1286-
:key name
1287-
:icon (lsp-treemacs-symbol-kind->icon kind)
1288-
:children-async (-partial
1289-
#'lsp-treemacs--call-hierarchy-children
1290-
buffer
1291-
(if outgoing "callHierarchy/outgoingCalls"
1292-
"callHierarchy/incomingCalls")
1293-
(if outgoing "to" "from"))
1294-
:item item))
1295-
(lsp-request "textDocument/prepareCallHierarchy"
1296-
(lsp--text-document-position-params)))
1297-
(concat (if outgoing "Outgoing" "Incoming") " Call Hierarchy")
1298-
nil
1299-
"*Call Hierarchy*") nil))))
1274+
(interactive "P")
1275+
(unless (lsp--find-workspaces-for "textDocument/prepareCallHierarchy")
1276+
(user-error "Call hierarchy not supported by the current servers: %s"
1277+
(-map #'lsp--workspace-print (lsp-workspaces))))
1278+
(let ((buffer (current-buffer)))
1279+
(select-window
1280+
(display-buffer-in-side-window
1281+
(lsp-treemacs-render
1282+
(seq-map
1283+
(-lambda ((item &as &hash "name" "kind" "detail"))
1284+
(list :label (concat name (when detail
1285+
(propertize (concat " - " detail) 'face 'lsp-lens-face)))
1286+
:key name
1287+
:icon (lsp-treemacs-symbol-kind->icon kind)
1288+
:children-async (-partial
1289+
#'lsp-treemacs--call-hierarchy-children
1290+
buffer
1291+
(if outgoing "callHierarchy/outgoingCalls"
1292+
"callHierarchy/incomingCalls")
1293+
(if outgoing "to" "from"))
1294+
:item item))
1295+
(lsp-request "textDocument/prepareCallHierarchy"
1296+
(lsp--text-document-position-params)))
1297+
(concat (if outgoing "Outgoing" "Incoming") " Call Hierarchy")
1298+
nil
1299+
"*Call Hierarchy*") nil))))
1300+
1301+
1302+
1303+
;; Type hierarchy.
1304+
1305+
(defconst lsp-treemacs--hierarchy-sub 0)
1306+
(defconst lsp-treemacs--hierarchy-super 1)
1307+
(defconst lsp-treemacs--hierarchy-both 2)
1308+
1309+
(defun lsp-treemacs--type-hierarchy-render-nodes (result loaded? &optional direction)
1310+
(-map (-lambda ((it &as &hash "name" "children" "parents" "kind" "uri" "range" (&hash "start")))
1311+
`(:label ,(concat name (cond
1312+
((eq lsp-treemacs--hierarchy-sub direction) (propertize "" 'face 'shadow))
1313+
((eq lsp-treemacs--hierarchy-super direction) (propertize "" 'face 'shadow))))
1314+
:key ,name
1315+
:icon ,(lsp-treemacs-symbol-kind->icon kind)
1316+
,@(if loaded?
1317+
(list :children (append
1318+
(lsp-treemacs--type-hierarchy-render-nodes
1319+
children nil lsp-treemacs--hierarchy-sub)
1320+
(lsp-treemacs--type-hierarchy-render-nodes
1321+
parents nil lsp-treemacs--hierarchy-super)))
1322+
(list :children-async (-partial #'lsp-treemacs--type-hierarchy-render
1323+
it
1324+
direction)))
1325+
:ret-action ,(lambda (&rest _)
1326+
(interactive)
1327+
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
1328+
(goto-char (lsp--position-to-point start))
1329+
(run-hooks 'xref-after-jump-hook))))
1330+
result))
1331+
1332+
(defun lsp-treemacs--type-hierarchy-render (node direction _ callback)
1333+
(-let [(&hash "uri" "range" (&hash "start")) node]
1334+
(lsp-request-async
1335+
"textDocument/typeHierarchy"
1336+
`(:textDocument (:uri ,uri)
1337+
:position ,start
1338+
:direction ,direction
1339+
:resolve 1)
1340+
(-lambda ((&hash "children" "parents"))
1341+
(funcall callback (lsp-treemacs--type-hierarchy-render-nodes
1342+
(if (eq direction lsp-treemacs--hierarchy-sub)
1343+
children
1344+
parents)
1345+
nil direction))))))
1346+
1347+
(defun lsp-treemacs-type-hierarchy (direction)
1348+
"Show the type hierarchy for the symbol at point.
1349+
With prefix 0 show sub-types.
1350+
With prefix 1 show super-types.
1351+
With prefix 2 show both."
1352+
(interactive "P")
1353+
(unless (lsp--find-workspaces-for "textDocument/typeHierarchy")
1354+
(user-error "Type hierarchy not supported by the current servers: %s"
1355+
(-map #'lsp--workspace-print (lsp-workspaces))))
1356+
(setq direction (or direction 0))
1357+
(let ((workspaces (lsp-workspaces))
1358+
(result (lsp-request
1359+
"textDocument/typeHierarchy"
1360+
(-> (lsp--text-document-position-params)
1361+
(plist-put :direction direction)
1362+
(plist-put :resolve 1)))))
1363+
(if result
1364+
(pop-to-buffer
1365+
(lsp-treemacs-render
1366+
(lsp-treemacs--type-hierarchy-render-nodes (vector result) t)
1367+
(concat (cond
1368+
((eq lsp-treemacs--hierarchy-sub direction) "Sub")
1369+
((eq lsp-treemacs--hierarchy-super direction) "Super")
1370+
((eq lsp-treemacs--hierarchy-both direction) "Sub/Super"))
1371+
" Type Hierarchy")
1372+
nil
1373+
"*lsp-treemacs-call-hierarchy*"))
1374+
(user-error "No class under point."))
1375+
(setq lsp--buffer-workspaces workspaces)))
13001376

13011377

13021378

0 commit comments

Comments
 (0)