From 39aadb5b76f2c3d1fdd59106c9e5efb3f77f4f33 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 14 May 2020 17:16:05 -0400 Subject: [PATCH] internal/lsp: fix docs on hover for var/const blocks The priorities for which comment to show should be 1) documentation directly above the var/const, 2) documentation for the var/const block, 3) line comments. See https://github.com/microsoft/vscode-go/issues/3240. Change-Id: Ie136f0f25ac8208147070682bb1f3a663d6da25f Reviewed-on: https://go-review.googlesource.com/c/tools/+/234101 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Heschi Kreinick --- internal/lsp/source/hover.go | 4 ++-- .../lsp/testdata/lsp/primarymod/godef/a/a.go | 8 +++++++ .../lsp/primarymod/godef/a/a.go.golden | 22 ++++++++++++++----- .../lsp/primarymod/godef/b/b.go.golden | 20 ++++++++--------- internal/lsp/testdata/lsp/summary.txt.golden | 2 +- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go index 073aa3fa1e..d3d26c7f6b 100644 --- a/internal/lsp/source/hover.go +++ b/internal/lsp/source/hover.go @@ -306,9 +306,9 @@ func formatVar(node ast.Spec, obj types.Object, decl *ast.GenDecl) *HoverInforma fieldList = t.Methods } case *ast.ValueSpec: - comment := decl.Doc + comment := spec.Doc if comment == nil { - comment = spec.Doc + comment = decl.Doc } if comment == nil { comment = spec.Comment diff --git a/internal/lsp/testdata/lsp/primarymod/godef/a/a.go b/internal/lsp/testdata/lsp/primarymod/godef/a/a.go index f957c19a41..f818acc822 100644 --- a/internal/lsp/testdata/lsp/primarymod/godef/a/a.go +++ b/internal/lsp/testdata/lsp/primarymod/godef/a/a.go @@ -12,6 +12,14 @@ var ( x string //@x,hover("x", x) ) +// Constant block. When I hover on h, I should see this comment. +const ( + // When I hover on g, I should see this comment. + g = 1 //@g,hover("g", g) + + h = 2 //@h,hover("h", h) +) + // z is a variable too. var z string //@z,hover("z", z) diff --git a/internal/lsp/testdata/lsp/primarymod/godef/a/a.go.golden b/internal/lsp/testdata/lsp/primarymod/godef/a/a.go.golden index 60b3bbd988..e21d537bff 100644 --- a/internal/lsp/testdata/lsp/primarymod/godef/a/a.go.golden +++ b/internal/lsp/testdata/lsp/primarymod/godef/a/a.go.golden @@ -81,7 +81,7 @@ func Random2(y int) int -- aPackage-hover -- Package a is a package for testing go to definition\. -- err-definition -- -godef/a/a.go:25:6-9: defined here as ```go +godef/a/a.go:33:6-9: defined here as ```go var err error ``` @@ -91,14 +91,14 @@ var err error "span": { "uri": "file://godef/a/a.go", "start": { - "line": 25, + "line": 33, "column": 6, - "offset": 418 + "offset": 597 }, "end": { - "line": 25, + "line": 33, "column": 9, - "offset": 421 + "offset": 600 } }, "description": "```go\nvar err error\n```\n\n\\@err" @@ -110,6 +110,18 @@ var err error ```go var err error ``` +-- g-hover -- +When I hover on g, I should see this comment\. + +```go +const g untyped int = 1 +``` +-- h-hover -- +Constant block\. + +```go +const h untyped int = 2 +``` -- make-hover -- The make built\-in function allocates and initializes an object of type slice, map, or chan \(only\)\. diff --git a/internal/lsp/testdata/lsp/primarymod/godef/b/b.go.golden b/internal/lsp/testdata/lsp/primarymod/godef/b/b.go.golden index c2eefff8ec..7f14091dc0 100644 --- a/internal/lsp/testdata/lsp/primarymod/godef/b/b.go.golden +++ b/internal/lsp/testdata/lsp/primarymod/godef/b/b.go.golden @@ -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:18:6-7: defined here as ```go +godef/a/a.go:26: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": 18, + "line": 26, "column": 6, - "offset": 273 + "offset": 452 }, "end": { - "line": 18, + "line": 26, "column": 7, - "offset": 274 + "offset": 453 } }, "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:20:6-12: defined here as ```go +godef/a/a.go:28: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": 20, + "line": 28, "column": 6, - "offset": 310 + "offset": 489 }, "end": { - "line": 20, + "line": 28, "column": 12, - "offset": 316 + "offset": 495 } }, "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)" diff --git a/internal/lsp/testdata/lsp/summary.txt.golden b/internal/lsp/testdata/lsp/summary.txt.golden index 0742c0e5b6..56d30df3ac 100644 --- a/internal/lsp/testdata/lsp/summary.txt.golden +++ b/internal/lsp/testdata/lsp/summary.txt.golden @@ -12,7 +12,7 @@ FoldingRangesCount = 2 FormatCount = 6 ImportCount = 8 SuggestedFixCount = 6 -DefinitionsCount = 51 +DefinitionsCount = 53 TypeDefinitionsCount = 2 HighlightsCount = 52 ReferencesCount = 11