internal/lsp: add documentation for package-level vars

Adds documentation on hover for package-level variables and constants.

Fixes #33793

Change-Id: I01b9d36b0551f246d2ad6056806db18bdd7adb5e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208662
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Rohan Challa 2019-11-25 13:00:05 -05:00
parent a99e43fcff
commit acc15743c3
1 changed files with 11 additions and 5 deletions

View File

@ -128,7 +128,7 @@ func formatGenDecl(node *ast.GenDecl, obj types.Object, typ types.Type) (*HoverI
// If we have a field or method.
switch obj.(type) {
case *types.Var, *types.Const, *types.Func:
return formatVar(spec, obj)
return formatVar(spec, obj, node), nil
}
// Handle types.
switch spec := spec.(type) {
@ -147,7 +147,7 @@ func formatGenDecl(node *ast.GenDecl, obj types.Object, typ types.Type) (*HoverI
return nil, errors.Errorf("unable to format spec %v (%T)", spec, spec)
}
func formatVar(node ast.Spec, obj types.Object) (*HoverInformation, error) {
func formatVar(node ast.Spec, obj types.Object, decl *ast.GenDecl) *HoverInformation {
var fieldList *ast.FieldList
if spec, ok := node.(*ast.TypeSpec); ok {
switch t := spec.Type.(type) {
@ -164,13 +164,19 @@ func formatVar(node ast.Spec, obj types.Object) (*HoverInformation, error) {
field := fieldList.List[i]
if field.Pos() <= obj.Pos() && obj.Pos() <= field.End() {
if field.Doc.Text() != "" {
return &HoverInformation{source: obj, comment: field.Doc}, nil
return &HoverInformation{source: obj, comment: field.Doc}
} else if field.Comment.Text() != "" {
return &HoverInformation{source: obj, comment: field.Comment}, nil
return &HoverInformation{source: obj, comment: field.Comment}
}
}
}
}
// If we have a package level variable that does have a
// comment group attached to it but not in the ast.spec.
if decl.Doc.Text() != "" {
return &HoverInformation{source: obj, comment: decl.Doc}
}
// If we weren't able to find documentation for the object.
return &HoverInformation{source: obj}, nil
return &HoverInformation{source: obj}
}