internal/lsp: show human-readable const time.Duration as a comment

The current hover information for constant time.Duration is not very
useful because it displays nanoseconds. So, show formatted duration
as an inline comment.

Fixes golang/go#44667

Change-Id: I6177455fb8932d1914d5cf623c0d9c4eff8f0b3f
GitHub-Last-Rev: e168968012741a1e614c66bc97fe60b196943ed3
GitHub-Pull-Request: golang/tools#281
Reviewed-on: https://go-review.googlesource.com/c/tools/+/297310
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Trust: Rebecca Stambler <rstambler@golang.org>
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Shoshin Nikita 2021-03-03 19:03:07 +00:00 committed by Rebecca Stambler
parent 9c452d8574
commit dafbee5034
4 changed files with 27 additions and 1 deletions

View File

@ -9,10 +9,12 @@ import (
"encoding/json"
"fmt"
"go/ast"
"go/constant"
"go/doc"
"go/format"
"go/types"
"strings"
"time"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/lsp/protocol"
@ -232,6 +234,18 @@ func objectString(obj types.Object, qf types.Qualifier) string {
switch obj := obj.(type) {
case *types.Const:
str = fmt.Sprintf("%s = %s", str, obj.Val())
// Try to add a formatted duration as an inline comment
typ, ok := obj.Type().(*types.Named)
if !ok {
break
}
pkg := typ.Obj().Pkg()
if pkg.Path() == "time" && pkg.Scope().Lookup("Duration") != nil {
if d, ok := constant.Int64Val(obj.Val()); ok {
str += " // " + time.Duration(d).String()
}
}
}
return str
}

6
internal/lsp/testdata/godef/a/g.go vendored Normal file
View File

@ -0,0 +1,6 @@
package a
import "time"
// dur is a constant of type time.Duration.
const dur = 15*time.Minute + 10*time.Second + 350*time.Millisecond //@dur,hover("dur", dur)

View File

@ -0,0 +1,6 @@
-- dur-hover --
```go
const dur time.Duration = 910350000000 // 15m10.35s
```
dur is a constant of type time\.Duration\.

View File

@ -15,7 +15,7 @@ ImportCount = 8
SemanticTokenCount = 3
SuggestedFixCount = 40
FunctionExtractionCount = 12
DefinitionsCount = 64
DefinitionsCount = 65
TypeDefinitionsCount = 2
HighlightsCount = 69
ReferencesCount = 25