1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:04:43 -07:00

internal/lsp: fix docs on hover for ungrouped package variables

Fixes golang/go#38525

Change-Id: I8833c925663b67b2c82ad4cbf580d1c6f3c7a81d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/230305
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-04-27 12:55:45 -04:00
parent 9ea0146da6
commit e0d5eebdf8
10 changed files with 71 additions and 31 deletions

View File

@ -486,7 +486,7 @@ func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) {
return []byte(hover.Contents.Value), nil
}))
if hover.Contents.Value != expectHover {
t.Errorf("for %v got %q want %q", d.Src, hover.Contents.Value, expectHover)
t.Errorf("%s:\n%s", d.Src, tests.Diff(expectHover, hover.Contents.Value))
}
}
if !d.OnlyHover {

View File

@ -281,7 +281,14 @@ func formatVar(node ast.Spec, obj types.Object, decl *ast.GenDecl) *HoverInforma
fieldList = t.Methods
}
case *ast.ValueSpec:
return &HoverInformation{source: obj, comment: spec.Doc}
comment := decl.Doc
if comment == nil {
comment = spec.Doc
}
if comment == nil {
comment = spec.Comment
}
return &HoverInformation{source: obj, comment: comment}
}
// If we have a struct or interface declaration,
// we need to match the object to the corresponding field or method.
@ -296,14 +303,7 @@ func formatVar(node ast.Spec, obj types.Object, decl *ast.GenDecl) *HoverInforma
}
}
}
// If we have a package level variable that does have a
// comment group attached to it but not in the ast.spec.
if decl.Doc.Text() != "" {
return &HoverInformation{source: obj, comment: decl.Doc}
}
// If we weren't able to find documentation for the object.
return &HoverInformation{source: obj}
return &HoverInformation{source: obj, comment: decl.Doc}
}
func FormatHover(h *HoverInformation, options Options) (string, error) {

View File

@ -13,6 +13,9 @@ var (
x string //@x,hover("x", x)
)
// z is a variable too.
var z string //@z,hover("z", z)
type A string //@mark(AString, "A")
func AStuff() { //@AStuff

View File

@ -75,28 +75,32 @@ func Random2(y int) int
func Random2(y int) int
```
-- err-definition --
godef/a/a.go:23:6-9: defined here as ```go
godef/a/a.go:26:6-9: defined here as ```go
var err error
```
\@err
-- err-definition-json --
{
"span": {
"uri": "file://godef/a/a.go",
"start": {
"line": 23,
"line": 26,
"column": 6,
"offset": 304
"offset": 361
},
"end": {
"line": 23,
"line": 26,
"column": 9,
"offset": 307
"offset": 364
}
},
"description": "```go\nvar err error\n```"
"description": "```go\nvar err error\n```\n\n\\@err"
}
-- err-hover --
\@err
```go
var err error
```
@ -118,3 +122,9 @@ x is a variable\.
```go
var x string
```
-- z-hover --
z is a variable too\.
```go
var z string
```

View File

@ -68,6 +68,8 @@ var Other Thing
```
[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)
\@Other
-- Other-definition-json --
{
"span": {
@ -83,10 +85,12 @@ var Other Thing
"offset": 91
}
},
"description": "```go\nvar Other Thing\n```\n\n[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)"
"description": "```go\nvar Other Thing\n```\n\n[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)\n\n\\@Other"
}
-- Other-hover --
\@Other
[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)
```go

View File

@ -29,7 +29,7 @@ package a ("golang.org/x/tools/internal/lsp/godef/a")
package a ("golang.org/x/tools/internal/lsp/godef/a")
```
-- AString-definition --
godef/a/a.go:16:6-7: defined here as ```go
godef/a/a.go:19:6-7: defined here as ```go
A string //@mark(AString, "A")
```
@ -40,14 +40,14 @@ A string //@mark(AString, "A")
"span": {
"uri": "file://godef/a/a.go",
"start": {
"line": 16,
"line": 19,
"column": 6,
"offset": 159
"offset": 216
},
"end": {
"line": 16,
"line": 19,
"column": 7,
"offset": 160
"offset": 217
}
},
"description": "```go\nA string //@mark(AString, \"A\")\n\n```\n\n[`a.A` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#A)"
@ -61,7 +61,7 @@ A string //@mark(AString, "A")
```
-- AStuff-definition --
godef/a/a.go:18:6-12: defined here as ```go
godef/a/a.go:21:6-12: defined here as ```go
func a.AStuff()
```
@ -71,14 +71,14 @@ func a.AStuff()
"span": {
"uri": "file://godef/a/a.go",
"start": {
"line": 18,
"line": 21,
"column": 6,
"offset": 196
"offset": 253
},
"end": {
"line": 18,
"line": 21,
"column": 12,
"offset": 202
"offset": 259
}
},
"description": "```go\nfunc a.AStuff()\n```\n\n[`a.AStuff` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#AStuff)"
@ -308,6 +308,8 @@ const X untyped int = 0
```
[`b.X` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/b#X)
\@mark\(bX, \"X\"\),godef\(\"X\", bX\)
-- bX-definition-json --
{
"span": {
@ -323,10 +325,12 @@ const X untyped int = 0
"offset": 814
}
},
"description": "```go\nconst X untyped int = 0\n```\n\n[`b.X` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/b#X)"
"description": "```go\nconst X untyped int = 0\n```\n\n[`b.X` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/b#X)\n\n\\@mark\\(bX, \\\"X\\\"\\),godef\\(\\\"X\\\", bX\\)"
}
-- bX-hover --
\@mark\(bX, \"X\"\),godef\(\"X\", bX\)
[`b.X` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/b#X)
```go

View File

@ -38,6 +38,8 @@ var a.Other a.Thing
```
[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)
\@Other
-- Other-definition-json --
{
"span": {
@ -53,10 +55,12 @@ var a.Other a.Thing
"offset": 91
}
},
"description": "```go\nvar a.Other a.Thing\n```\n\n[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)"
"description": "```go\nvar a.Other a.Thing\n```\n\n[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)\n\n\\@Other"
}
-- Other-hover --
\@Other
[`a.Other` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#Other)
```go

View File

@ -2,6 +2,8 @@
godef/broken/unclosedIf.go:7:7-19: defined here as ```go
var myUnclosedIf string
```
\@myUnclosedIf
-- myUnclosedIf-definition-json --
{
"span": {
@ -17,10 +19,12 @@ var myUnclosedIf string
"offset": 80
}
},
"description": "```go\nvar myUnclosedIf string\n```"
"description": "```go\nvar myUnclosedIf string\n```\n\n\\@myUnclosedIf"
}
-- myUnclosedIf-hover --
\@myUnclosedIf
```go
var myUnclosedIf string
```

View File

@ -12,7 +12,7 @@ FoldingRangesCount = 2
FormatCount = 6
ImportCount = 8
SuggestedFixCount = 6
DefinitionsCount = 46
DefinitionsCount = 47
TypeDefinitionsCount = 2
HighlightsCount = 52
ReferencesCount = 11

View File

@ -522,3 +522,14 @@ func EnableAllAnalyzers(snapshot source.Snapshot, opts *source.Options) {
}
}
}
func Diff(want, got string) string {
if want == got {
return ""
}
// Add newlines to avoid newline messages in diff.
want += "\n"
got += "\n"
d := myers.ComputeEdits("", want, got)
return fmt.Sprintf("%q", diff.ToUnified("want", "got", want, d))
}