From 61483d104abdaccec5e16baea23b99392ba3e779 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Tue, 17 Dec 2019 00:32:08 -0500 Subject: [PATCH] internal/lsp: show comments for grouped variable declarations Fixes golang/go#36139 Change-Id: I8aaf1a5d65b8d66ea7bb350439cc960c7505307d Reviewed-on: https://go-review.googlesource.com/c/tools/+/211637 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick --- internal/lsp/source/hover.go | 5 ++++- internal/lsp/testdata/godef/a/a.go | 5 +++++ internal/lsp/testdata/godef/a/a.go.golden | 16 +++++++++++----- internal/lsp/testdata/godef/b/b.go.golden | 20 ++++++++++---------- internal/lsp/testdata/summary.txt.golden | 2 +- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go index c032170876..43bcd1bc7f 100644 --- a/internal/lsp/source/hover.go +++ b/internal/lsp/source/hover.go @@ -212,13 +212,16 @@ func formatGenDecl(node *ast.GenDecl, obj types.Object, typ types.Type) (*HoverI func formatVar(node ast.Spec, obj types.Object, decl *ast.GenDecl) *HoverInformation { var fieldList *ast.FieldList - if spec, ok := node.(*ast.TypeSpec); ok { + switch spec := node.(type) { + case *ast.TypeSpec: switch t := spec.Type.(type) { case *ast.StructType: fieldList = t.Fields case *ast.InterfaceType: fieldList = t.Methods } + case *ast.ValueSpec: + return &HoverInformation{source: obj, comment: spec.Doc} } // If we have a struct or interface declaration, // we need to match the object to the corresponding field or method. diff --git a/internal/lsp/testdata/godef/a/a.go b/internal/lsp/testdata/godef/a/a.go index 0ea47e35ff..39eb39735c 100644 --- a/internal/lsp/testdata/godef/a/a.go +++ b/internal/lsp/testdata/godef/a/a.go @@ -8,6 +8,11 @@ import ( "sync" ) +var ( + // x is a variable. + x string //@x,hover("x", x) +) + type A string //@A func AStuff() { //@AStuff diff --git a/internal/lsp/testdata/godef/a/a.go.golden b/internal/lsp/testdata/godef/a/a.go.golden index 92ae96da62..70d0c42679 100644 --- a/internal/lsp/testdata/godef/a/a.go.golden +++ b/internal/lsp/testdata/godef/a/a.go.golden @@ -75,7 +75,7 @@ func Random2(y int) int func Random2(y int) int ``` -- err-definition -- -godef/a/a.go:18:6-9: defined here as ```go +godef/a/a.go:23:6-9: defined here as ```go var err error ``` -- err-definition-json -- @@ -83,14 +83,14 @@ var err error "span": { "uri": "file://godef/a/a.go", "start": { - "line": 18, + "line": 23, "column": 6, - "offset": 228 + "offset": 287 }, "end": { - "line": 18, + "line": 23, "column": 9, - "offset": 231 + "offset": 290 } }, "description": "```go\nvar err error\n```" @@ -112,3 +112,9 @@ func(t Type, size ...IntegerType) Type ```go string ``` +-- x-hover -- +x is a variable\. + +```go +var x string +``` diff --git a/internal/lsp/testdata/godef/b/b.go.golden b/internal/lsp/testdata/godef/b/b.go.golden index 46c3e12abb..2d4837f6fd 100644 --- a/internal/lsp/testdata/godef/b/b.go.golden +++ b/internal/lsp/testdata/godef/b/b.go.golden @@ -1,5 +1,5 @@ -- A-definition -- -godef/a/a.go:11:6-7: defined here as [`a.A` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#A) +godef/a/a.go:16:6-7: defined here as [`a.A` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#A) ```go A string //@A @@ -10,14 +10,14 @@ A string //@A "span": { "uri": "file://godef/a/a.go", "start": { - "line": 11, + "line": 16, "column": 6, - "offset": 100 + "offset": 159 }, "end": { - "line": 11, + "line": 16, "column": 7, - "offset": 101 + "offset": 160 } }, "description": "[`a.A` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#A)\n\n```go\nA string //@A\n\n```" @@ -61,7 +61,7 @@ package a ("golang.org/x/tools/internal/lsp/godef/a") package a ("golang.org/x/tools/internal/lsp/godef/a") ``` -- AStuff-definition -- -godef/a/a.go:13:6-12: defined here as [`a.AStuff` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#AStuff) +godef/a/a.go:18:6-12: defined here as [`a.AStuff` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#AStuff) ```go func a.AStuff() @@ -71,14 +71,14 @@ func a.AStuff() "span": { "uri": "file://godef/a/a.go", "start": { - "line": 13, + "line": 18, "column": 6, - "offset": 120 + "offset": 179 }, "end": { - "line": 13, + "line": 18, "column": 12, - "offset": 126 + "offset": 185 } }, "description": "[`a.AStuff` on pkg.go.dev](https://pkg.go.dev/golang.org/x/tools/internal/lsp/godef/a#AStuff)\n\n```go\nfunc a.AStuff()\n```" diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index a9a8c6e91e..22d4e6f3c6 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -11,7 +11,7 @@ FoldingRangesCount = 2 FormatCount = 6 ImportCount = 7 SuggestedFixCount = 1 -DefinitionsCount = 42 +DefinitionsCount = 43 TypeDefinitionsCount = 2 HighlightsCount = 44 ReferencesCount = 7