diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go index 21e1c57411..4b9e45feda 100644 --- a/internal/lsp/source/hover.go +++ b/internal/lsp/source/hover.go @@ -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 } diff --git a/internal/lsp/testdata/godef/a/g.go b/internal/lsp/testdata/godef/a/g.go new file mode 100644 index 0000000000..4f31857e39 --- /dev/null +++ b/internal/lsp/testdata/godef/a/g.go @@ -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) diff --git a/internal/lsp/testdata/godef/a/g.go.golden b/internal/lsp/testdata/godef/a/g.go.golden new file mode 100644 index 0000000000..d46ff048bd --- /dev/null +++ b/internal/lsp/testdata/godef/a/g.go.golden @@ -0,0 +1,6 @@ +-- dur-hover -- +```go +const dur time.Duration = 910350000000 // 15m10.35s +``` + +dur is a constant of type time\.Duration\. diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index f9b6c383e8..e0b6366efd 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -15,7 +15,7 @@ ImportCount = 8 SemanticTokenCount = 3 SuggestedFixCount = 40 FunctionExtractionCount = 12 -DefinitionsCount = 64 +DefinitionsCount = 65 TypeDefinitionsCount = 2 HighlightsCount = 69 ReferencesCount = 25