Skip to content

Commit 992bf9c

Browse files
xieyuschenadonovan
authored andcommitted
gopls/internal/golang/hover: show alias real type decl for types only
The existing hover tries to get the real type decl for all kinds of objects, which makes the returned hoverResult contains a non-empty typeDecl for a var. As a result, hovering over 'alias' gets wrong output 'type Named int' rather than expected 'var alias Alias'. type Named int type Alias = Named var alias Alias This CL has fixed this wrong behavior by getting typeDecl only if the object is a type, including type parameter. So hovering over a variable will behave correctly. Fixes golang/go#74361 Change-Id: I64cac07bc92ebfbd113fd2dac78eecb2ef8426cc Reviewed-on: https://go-review.googlesource.com/c/tools/+/685775 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Mark Freeman <[email protected]>
1 parent 861996a commit 992bf9c

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

gopls/internal/golang/hover.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,10 +556,15 @@ func hover(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, pp pro
556556
}
557557
}
558558

559-
// realTypeDecl is defined to store the underlying definition of an alias.
560-
realTypeDecl, _ := findRhsTypeDecl(ctx, snapshot, pkg, obj) // tolerate the error
561-
if realTypeDecl != "" {
562-
typeDecl += fmt.Sprintf("\n\n%s", realTypeDecl)
559+
if isTypeName {
560+
// get the real type decl only if current object is a type,
561+
// for non-types, we'd better hide the real type decl to avoid possible confusion.
562+
//
563+
// realTypeDecl is defined to store the underlying definition of an alias.
564+
realTypeDecl, _ := findRhsTypeDecl(ctx, snapshot, pkg, obj) // tolerate the error
565+
if realTypeDecl != "" {
566+
typeDecl += fmt.Sprintf("\n\n%s", realTypeDecl)
567+
}
563568
}
564569

565570
// Compute link data (on pkg.go.dev or other documentation host).

gopls/internal/test/marker/testdata/hover/hover_alias.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This test checks gopls behavior when hovering over alias type.
66
-- go.mod --
77
module mod.com
88

9+
go 1.18
10+
911
-- main.go --
1012
package main
1113

@@ -35,6 +37,17 @@ type RealType struct {
3537
Age int
3638
}
3739

40+
-- generic/a.go --
41+
package generic
42+
func generic[T any]() {}
43+
44+
type Named string
45+
type Alias = Named
46+
47+
func _(){
48+
generic[Alias]() //@hover("Alias", "Alias", Alias)
49+
}
50+
3851
-- @ToTypeDecl --
3952
```go
4053
type ToTypeDecl = b.RealType // size=24 (0x18)
@@ -79,3 +92,13 @@ type ToAliasWithComment = a.AliasWithComment // size=24 (0x18)
7992
---
8093

8194
[`main.ToAliasWithComment` on pkg.go.dev](https://pkg.go.dev/mod.com#ToAliasWithComment)
95+
-- @Alias --
96+
```go
97+
type Alias = Named
98+
99+
type Named string
100+
```
101+
102+
---
103+
104+
[`generic.Alias` on pkg.go.dev](https://pkg.go.dev/mod.com/generic#Alias)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- flags --
2+
-skip_goarch=386,arm
3+
4+
-- settings.json --
5+
{"analyses": {"unusedfunc": false}}
6+
7+
-- go.mod --
8+
module mod.com
9+
10+
-- a/a.go --
11+
package a
12+
13+
type (
14+
Named int
15+
Alias = Named
16+
Alias2 = Alias
17+
)
18+
19+
var (
20+
named Named
21+
alias Alias //@hover("alias", "alias", alias)
22+
alias2 Alias2 //@hover("alias2", "alias2", alias2)
23+
)
24+
25+
-- @alias --
26+
```go
27+
var alias Alias
28+
```
29+
30+
---
31+
32+
@hover("alias", "alias", alias)
33+
-- @alias2 --
34+
```go
35+
var alias2 Alias2
36+
```
37+
38+
---
39+
40+
@hover("alias2", "alias2", alias2)

0 commit comments

Comments
 (0)