You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This CL adds basic support for the LSP Type Hierarchy feature.
There are three new RPCs:
- PrepareTypeHierarchy asks for a description of the currently
selected text, which must be a reference to a type name.
- Supertypes and Subtypes ask for the items related by the
subtyping relation, using the same machinery as
Implementations by method sets, which has been factored
to deliver a concurrent stream of results at a higher
level then just protocol.Location.
Unlike Implementations, Type Hierarchy does not report
relationships between func types and FuncDecl/FuncLit/RangeStmt.
The names of types are now saved in the methodsets index.
The marker test framework has been extended with
@{super,sub}types markers.
This CL also sets us up to start reporting interface/interface
relationships (golang/go#68641), which are especially desirable
in the Type Hierarchy viewer; but that behavior change will be
left for a follow-up.
+ tests, docs, relnotes
Fixesgolang/go#72142
Change-Id: Id60c9f447e938ac7e34262522ccd79bd54d90fc5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/663055
Auto-Submit: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Commit-Queue: Alan Donovan <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
Copy file name to clipboardExpand all lines: gopls/doc/features/navigation.md
+36-1Lines changed: 36 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ Interfaces and concrete types are matched using method sets:
94
94
location of the declaration of each type that implements
95
95
the interface.
96
96
- When invoked on a **concrete type**,
97
-
it returns the locations of the matching interface types.
97
+
it returns the locations of the matching interface types.
98
98
- When invoked on an **interface method**, it returns the corresponding
99
99
methods of the types that satisfy the interface.
100
100
- When invoked on a **concrete method**,
@@ -282,3 +282,38 @@ Client support:
282
282
-**VS Code**: `Show Call Hierarchy` menu item (`⌥⇧H`) opens [Call hierarchy view](https://code.visualstudio.com/docs/cpp/cpp-ide#_call-hierarchy) (note: docs refer to C++ but the idea is the same for Go).
283
283
-**Emacs + eglot**: Not standard; install with `(package-vc-install "https://github.com/dolmens/eglot-hierarchy")`. Use `M-x eglot-hierarchy-call-hierarchy` to show the direct incoming calls to the selected function; use a prefix argument (`C-u`) to show the direct outgoing calls. There is no way to expand the tree.
284
284
-**CLI**: `gopls call_hierarchy file.go:#offset` shows outgoing and incoming calls.
285
+
286
+
287
+
## Type Hierarchy
288
+
289
+
The LSP TypeHierarchy mechanism consists of three queries that
290
+
together enable clients to present a hierarchical view of a portion of
291
+
the subtyping relation over named types.
292
+
293
+
-[`textDocument/prepareTypeHierarchy`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#textDocument_prepareTypeHierarchy) returns an [item](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#typeHierarchyItem) describing the named type at the current position;
294
+
-[`typeHierarchyItem/subtypes`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#typeHierarchy_subtypes) returns the set of subtypes of the selected (interface) type; and
295
+
-[`typeHierarchy/supertypes`](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification#typeHierarchy_supertypes) returns the set of supertypes (interface types) of the selected type.
296
+
297
+
Invoke the command while selecting the name of a type.
298
+
299
+
As with an Implementation query, a type hierarchy query reports
300
+
function-local types only within the same package as the query type.
301
+
Also the result does not include alias types, only defined types.
302
+
303
+
<!--
304
+
The screenshot below shows ...
305
+
TODO: screenshot, but wait till #68641 is fixed.
306
+
<img title="Subtypes of io.Reader" src="../assets/subtypes.png" width="640">
307
+
-->
308
+
309
+
Caveats:
310
+
311
+
- The type hierarchy supports only named types and their assignability
312
+
relation. By contrast, the Implementations request also reports the
313
+
relation between unnamed `func` types and function declarations,
314
+
function literals, and dynamic calls of values of those types.
315
+
316
+
Client support:
317
+
-**VS Code**: `Show Type Hierarchy` menu item opens [Type hierarchy view](https://code.visualstudio.com/docs/java/java-editing#_type-hierarchy) (note: docs refer to Java but the idea is the same for Go).
318
+
-**Emacs + eglot**: Support added in March 2025. Use `M-x eglot-show-call-hierarchy`.
0 commit comments