mirror of
https://github.com/golang/go
synced 2024-11-18 19:14:40 -07:00
internal/lsp: support hover for *ast.ImportSpec
Since 'IdentifierInfo' doesn't contain ast node of import spec, gopls will construct an empty string under plaintext mode and '```go\n\n```' under markdown mode for *ast.ImportSpec. For now, the hovering result of import spec is the corresponding node format. Fixes golang/go#33000 Change-Id: I4c25782ddb5bcc557ace82f46d480316b0b90509 GitHub-Last-Rev: 150728f401c5f9b161b557584ad3250f46e50869 GitHub-Pull-Request: golang/tools#134 Reviewed-on: https://go-review.googlesource.com/c/tools/+/185357 Reviewed-by: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
5f9351755f
commit
01b81f4f93
@ -84,6 +84,8 @@ func (d declaration) hover(ctx context.Context) (*documentation, error) {
|
||||
defer ts.End()
|
||||
obj := d.obj
|
||||
switch node := d.node.(type) {
|
||||
case *ast.ImportSpec:
|
||||
return &documentation{node, nil}, nil
|
||||
case *ast.GenDecl:
|
||||
switch obj := obj.(type) {
|
||||
case *types.TypeName, *types.Var, *types.Const, *types.Func:
|
||||
|
@ -74,7 +74,7 @@ func identifier(ctx context.Context, view View, f GoFile, pos token.Pos) (*Ident
|
||||
return nil, fmt.Errorf("pkg for %s is ill-typed", f.URI())
|
||||
}
|
||||
// Handle import specs separately, as there is no formal position for a package declaration.
|
||||
if result, err := importSpec(ctx, f, file, pkg, pos); result != nil || err != nil {
|
||||
if result, err := importSpec(f, file, pkg, pos); result != nil || err != nil {
|
||||
return result, err
|
||||
}
|
||||
path, _ := astutil.PathEnclosingInterval(file, pos, pos)
|
||||
@ -267,7 +267,7 @@ func objToNode(ctx context.Context, view View, originPkg *types.Package, obj typ
|
||||
}
|
||||
|
||||
// importSpec handles positions inside of an *ast.ImportSpec.
|
||||
func importSpec(ctx context.Context, f GoFile, fAST *ast.File, pkg Package, pos token.Pos) (*IdentifierInfo, error) {
|
||||
func importSpec(f GoFile, fAST *ast.File, pkg Package, pos token.Pos) (*IdentifierInfo, error) {
|
||||
var imp *ast.ImportSpec
|
||||
for _, spec := range fAST.Imports {
|
||||
if spec.Pos() <= pos && pos < spec.End() {
|
||||
@ -306,6 +306,7 @@ func importSpec(ctx context.Context, f GoFile, fAST *ast.File, pkg Package, pos
|
||||
return nil, fmt.Errorf("package %q has no files", importPath)
|
||||
}
|
||||
result.decl.rng = span.NewRange(f.FileSet(), dest.Name.Pos(), dest.Name.End())
|
||||
result.decl.node = imp
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
1
internal/lsp/testdata/godef/b/b.go.golden
vendored
1
internal/lsp/testdata/godef/b/b.go.golden
vendored
@ -66,6 +66,7 @@ foo/foo.go:1:9-12: defined here as
|
||||
}
|
||||
|
||||
-- PackageFoo-hover --
|
||||
myFoo "golang.org/x/tools/internal/lsp/foo" //@godef("foo", PackageFoo),godef("myFoo", PackageFoo)
|
||||
|
||||
-- S1-definition --
|
||||
godef/b/b.go:8:6-8: defined here as S1 struct {
|
||||
|
Loading…
Reference in New Issue
Block a user