Skip to content

Commit

Permalink
Change RET action to jump to the selectionRange instead of the item…
Browse files Browse the repository at this point in the history
…'s `Range`.

The current RET action jumps to the range of the `Item` returned from
`CallHierarchyXXXCall`. A more useful behaviour (and the one VS Code
uses) is to jump to the location of the first `fromRanges` (which is
where the function is actually called).

For e.g. if function A is called by B. The current incoming call
hierarchy for A will have B. Hitting RET on B will jump to
the *definition* of B, rather than to where A is called in function B.

It is possible for multiple `Range`s to be returned in the
`selectionRange` and for now we only jump to the first one. This is
similar to the behaviour of VS Code.
  • Loading branch information
jakejx committed Nov 3, 2024
1 parent fb1a07a commit 7f0ff5f
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lsp-treemacs.el
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,8 @@ implementations automatically."

;; Call hierarchy.

(lsp-defun lsp-treemacs--call-hierarchy-ret-action ((&CallHierarchyItem :uri :selection-range (&Range :start)))
"Build the ret action for a call hierarchy item using URI and START range."
(lsp-defun lsp-treemacs--call-hierarchy-ret-action ((&CallHierarchyItem :uri :selection-range) [(&Range :start)])
"Build the ret action for a call hierarchy item using URI. START is the selection range."
(lsp-treemacs--open-file-in-mru (lsp--uri-to-path uri))
(goto-char (lsp--position-to-point start))
(run-hooks 'xref-after-jump-hook))
Expand All @@ -793,7 +793,10 @@ implementations automatically."
callback
(seq-map
(-lambda (node)
(-let* (((child-item &as &CallHierarchyItem :_name :kind :_detail? :_uri :selection-range (&Range :_start))
(-let* ((ranges (if outgoing
(lsp:call-hierarchy-outgoing-call-from-ranges node)
(lsp:call-hierarchy-incoming-call-from-ranges node)))
((child-item &as &CallHierarchyItem :_name :kind :_detail? :_uri :selection-range (&Range :_start))
(if outgoing
(lsp:call-hierarchy-outgoing-call-to node)
(lsp:call-hierarchy-incoming-call-from node)))
Expand All @@ -804,7 +807,7 @@ implementations automatically."
:children-async (-partial #'lsp-treemacs--call-hierarchy-children buffer method outgoing)
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--call-hierarchy-ret-action child-item))
(lsp-treemacs--call-hierarchy-ret-action child-item ranges))
:item child-item)))
result)))
:mode 'detached))))
Expand All @@ -822,7 +825,8 @@ With a prefix argument, show the outgoing call hierarchy."
(display-buffer-in-side-window
(lsp-treemacs-render
(seq-map
(-lambda ((item &as &CallHierarchyItem :kind :name))
(-lambda ((item &as &CallHierarchyItem :kind :name :range))
(print range)
(list :label (lsp-render-symbol item t)
:key name
:icon (lsp-treemacs-symbol-kind->icon kind)
Expand All @@ -835,7 +839,7 @@ With a prefix argument, show the outgoing call hierarchy."
outgoing)
:ret-action (lambda (&rest _)
(interactive)
(lsp-treemacs--call-hierarchy-ret-action item))
(lsp-treemacs--call-hierarchy-ret-action item (vector range)))
:item item))
(lsp-request "textDocument/prepareCallHierarchy"
(lsp--text-document-position-params)))
Expand Down

0 comments on commit 7f0ff5f

Please sign in to comment.