mirror of
https://github.com/golang/go
synced 2024-11-18 22:14:56 -07:00
3576414c54
This change separates a cache package out of the golang.org/x/tools/internal/lsp/source package. The source package now uses an interface instead a File struct, which will allow it be reused more easily. The cache package contains the View and File structs now. Change-Id: Ia2114e9dafc5214c8b21bceba3adae1c36b9799d Reviewed-on: https://go-review.googlesource.com/c/152798 Reviewed-by: Ian Cottrell <iancottrell@google.com>
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package source
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go/token"
|
|
"go/types"
|
|
)
|
|
|
|
func Hover(ctx context.Context, f File, pos token.Pos) (string, Range, error) {
|
|
fAST, err := f.GetAST()
|
|
if err != nil {
|
|
return "", Range{}, err
|
|
}
|
|
pkg, err := f.GetPackage()
|
|
if err != nil {
|
|
return "", Range{}, err
|
|
}
|
|
i, err := findIdentifier(fAST, pos)
|
|
if err != nil {
|
|
return "", Range{}, err
|
|
}
|
|
if i.ident == nil {
|
|
return "", Range{}, fmt.Errorf("not a valid identifier")
|
|
}
|
|
obj := pkg.TypesInfo.ObjectOf(i.ident)
|
|
if obj == nil {
|
|
return "", Range{}, fmt.Errorf("no object")
|
|
}
|
|
if i.wasEmbeddedField {
|
|
// the original position was on the embedded field declaration
|
|
// so we try to dig out the type and jump to that instead
|
|
if v, ok := obj.(*types.Var); ok {
|
|
if n, ok := v.Type().(*types.Named); ok {
|
|
obj = n.Obj()
|
|
}
|
|
}
|
|
}
|
|
// TODO(rstambler): Add documentation and improve quality of object string.
|
|
content := types.ObjectString(obj, qualifier(fAST, pkg.Types, pkg.TypesInfo))
|
|
markdown := "```go\n" + content + "\n```"
|
|
return markdown, Range{
|
|
Start: i.ident.Pos(),
|
|
End: i.ident.End(),
|
|
}, nil
|
|
}
|