Skip to content

Commit

Permalink
internal/lsp: support hover for *ast.ImportSpec.
Browse files Browse the repository at this point in the history
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. Related issue: golang/go#33000.
  • Loading branch information
Henry Wong committed Jul 11, 2019
1 parent 4fc3618 commit 8388f4a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions internal/lsp/source/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions internal/lsp/source/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions internal/lsp/testdata/godef/b/b.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8388f4a

Please sign in to comment.